Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中计算列表中的名称数_Python - Fatal编程技术网

在python中计算列表中的名称数

在python中计算列表中的名称数,python,Python,我有一张名单,上面有名字: names = ['test','hallo','test'] uniquenames = ['test','hallo'] 通过set,我得到uniquenames,因此唯一的名称在不同的列表中 但是现在我想数一数每个so测试有多少个名字:2 hallo:1 我有这个: for i in range(len(uniquenames)): countname = name.count[i] 但它给了我一个错误: TypeError:“内置函数”或“方法”对

我有一张名单,上面有名字:

names = ['test','hallo','test']
uniquenames = ['test','hallo']
通过set,我得到uniquenames,因此唯一的名称在不同的列表中

但是现在我想数一数每个so测试有多少个名字:2 hallo:1

我有这个:

for i in range(len(uniquenames)):
    countname = name.count[i]
但它给了我一个错误: TypeError:“内置函数”或“方法”对象不可下标


如何解决此问题?

使用
集合中的
计数器

>>> from collections import Counter
>>> Counter(names)
Counter({'test': 2, 'hallo': 1})

此外,为了使示例有效,您应该更改
名称。count[i]
用于
名称。count(i)
因为
count
是一个函数。

使用
集合中的
计数器

>>> from collections import Counter
>>> Counter(names)
Counter({'test': 2, 'hallo': 1})

此外,为了使示例有效,您应该更改
名称。count[i]
用于
名称。count(i)
因为
count
是一个函数。

您可以使用字典:

names = ['test','hallo','test']
countnames = {}
for name in names:
    if name in countnames:
        countnames[name] += 1
    else:
        countnames[name] = 1

print(countnames) # => {'test': 2, 'hallo': 1}
如果要使其不区分大小写,请使用以下命令:

names = ['test','hallo','test', 'HaLLo', 'tESt']
countnames = {}
for name in names:
    name = name.lower() # => to make 'test' and 'Test' and 'TeST'...etc the same
    if name in countnames:
        countnames[name] += 1
    else:
        countnames[name] = 1

print(countnames) # => {'test': 3, 'hallo': 2}
如果要将键作为计数,请使用数组将名称存储在:

names = ['test','hallo','test','name', 'HaLLo', 'tESt','name', 'Hi', 'hi', 'Name', 'once']
temp = {}
for name in names:
    name = name.lower()
    if name in temp:
        temp[name] += 1
    else:
        temp[name] = 1
countnames = {}
for key, value in temp.items():
    if value in countnames:
        countnames[value].append(key)
    else:
        countnames[value] = [key]
print(countnames) # => {3: ['test', 'name'], 2: ['hallo', 'hi'], 1: ['once']}

你可以使用字典:

names = ['test','hallo','test']
countnames = {}
for name in names:
    if name in countnames:
        countnames[name] += 1
    else:
        countnames[name] = 1

print(countnames) # => {'test': 2, 'hallo': 1}
如果要使其不区分大小写,请使用以下命令:

names = ['test','hallo','test', 'HaLLo', 'tESt']
countnames = {}
for name in names:
    name = name.lower() # => to make 'test' and 'Test' and 'TeST'...etc the same
    if name in countnames:
        countnames[name] += 1
    else:
        countnames[name] = 1

print(countnames) # => {'test': 3, 'hallo': 2}
如果要将键作为计数,请使用数组将名称存储在:

names = ['test','hallo','test','name', 'HaLLo', 'tESt','name', 'Hi', 'hi', 'Name', 'once']
temp = {}
for name in names:
    name = name.lower()
    if name in temp:
        temp[name] += 1
    else:
        temp[name] = 1
countnames = {}
for key, value in temp.items():
    if value in countnames:
        countnames[value].append(key)
    else:
        countnames[value] = [key]
print(countnames) # => {3: ['test', 'name'], 2: ['hallo', 'hi'], 1: ['once']}

谢谢一个问题,我如何旋转这个?例如,3变为键,名称变为字典中的索引?@klaashansen您不能这样做,因为键必须不同,因此如果两个名称具有相同的计数,则只会注册最后一个名称,但是,数组有一个解决方法,查看更新的答案。谢谢标记为answer@klaashansen很高兴为您提供帮助:)。谢谢您提出一个问题:如何旋转此文件?例如,3变为键,名称变为字典中的索引?@klaashansen您不能这样做,因为键必须不同,因此如果两个名称具有相同的计数,则只会注册最后一个名称,但是,对于数组有一种解决方法,请参阅更新的答案。谢谢标记为answer@klaashansen很乐意帮忙:)。