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

更新於:2023年7月10日

535 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

立即開始
廣告