Python 在列表中查找引用

Python 在列表中查找引用,python,Python,我试图找到列表中每个索引的最大出现次数。例如,如果索引0中的事件有5次“查找”,则记录5次,如果索引1有2次“查找”,则记录2次。因此,对于索引0和索引1,都将得到5,2 a = [{'test': []}, {'test': [{'testing': 'Not', 'Nottesting': 'Yes'}]}, {'test': [{'testing': 'find', 'Nottesting': 'yes'}]}, {'test': [{'testing':

我试图找到列表中每个索引的最大出现次数。例如,如果索引0中的事件有5次“查找”,则记录5次,如果索引1有2次“查找”,则记录2次。因此,对于索引0和索引1,都将得到5,2

a = [{'test': []}, 
     {'test': [{'testing': 'Not', 'Nottesting': 'Yes'}]},
     {'test': [{'testing': 'find', 'Nottesting': 'yes'}]},
     {'test': [{'testing': 'maybe', 'Nottesting': 'yes'},
               {'testing': 'find', 'Nottesting': 'maybe'},
               {'testing': 'find', 'Nottesting': 'haha'},
               {'testing': 'find', 'Nottesting': 'sometimes'},
               {'testing': 'sowell', 'Nottesting': 'some'}]},
     {},
     {}]
有没有办法根据每个列表的最大值打印计数? 我试着用建议的方法,但没有用

我的当前输出:

1
1
2
3
预期产量

1
3

只需将printing指令向后缩进,这样它就只能在嵌套循环之后执行,而不是在每次出现时执行

for index, l in enumerate(a):
    count = 0
    find = []

    for trying in l.get("test", []):
        if trying["testing"] == "find":
            count += 1

    if count : print(count)

只需将printing指令向后缩进,这样它就只能在嵌套循环之后执行,而不是在每次出现时执行

for index, l in enumerate(a):
    count = 0
    find = []

    for trying in l.get("test", []):
        if trying["testing"] == "find":
            count += 1

    if count : print(count)

您正在增加计数的内部循环中打印。你需要在外面打印

a = [{'test': []}, {'test': [{'testing': 'Not', 'Nottesting': 'Yes'}]}, {'test': [{'testing': 'find', 'Nottesting': 'yes'}]}, {'test': [{'testing': 'maybe', 'Nottesting': 'yes'}, {'testing': 'find', 'Nottesting': 'maybe'}, {'testing': 'find', 'Nottesting':'haha'}, {'testing': 'find', 'Nottesting': 'sometimes'}, {'testing': 'sowell', 'Nottesting': 'some'}]}, {}, {}]

aa = []
for index, l in enumerate(a):
    count = 0
    find = []
    for trying in l.get("test", []):
        if trying["testing"] == "find":
            count += 1
    if count != 0:
        print(count)

您正在增加计数的内部循环中打印。你需要在外面打印

a = [{'test': []}, {'test': [{'testing': 'Not', 'Nottesting': 'Yes'}]}, {'test': [{'testing': 'find', 'Nottesting': 'yes'}]}, {'test': [{'testing': 'maybe', 'Nottesting': 'yes'}, {'testing': 'find', 'Nottesting': 'maybe'}, {'testing': 'find', 'Nottesting':'haha'}, {'testing': 'find', 'Nottesting': 'sometimes'}, {'testing': 'sowell', 'Nottesting': 'some'}]}, {}, {}]

aa = []
for index, l in enumerate(a):
    count = 0
    find = []
    for trying in l.get("test", []):
        if trying["testing"] == "find":
            count += 1
    if count != 0:
        print(count)
或一个班轮:

print([sum([1 for x in i.get('test',[]) if x['testing']=='find']) for i in a if sum([1 for x in i.get('test',[]) if x['testing']=='find'])!=0])
输出:

[1, 3]
[1, 3]
或两行(可读性更强):

输出:

[1, 3]
[1, 3]
或一个班轮:

print([sum([1 for x in i.get('test',[]) if x['testing']=='find']) for i in a if sum([1 for x in i.get('test',[]) if x['testing']=='find'])!=0])
输出:

[1, 3]
[1, 3]
或两行(可读性更强):

输出:

[1, 3]
[1, 3]

我尝试了,但是我得到了以下输出:0 0 1 3 0 0我尝试了许多不同的缩进,但不知道为什么所有缩进都会包含零well@Beginner因为
count
是用值
0
初始化的,所以它将始终打印此值。。。看看我的小改动。我试过了,但是我得到了以下结果:0 0 1 3 0 0我试过许多不同的缩进,不知道为什么所有缩进都会包含零well@Beginner因为
count
是用值
0
初始化的,所以它将始终打印此值。。。看看我的零钱。酷!谢谢你,先生,你能告诉我为什么有零吗?我仍然不明白为什么对于a中的某些索引,find的计数是0,因此有0'sCool!谢谢你,先生,你能告诉我为什么有零吗?对于a中的某些索引,我仍然不明白为什么find的计数是0,因此有0'一些行程序是最好的行程序。@SvenKrüger正确、最短、最好,请注意
for
循环速度慢,列表理解也慢t@Tryph是的,tho,:-),哈哈,我靠看不懂的东西生活哈哈哈,:-)@Tryph Now:-)?一行程序是最好的行程序。@SvenKrüger正确、最短、最好,请注意
for
循环速度慢,列表理解也慢t@Tryph是的,tho,:-),哈哈,我靠看不懂的东西生活哈哈哈,:-)@Tryph Now:-)?