Python- 如何 return 一个计算字符串列表出现次数的字典?
Python- How to return a dictionary that counts occurrences in a list of strings?
我正在尝试创建一个函数来计算字符串列表中第一个字母的出现次数,并将它们 returns 作为字典。
例如:
list=["banana","ball", "cat", "hat"]
dictionary would look like: {b:2, c:1, h:1}
这是我的代码,它迭代但计数不正确。那就是我被困的地方。如何更新要计数的值?
def count_starts(text):
new_list=[]
for word in range(len(text)):
for letter in text[word]:
if letter[0]=='':
new_list.append(None)
else:
new_list.append(letter[0])
new_dict= {x:new_list.count(x) for x in new_list}
return new_dict
此外,如何避免出现以下格式的超出范围错误:
def count_starts(text):
import collections
c=collections.Counter(x[0] for x in text)
return c
此外,如果列表包含“None”作为值,我需要做什么?我需要数 None.
您的代码存在问题,您似乎对单词的所有字母进行了迭代。 letter[0]
是字母的子串(这是一个字符串)。
你必须做得更简单,不需要双循环,取你单词的每个首字母:
for word in text:
if word: # to filter out empty strings
first_letter = word[0]
但是再一次 collections.Counter
使用生成器理解来提取第一个字母是最好的选择和一行(添加条件以过滤掉空字符串):
import collections
c = collections.Counter(x[0] for x in ["banana","ball", "cat", "", "hat"] if x)
c
现在是一个字典:Counter({'b': 2, 'h': 1, 'c': 1})
要插入 None
而不是过滤掉空值的一个变体是:
c = collections.Counter(x[0] if x else None for x in ["banana","ball", "cat", "", "hat"])
您需要计数器:
from collections import Counter
lst = ["banana","ball", "cat", "hat"]
dct = Counter(lst)
现在,dct 存储 lst 中每个元素出现的次数。
dct = {'b': 2, 'h': 1, 'c': 1}
my_list=["banana","ball", "cat", "hat"]
my_dict = dict()
for word in my_list:
try:
my_dict[word[0]] += 1
except KeyError:
my_dict[word[0]] = 1
这会将现有密钥的密钥值增加 1,如果在使用值 1 创建密钥之前没有找到它们
选择:
my_list=["banana","ball", "bubbles", "cat", "hat"]
my_dict = dict()
for word in my_list:
if word[0] in my_dict.keys():
my_dict[word[0]] += 1
else:
my_dict[word[0]] = 1
Also, what do I need to do if the list contains "None" as a value? I
need to count None.
删除None
lst_no_Nones = [x for x in lis if x != None]
计数None
total_None = (sum(x != None for x in lst))
我正在尝试创建一个函数来计算字符串列表中第一个字母的出现次数,并将它们 returns 作为字典。
例如:
list=["banana","ball", "cat", "hat"]
dictionary would look like: {b:2, c:1, h:1}
这是我的代码,它迭代但计数不正确。那就是我被困的地方。如何更新要计数的值?
def count_starts(text):
new_list=[]
for word in range(len(text)):
for letter in text[word]:
if letter[0]=='':
new_list.append(None)
else:
new_list.append(letter[0])
new_dict= {x:new_list.count(x) for x in new_list}
return new_dict
此外,如何避免出现以下格式的超出范围错误:
def count_starts(text):
import collections
c=collections.Counter(x[0] for x in text)
return c
此外,如果列表包含“None”作为值,我需要做什么?我需要数 None.
您的代码存在问题,您似乎对单词的所有字母进行了迭代。 letter[0]
是字母的子串(这是一个字符串)。
你必须做得更简单,不需要双循环,取你单词的每个首字母:
for word in text:
if word: # to filter out empty strings
first_letter = word[0]
但是再一次 collections.Counter
使用生成器理解来提取第一个字母是最好的选择和一行(添加条件以过滤掉空字符串):
import collections
c = collections.Counter(x[0] for x in ["banana","ball", "cat", "", "hat"] if x)
c
现在是一个字典:Counter({'b': 2, 'h': 1, 'c': 1})
要插入 None
而不是过滤掉空值的一个变体是:
c = collections.Counter(x[0] if x else None for x in ["banana","ball", "cat", "", "hat"])
您需要计数器:
from collections import Counter
lst = ["banana","ball", "cat", "hat"]
dct = Counter(lst)
现在,dct 存储 lst 中每个元素出现的次数。
dct = {'b': 2, 'h': 1, 'c': 1}
my_list=["banana","ball", "cat", "hat"]
my_dict = dict()
for word in my_list:
try:
my_dict[word[0]] += 1
except KeyError:
my_dict[word[0]] = 1
这会将现有密钥的密钥值增加 1,如果在使用值 1 创建密钥之前没有找到它们
选择:
my_list=["banana","ball", "bubbles", "cat", "hat"]
my_dict = dict()
for word in my_list:
if word[0] in my_dict.keys():
my_dict[word[0]] += 1
else:
my_dict[word[0]] = 1
Also, what do I need to do if the list contains "None" as a value? I need to count None.
删除None
lst_no_Nones = [x for x in lis if x != None]
计数None
total_None = (sum(x != None for x in lst))