我试图找到列表中每个索引的最大出现次数。例如,如果索引0中的出现次数有5次“find”,则记录5次,如果索引1有2次“find”,则记录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'}]},
{},
{}]
aa = []
for index, l in enumerate(a):
count = 0
find = []
for trying in l.get("test", []):
if trying["testing"] == "find":
count += 1
print(count)
我试着用建议的方法,但没有用。你知道吗
我的电流输出:
1
1
2
3
预期产量
1
3
只需向后缩进printing指令,这样它就只能在嵌套循环之后执行,而不是每次出现时都执行。你知道吗
您正在打印内部循环,您正在增加计数。你需要在外面打印。你知道吗
或一个班轮:
输出:
或两行(可读性更强):
输出:
相关问题 更多 >
编程相关推荐