我写了这个函数:
def make_upper(words):
for word in words:
ind = words.index(word)
words[ind] = word.upper()
但是我需要使用不熟悉的lambda函数来编写它。有什么想法吗?
我还写了一个函数来计算每个字母出现的频率:
def letter_cnt(word,freq):
for let in word:
if let == 'A': freq[0]+=1
elif let == 'B': freq[1]+=1
elif let == 'C': freq[2]+=1
elif let == 'D': freq[3]+=1
elif let == 'E': freq[4]+=1
所以我需要用字典来编写它。有什么建议吗?
答案 0 :(得分:0)
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> words = ['werfwr', 'fwerw', 'ghebe', 'wgergwtg', 'gbeyteb']
>>> def make_upper(words):
... return map(lambda word: word.upper(), words)
...
>>> make_upper(words)
['WERFWR', 'FWERW', 'GHEBE', 'WGERGWTG', 'GBEYTEB']
>>> def letter_cnt(word):
... letters = {}
... for symbol in word:
... symbol = symbol.lower()
... if 'a' <= symbol and symbol <= 'z':
... if symbol in letters:
... letters[symbol] += 1
... else:
... letters[symbol] = 1
... return letters
...
>>> letter_cnt('werfervt')
{'e': 2, 'f': 1, 'r': 2, 't': 1, 'w': 1, 'v': 1}