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

标签 python count

您好,这是我的代码,用于查找列表 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/

相关文章:

python - 当我在没有过滤器的情况下在 Dynamodb 中执行 scan() 并仅检索 10 个对象时,它是否仍然访问整个数据库?

python - 获取 django 模型字符串字段作为 str 而不是 unicode

mysql - 使用 group by 子句和 count() 函数时 MySQL 的不一致/奇怪行为

excel - 复制一系列单元格并仅选择包含数据的单元格

python - aiohttp.web + aiopg + sqlalchemy : random "cursor.execute() called while another coroutine is already waiting for incoming data" under load

python - 在python中解析xml内容

MYSQL 品牌按产品数量列出顺序,但是

laravel - Eloquent : How to get a model that has the count of a related model exactly n with a condition?

python - 错误 "resolve() got an unexpected keyword argument ' Replace_conflicting'"是什么意思?

MySQL COUNT 和 IFNULL