使用字典计算字符串中的第一个字母 - Python
Using Dictionaries to Count First Letter in a String - Python
我一直在 Python 进行字典练习,我对语言和编程本身还很陌生。我一直在尝试获取一个字符串或字符串列表,让我的代码比较字符串的第一个字母,并根据有多少字符串以字母表中的某个字母开头来制作字典。这是我目前所拥有的:
d = {}
text=["time","after","time"]
# count occurances of character
for w in text:
d[w] = text.count(w)
# print the result
for k in sorted(d):
print (k + ': ' + str(d[k]))
我的目标是获得以下结果:
count_starts(["time","after","time"]) -->{'t': 2, 'a': 1}
但是,我得到的更像是以下内容:
count_starts(["time","after","time"]) --> {time:2, after:1}
利用我所拥有的,我已经能够计算整个唯一字符串出现的次数,而不仅仅是计算字符串中的第一个字母。
我还尝试了以下方法:
d = {}
text=["time","after","time"]
# count occurances of character
for w in text:
for l in w[:1]:
d[l] = text.count(l)
# print the result
for k in sorted(d):
print (k + ': ' + str(d[k]))
但是在打印输出中给我的是:
{"a":0,"t":0}
我正在使用 Python Visualizer 进行测试。
计算文本中每一项首字母出现的次数:
from collections import Counter
text = ["time", "after", "time"]
>>> Counter(t[0] for t in text)
Counter({'a': 1, 't': 2})
或者只是获取字典 key/value 对:
>>> dict(Counter(t[0] for t in text))
{'a': 1, 't': 2}
d = {}
text = ['time', 'after', 'time']
for w in text:
if w: # If we have the empty string. w[0] Does not Exist (DNE)
if w[0] in d: # Check to see if we have first character in dictionary.
d[w[0]] = d[w[0]] + 1 # Use the first character as key to dictionary.
else: # If character has not been found start counting.
d[w[0]] = 1 # Use the first character as key to dictionary.
使用 Python 的 IDLE 我得到:
>>> d = {}
>>> text = ['time', 'after', 'time']
>>> for w in text:
if w:
if w[0] in d:
d[w[0]] = d[w[0]] + 1
else:
d[w[0]] = 1
>>> print d
{'a': 1, 't': 2}
>>>
我一直在 Python 进行字典练习,我对语言和编程本身还很陌生。我一直在尝试获取一个字符串或字符串列表,让我的代码比较字符串的第一个字母,并根据有多少字符串以字母表中的某个字母开头来制作字典。这是我目前所拥有的:
d = {}
text=["time","after","time"]
# count occurances of character
for w in text:
d[w] = text.count(w)
# print the result
for k in sorted(d):
print (k + ': ' + str(d[k]))
我的目标是获得以下结果:
count_starts(["time","after","time"]) -->{'t': 2, 'a': 1}
但是,我得到的更像是以下内容:
count_starts(["time","after","time"]) --> {time:2, after:1}
利用我所拥有的,我已经能够计算整个唯一字符串出现的次数,而不仅仅是计算字符串中的第一个字母。
我还尝试了以下方法:
d = {}
text=["time","after","time"]
# count occurances of character
for w in text:
for l in w[:1]:
d[l] = text.count(l)
# print the result
for k in sorted(d):
print (k + ': ' + str(d[k]))
但是在打印输出中给我的是:
{"a":0,"t":0}
我正在使用 Python Visualizer 进行测试。
计算文本中每一项首字母出现的次数:
from collections import Counter
text = ["time", "after", "time"]
>>> Counter(t[0] for t in text)
Counter({'a': 1, 't': 2})
或者只是获取字典 key/value 对:
>>> dict(Counter(t[0] for t in text))
{'a': 1, 't': 2}
d = {}
text = ['time', 'after', 'time']
for w in text:
if w: # If we have the empty string. w[0] Does not Exist (DNE)
if w[0] in d: # Check to see if we have first character in dictionary.
d[w[0]] = d[w[0]] + 1 # Use the first character as key to dictionary.
else: # If character has not been found start counting.
d[w[0]] = 1 # Use the first character as key to dictionary.
使用 Python 的 IDLE 我得到:
>>> d = {}
>>> text = ['time', 'after', 'time']
>>> for w in text:
if w:
if w[0] in d:
d[w[0]] = d[w[0]] + 1
else:
d[w[0]] = 1
>>> print d
{'a': 1, 't': 2}
>>>