卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章17335本站已运行3326

Python程序找到字符串的权重

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 步 - 迭代字符权重字典并找到给定字符串中每个字符的权重值。

第 5 步 - 将字符出现的频率与其权重相乘。

第 6 步 - 通过添加此计算值来更新字符串权重。

第 7 步 - 重复此操作并在最后打印总结果。

给定公式中使用的术语的说明

TotalWeight 是给定测试字符串的总重量。

N1,n2表示给定测试字符串中出现的字符

Occr(n1) 表示在给定的测试字符串中出现 n1。

Weight(n1) 表示给定字符 n1 在 charweight 字典中的字符权重。

这里‘*’被用作数字的乘法运算符

这里‘+’被用作数字的加法运算符

使用的公式

TotalWeight= (Occr(n1) * 权重(n1)) + (Occr(n2) * 权重(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 中,首先找到字符串中的字符频率,然后将该频率乘以该字符的权重。对给定字符串中使用的所有字符重复此过程,然后计算最终的字符串权重。

卓越飞翔博客
上一篇: 计算在仅一个位置上不同的字符串对的数量
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏