首頁  >  文章  >  後端開發  >  Python程式找到字串的權重

Python程式找到字串的權重

WBOY
WBOY轉載
2023-09-04 20:09:10959瀏覽

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 中,首先找到字串中的字元頻率,然後將該頻率乘以該字元的權重。對給定字串中使用的所有字元重複此過程,然後計算最終的字串權重。

以上是Python程式找到字串的權重的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除