gpt4 book ai didi

Python 3.6 - 在条件语句后从两个列表中读取行的值

转载 作者:太空宇宙 更新时间:2023-11-03 13:10:02 26 4
gpt4 key购买 nike

您好,这是我的代码,用于查找列表 a 中重复 3 次或更多次的值:

a = ['1','1','1','2','2','3']
b = ['4','5','1','7','8','4']

d = [item for item in a if a.count(item) >= 3]

print(d)
# ['1', '1', '1']

所以我的问题是我怎样才能同时读取列表 b 中的相应值。此外,列表 a 和 b 的大小始终相同。我想要的输出应该是:

output: [['1', '1', '1'], ['4', '5', '1']]

谢谢!

最佳答案

您可以使用 zip 解决此问题:

>>> list(zip(*[(ai, bi) for ai, bi in zip(a, b) if a.count(ai) >= 3]))
[('1', '1', '1'), ('4', '5', '1')]

关于Python 3.6 - 在条件语句后从两个列表中读取行的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45693098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com