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)表示charweight字典中给定字符n1的字符权重。
这里’*’被用作乘法运算符
这里’+’被用作加法运算符
使用的公式
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中,首先找到字符串中的字符频率,然后将该频率乘以该字符的权重。将该过程与给定字符串中使用的所有字符重复,然后计算最终的字符串权重。