gpt4 book ai didi

python - 如何对元组列表执行搜索

转载 作者:太空狗 更新时间:2023-10-30 00:07:27 25 4
gpt4 key购买 nike

我有一个元组列表。例如,我有以下内容:

a=[('jamy', 'k'), ('Park', 'h'), ('kick', 'p'), ('an', 'o'),('an',
'o'),('an', 'o'),('an', 'r'), ('car', 'k'), ('rock', 'h'), ('pig',
'p')]

还有另一个列表,

b = ['k','h','p']

我想从列表 a 的第二个元组元素中找到列表 b 中的模式。

在上面的示例中,输出应该返回,

[('jamy','Park','kick'),('car','rock','pig')]

有人可以帮助我实现我的目标吗?

最佳答案

c = [(a[x][0], a[x+1][0], a[x+2][0])
for x, _ in enumerate(a)
if a[x][1] == b[0] and
a[x+1][1] == b[1] and
a[x+2][1] == b[2]]

关于python - 如何对元组列表执行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19611878/

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