Python 如何访问嵌套列表中的元组元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14864300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access tuple elements in a nested list
提问by user1882766
I have a list with nested lists, which contain tuples. The list looks like this:
我有一个带有嵌套列表的列表,其中包含元组。该列表如下所示:
428 [(' whether', None), (' mated', None), (' rooster', None), ('', None)]
429 [(' produced', None), (' without', None), (' rooster', None), (' infertile', None), ('', None)]
I would like to be able access the "None" elements of the tuple, per index value. I would like to create a new list with the same index values that would look like:
我希望能够根据索引值访问元组的“无”元素。我想创建一个具有相同索引值的新列表,如下所示:
428 [(None, None, None, None)]
429 [(None, None, None, None, None)]
I don't really care what type the "None" is in. I just want them as a separate list.
我真的不在乎“无”是什么类型。我只是希望它们作为一个单独的列表。
I've tried list comprehensions, but I only can retrieve the tuples themselves, not the elements inside.
我试过列表推导式,但我只能检索元组本身,而不是内部的元素。
Any help would be appreciated.
任何帮助,将不胜感激。
采纳答案by Roy Nieterau
Simplest for just a single list containing tuples would be:
仅包含元组的单个列表最简单的是:
[x[1] for x in myList]
# [None, None, None, None]
Or if it's always the last value in the tuple (if it contains more than two values):
或者,如果它始终是元组中的最后一个值(如果它包含两个以上的值):
[x[-1] for x in myList]
# [None, None, None, None]
Note that these examples below are using nested lists. It's a list of lists that contain tuples. I figured that's what you were looking for since you were showing two variations of the lists.
请注意,下面的这些示例使用嵌套列表。它是包含元组的列表列表。我认为这就是您要查找的内容,因为您正在展示列表的两种变体。
Use a nested comprehension list:
使用嵌套的理解列表:
myList =[ [(' whether', None), (' mated', None), (' rooster', None), ('', None)] ,
[(' produced', None), (' without', None), (' rooster', None), (' infertile', None), ('', None)] ]
print [[x[1] for x in el] for el in myList]
# [[None, None, None, None], [None, None, None, None, None]]
Or some other variations:
或其他一些变体:
myList =[ [(None, None), (' mated', None), (' rooster', None), ('', None)] ,
[(' produced', None), (' without', None), (' rooster', None), (' infertile', None), ('', None)] ]
# If there are multiple none values (if the tuple isn't always just two values)
print [ [ [ x for x in z if x == None] for z in el ] for el in myList ]
# [[[None, None], [None], [None], [None]], [[None], [None], [None], [None], [None]]]
# If it's always the last value in the tuple
print [[x[-1] for x in el] for el in myList]
# [[None, None, None, None], [None, None, None, None, None]]
Also see: SO: Understanding nested list comprehension
另请参阅: SO:理解嵌套列表理解
回答by óscar López
You can address the elements inside a tuple in the same way as you would with the elements of a list: using indexes. For example:
您可以使用与处理列表元素相同的方式来处理元组中的元素:使用索引。例如:
lst = [1, (2, 3)]
lst[1][1] # first index accesses tuple, second index element inside tuple
=> 3
回答by TheDude
If you just want to get Noneif it exists in the tuple:
如果您只想获取None它是否存在于元组中:
tuple([None for t in list if None in t])
tuple([None for t in list if None in t])
This would recreate a tuple containing Nonefor each tuple it is in. Note that this would not be a good solution if you wanted the # of None's in total.
这将重新创建一个包含None它所在的每个元组的元组。请注意,如果您None总共想要 # of ,这将不是一个好的解决方案。
回答by abarnert
The thing you showed isn't actually a list, and it's hard to guess what the actual list might be. But I'm going to assume it's something like this:
你展示的东西实际上不是一个列表,很难猜测实际的列表可能是什么。但我会假设它是这样的:
list_o_lists = [428,
[(' whether', None), (' mated', None), (' rooster', None), ('', None)],
429,
[(' produced', None),
(' without', None),
(' rooster', None),
(' infertile', None),
('', None)]]
Any list comprehension to access the tuples within that is already going to be pretty horrible:
任何访问其中元组的列表理解都已经非常可怕了:
[[tup for tup in lst] if is_sequence(lst) else lst for lst in list_o_lists]
But modifying it to access the second element of each tuple is trivial:
但是修改它以访问每个元组的第二个元素是微不足道的:
[[tup[1] for tup in lst] if is_sequence(lst) else lst for lst in list_o_lists]
Really, no matter what your list comprehension is, no matter how horrible it is, based on your question, somewhere you've got each tuple as an expression, which means all you have to do is put a [1]on that expression.
真的,不管你的列表理解是什么,不管它有多可怕,根据你的问题,你在某个地方将每个元组作为一个表达式,这意味着你所要做的就是[1]在该表达式上加上一个。
From your comment:
从你的评论:
Sorry, the numbers are the index values.
抱歉,数字是索引值。
I thinkyou actually have something simpler:
我认为你实际上有一些更简单的东西:
list_o_lists = [
[(' whether', None), (' mated', None), (' rooster', None), ('', None)],
[(' produced', None),
(' without', None),
(' rooster', None),
(' infertile', None),
('', None)]]
And then your attempted list comprehension was probably something like this:
然后你尝试的列表理解可能是这样的:
[[tup for tup in lst] for lst in list_o_lists]
Of course this is just a guess, because you still haven't shown us your actual list, or the list comprehension you tried. But as I said above: "… no matter what your list comprehension is…?somewhere you've got each tuple as an expression, which means all you have to do is put a [1]on that expression."
当然,这只是一个猜测,因为您还没有向我们展示您的实际列表,或者您尝试过的列表理解。但正如我上面所说的:“……不管你的列表推导式是什么……?在某个地方你已经把每个元组作为一个表达式,这意味着你所要做的就是[1]在那个表达式上加上一个。”
So, this one is just as easy to change as the one above:
所以,这个和上面的一样容易改变:
[[tup[1] for tup in lst] for lst in list_o_lists]
And if it's not what you actually have, then what you actually have will alsobe just as easy to change. But you'll have to do it yourself, because we have all failed in our repeated attempts to read your mind, while you have your actual code in front of you.
如果它不是您实际拥有的,那么您实际拥有的也将同样容易改变。但是你必须自己做,因为我们在反复尝试读懂你的想法时都失败了,而你的实际代码就摆在你面前。
回答by jcr
use zip with the * to expand the list into args:
使用带 * 的 zip 将列表扩展为 args:
x = [(' whether', None), (' mated', None), (' rooster', None), ('', None)]
stuff, nones = zip(*x)
# prints: stuff
#(' whether', ' mated', ' rooster', '')
print nones
# prints: (None, None, None, None)
zip works by taking a bunch of lists and making the first element of every argument into a list, and the second argument into a list ect
zip 的工作原理是获取一堆列表并将每个参数的第一个元素放入一个列表中,将第二个参数放入一个列表中
the asterisks (*) expands the list so you can imagine zip(*x) as zip(x[0],x[1], x[2],...x[n])
星号 (*) 扩展了列表,因此您可以将 zip(*x) 想象为 zip(x[0],x[1], x[2],...x[n])
回答by eyquem
I suppose that 428 and 429 are indexes not present in the list.
我想 428 和 429 是列表中不存在的索引。
So, given the question
所以,鉴于问题
li = [[(' whether', None), (' mated', None),
(' rooster', None), ('', None)],
[(' produced', None), (' without', None),
(' rooster', None), (' infertile', None),
('', None)]
]
print [ [len(subli)*(None,)] for subli in li]
will do the job.
会做的工作。
[[(None, None, None, None)], [(None, None, None, None, None)]]
Your question is strange. What will be the use of such data ?
你的问题很奇怪。这些数据有什么用?

