Value in list, python

Each character in English has a percentage of occurrence, these are percentages:

ABCDEFGHI .0817 .0149 .0278 .0425 .1270 .0223 .0202 .0609 .0697 JKLMNOPQR .0015 .0077 .0402 .0241 .0675 .0751 .0193 .0009 .0599 STUVWXYZ .0633 .0906 .0276 .0098 .0236 .0015 .0197 .0007 

A list called letterGoodness predefined as:

 letterGoodness = [.0817,.0149,.0278,.0425,.1270,.0223,.0202,... 

I need to find the "kindness" of the string. For example, the kindness of β€œI EAT” is: .0697 +.1270 +.0817 +.0906 = .369. This is part of a more serious problem, but I need to solve this problem in order to solve a bigger problem. I started like this:

 def goodness(message): for i in L: for j in i: 

This will be enough to learn how to get the percentage of occurrence of any character. Could you help me? The string contains only uppercase letters and spaces.

+4
source share
2 answers

letterGoodness is better than a dictionary, then you can simply:

 sum(letterGoodness.get(c,0) for c in yourstring.upper()) # #^.upper for defensive programming 

To convert letterGoodness from your list to a dictator, you can do:

 import string letterGoodness = dict(zip(string.ascii_uppercase,letterGoodness)) 

If you only have letters and spaces in uppercase, you can do:

 letterGoodness = dict(zip(string.ascii_uppercase,letterGoodness)) letterGoodness[' '] = 0 sum(letterGoodness[c] for c in yourstring) 

but the performance gain here is probably pretty minimal, so I would prefer a more robust version above.


If you insist on keeping letterGoodness as a list (and I do not recommend this), you can use the built-in ord to get the index (cwallenpoole is indicated):

  ordA = ord('A') sum(letterGoodness[ord(c)-ordA] for c in yourstring if c in string.ascii_uppercase) 

I'm too lazy to timeit right now, but you can also define a temporary set to store string.ascii_uppercase . Perhaps your function will work a little faster (depending on how optimized str.__contains__ is compared to set.__contains__ ):

  ordA = ord('A') big_letters = set(string.ascii_uppercase) sum(letterGoodness[ord(c)-ordA] for c in yourstring.upper() if c in big_letters) 
+12
source

You will be better off using the dictionary data structure.

EDIT: This is not my source code, but instead the code updated on DSM lines.

 import string num_vals = [.0817, .0149, .0278, .0425, .1270, .0223, .0202, .0609, .0697 , .0015, .0077, .0402, .0241, .0675, .0751, .0193, .0009, .0599, .0633, .0906, .0276, .0098, .0236, .0015, .0197, .0007] letterGoodness = {letter : value for letter,value in map(None, string.ascii_uppercase, num_vals)} def goodness(message): string_goodness = 0 for letter in message: letter = letter.upper() if letter in letterGoodness.keys(): string_goodness += letterGoodness[letter] return string_goodness print goodness("I eat") 

Using the test case you provided:

 print goodness("I eat") 

outputs the result:

 .369 

One note - creating a dictionary, as done here, is required for Python 2.7+. The same thing can be done in Python 2.6+ with the dict() constructor.

+1
source

All Articles