簡體   English   中英

python:元組搜索列表

[英]python: list of tuples search

在這里需要一些幫助

num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]

如果找到最后 6 位數字,則返回第一位數字。

例如如果找到 10,11,29,30,36,47 返回 2

您可以將next與條件生成器表達式一起使用:

num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
search = 11

next(first for first, *rest in num if search in rest)
# 2

您可以使用next類似用戶的方法:

num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
to_find = [10,11,29,30,36,47]

print(next(n for n, *nums in num if nums == to_find))

2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM