简体   繁体   English

如何仅迭代元组列表中的第一个元组?

[英]how to iterate ONLY the first tuple in a list of tuples?

I don't know if there is a way to find the inside of a tuple of ONLY the first tuple in a list?我不知道是否有办法找到列表中只有第一个元组的元组内部?

list = [(a, b, c, d), (d, e, f, g), (h, i, j, k)]

output:
a
b
c
d

This is what my current loop looks like:这是我当前循环的样子:

for x in list[0]:

EDIT: edited full question.编辑:编辑完整的问题。

Input:输入:

_list = [('a', 'b', 'c', 'd'), ('d', 'e', 'f', 'g'), ('h', 'i', 'j', 'k')]

for i in _list[0]:
    print(i)

Output: Output:

a
b
c
d

EDIT :编辑

Maybe you could try the next() function in the standard library.也许你可以试试标准库中的 next() function。 Create an iterator from your list of tuples:从您的元组列表中创建一个迭代器:

iter_list = iter(_list)

Then just pass it to the next() function:然后将其传递给 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.

相关问题 如何通过 class 方法迭代元组列表中每个元组的第一个元素? - How can I iterate the first element of each tuple in a list of tuples through a class method? 通过仅取元组的第一个值来重新构造元组列表时出现问题吗? - Problems while reformating a list of tuples, by taking only the first value of the tuple? 迭代元组列表并仅解压第一个元素 - Iterate over list of tuples and unpack first elements only 在列表中迭代元组并为元组添加权重 - Iterate tuples in list and add weight to the tuple 如果第一个元组元素匹配,如何合并列表中的两个元组? - How to merge two tuples in a list if the first tuple elements match? 如何基于第一个值Python替换元组排序列表中的元组 - How to replace a tuple in sorted list of tuples basing on first value Python 如何对元组列表进行排序,但一个特定的元组是第一个? - How to sort a list of tuples but with one specific tuple being the first? 仅使用元组的前两个部分将3元组与3元组列表进行比较 - Comparing a 3-tuple to a list of 3-tuples using only the first two parts of the tuple Python list_of_tuples:每个元组的第二个val,仅当元组的第一个val == - Python list_of_tuples: sum second val of each tuple, only if first val of tuple == something 将列表与元组列表中元组的第一项合并 - Merge list with first item of tuple that is in a list of tuples
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM