![](/img/trans.png)
[英]How can I iterate the first element of each tuple in a list of tuples through a class method?
[英]how to iterate ONLY the first tuple in a list of tuples?
我不知道是否有办法找到列表中只有第一个元组的元组内部?
list = [(a, b, c, d), (d, e, f, g), (h, i, j, k)]
output:
a
b
c
d
这是我当前循环的样子:
for x in list[0]:
编辑:编辑完整的问题。
输入:
_list = [('a', 'b', 'c', 'd'), ('d', 'e', 'f', 'g'), ('h', 'i', 'j', 'k')]
for i in _list[0]:
print(i)
Output:
a
b
c
d
编辑:
也许你可以试试标准库中的 next() function。 从您的元组列表中创建一个迭代器:
iter_list = iter(_list)
然后将其传递给 next() function:
In: next(iter_list)
Out: ('a', 'b', 'c', 'd')
In: next(iter_list)
Out: ('d', 'e', 'f', 'g')
In: next(iter_list)
Out: ('h', 'i', 'j', 'k')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.