Python程序查找字符串权重
本文的任务是查找字符串的总权重。为了计算字符串权重,我们将给定的字符串转换为小写形式。考虑到字符的权重,我们取a=1,b=2,以此类推,直到z=26。在这篇Python文章中,通过两个不同的示例给出了查找给定字符串权重的方法。在第一个示例中,给定字符串中的字符依次为fetc、hed,然后将它们的权重加到更新的权重中。在示例2中,首先计算给定字符串中某个字符出现的频率,然后将该频率乘以相应的字符权重,然后将所有这些分量权重加在一起得到最终结果。
示例1:使用迭代并添加字符权重来查找字符串权重。
算法
步骤1 − 首先创建atoz = 'abcdefghijklmnopqrstuvwxyz'。
步骤2 − 我们将使用atoz.index()函数获取权重数字,例如,这里空格' '将具有0值,b将具有2值,依此类推。
步骤3 − 现在指定要计算字符串权重的给定字符串。
步骤4 − 遍历给定字符串,逐个获取字符。
步骤5 − 在atoz中查找字符的位置值(权重值)。
步骤6 − 通过添加字符的权重值来更新字符串权重。
步骤7 − 最后,在最后打印总结果。
示例
givenstr = 'this is a sample string' def calculateWeight(teststr): teststr = teststr.lower() atoz = ' abcdefghijklmnopqrstuvwxyz' weight = 0 for item in range(len(teststr)): elem = teststr[item] currweight = atoz.index(elem) weight += currweight print("This albhabet:",elem, ", alphabet weight:", currweight, ", Updated String Weight ", weight) return weight finalresult= calculateWeight(givenstr) print("Final String Weight: ",finalresult)
输出
This albhabet: t , alphabet weight: 20 , Updated String Weight 20 This albhabet: h , alphabet weight: 8 , Updated String Weight 28 This albhabet: i , alphabet weight: 9 , Updated String Weight 37 This albhabet: s , alphabet weight: 19 , Updated String Weight 56 This albhabet: , alphabet weight: 0 , Updated String Weight 56 This albhabet: i , alphabet weight: 9 , Updated String Weight 65 This albhabet: s , alphabet weight: 19 , Updated String Weight 84 This albhabet: , alphabet weight: 0 , Updated String Weight 84 This albhabet: a , alphabet weight: 1 , Updated String Weight 85 This albhabet: , alphabet weight: 0 , Updated String Weight 85 This albhabet: s , alphabet weight: 19 , Updated String Weight 104 This albhabet: a , alphabet weight: 1 , Updated String Weight 105 This albhabet: m , alphabet weight: 13 , Updated String Weight 118 This albhabet: p , alphabet weight: 16 , Updated String Weight 134 This albhabet: l , alphabet weight: 12 , Updated String Weight 146 This albhabet: e , alphabet weight: 5 , Updated String Weight 151 This albhabet: , alphabet weight: 0 , Updated String Weight 151 This albhabet: s , alphabet weight: 19 , Updated String Weight 170 This albhabet: t , alphabet weight: 20 , Updated String Weight 190 This albhabet: r , alphabet weight: 18 , Updated String Weight 208 This albhabet: i , alphabet weight: 9 , Updated String Weight 217 This albhabet: n , alphabet weight: 14 , Updated String Weight 231 This albhabet: g , alphabet weight: 7 , Updated String Weight 238 Final String Weight: 238
示例2:使用字符权重和出现次数公式查找字符串权重
算法
步骤1 − 首先创建一个名为charweight的字典:{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, ……直到'z': 26}。
步骤2 − 现在指定要计算字符串权重的给定字符串。
步骤3 − 查找给定字符串中字符出现的频率。
步骤4 − 遍历charweight字典,并找到给定字符串中每个字符的权重值。
步骤5 − 将字符出现的频率乘以其权重。
步骤6 − 通过添加此计算值来更新字符串权重。
步骤7 − 重复此操作,并在最后打印总结果。
给定公式中使用的术语的描述
TotalWeight是给定测试字符串的总权重。
N1、n2表示给定测试字符串中出现的字符
Occr(n1)表示n1在给定测试字符串中出现的次数。
Weight(n1)表示给定字符n1在charweight字典中的字符权重。
这里' * '用作数字的乘法运算符
这里' + '用作数字的加法运算符
使用的公式
TotalWeight= (Occr(n1) * Weight(n1)) + (Occr(n2) * Weight(n2)) ……依此类推
示例
givenstr = 'this is a sample string' charweight= {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26} WeightSum=0 occurFreq = {} for i in givenstr: if i in occurFreq: occurFreq[i] += 1 else: occurFreq[i] = 1 print("Char Weights: " , charweight) print("Occurance: ", occurFreq) for alphabetChar, alphabetCharCount in occurFreq.items(): print(alphabetChar, ":", alphabetCharCount) for key in charweight.keys(): if key.find(alphabetChar) > -1: #print(charweight[key]*alphabetCharCount) WeightSum=WeightSum + charweight[key]*alphabetCharCount #print(WeightSum) print("This albhabet:",alphabetChar, ", alphabet Count:", alphabetCharCount, ", alphabet Weight:", charweight[key], " Updated String Weight ", WeightSum) print("Final String Weight: ", WeightSum)
输出
Char Weights: {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26} Occurance: {'t': 2, 'h': 1, 'i': 3, 's': 4, ' ': 4, 'a': 2, 'm': 1, 'p': 1, 'l': 1, 'e': 1, 'r': 1, 'n': 1, 'g': 1} t : 2 This albhabet: t , alphabet Count: 2 , alphabet Weight: 20 Updated String Weight 40 h : 1 This albhabet: h , alphabet Count: 1 , alphabet Weight: 8 Updated String Weight 48 i : 3 This albhabet: i , alphabet Count: 3 , alphabet Weight: 9 Updated String Weight 75 s : 4 This albhabet: s , alphabet Count: 4 , alphabet Weight: 19 Updated String Weight 151 : 4 a : 2 This albhabet: a , alphabet Count: 2 , alphabet Weight: 1 Updated String Weight 153 m : 1 This albhabet: m , alphabet Count: 1 , alphabet Weight: 13 Updated String Weight 166 p : 1 This albhabet: p , alphabet Count: 1 , alphabet Weight: 16 Updated String Weight 182 l : 1 This albhabet: l , alphabet Count: 1 , alphabet Weight: 12 Updated String Weight 194 e : 1 This albhabet: e , alphabet Count: 1 , alphabet Weight: 5 Updated String Weight 199 r : 1 This albhabet: r , alphabet Count: 1 , alphabet Weight: 18 Updated String Weight 217 n : 1 This albhabet: n , alphabet Count: 1 , alphabet Weight: 14 Updated String Weight 231 g : 1 This albhabet: g , alphabet Count: 1 , alphabet Weight: 7 Updated String Weight 238 Final String Weight: 238
结论
我们在这里提供了两种不同的方法来展示如何查找给定字符串的字符串权重。首先,从给定的测试字符串中逐个获取使用的字符,然后添加其各自的权重。通过重复此过程,计算出最终的字符串权重。在示例2中,首先找到字符串中字符的频率,然后将该频率乘以该字符的权重。此过程对给定字符串中使用的所有字符重复,然后计算最终的字符串权重。