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

问题描述 投票:0回答:1

我不知道是否有办法只找到列表中第一个元组的元组内部?

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

output:
a
b
c
d

这是我当前的循环样子:

for x in list[0]:

编辑:已编辑完整问题。

python loops for-loop pycharm a-star
1个回答
2
投票

输入:

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

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

输出:

a
b
c
d

编辑

也许您可以尝试使用标准库中的next()函数。从元组列表中创建一个迭代器:

iter_list = iter(_list)

然后将其传递给next()函数:

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')
© www.soinside.com 2019 - 2024. All rights reserved.