访问列表列表中的项目 [英] Access item in a list of lists
问题描述
如果我有一个列表列表并且只想操作该列表中的单个项目,我该怎么做?
例如:
List1 = [[10,13,17],[3,5,1],[13,11,12]]
如果我想取一个值(比如 50)并只查看 List1
中的第一个子列表,然后减去 10(第一个值),然后加上 13,然后减去 17,该怎么办?
您可以通过首先指定您感兴趣的列表,然后指定您想要该列表的哪个元素来访问列表列表中的元素.例如,17
是列表0
中的元素2
,即list1[0][2]
:
所以,你的例子是
50 - list1[0][0] + list1[0][1] - list1[0][2]
If I have a list of lists and just want to manipulate an individual item in that list, how would I go about doing that?
For example:
List1 = [[10,13,17],[3,5,1],[13,11,12]]
What if I want to take a value (say 50) and look just at the first sublist in List1
, and subtract 10 (the first value), then add 13, then subtract 17?
You can access the elements in a list-of-lists by first specifying which list you're interested in and then specifying which element of that list you want. For example, 17
is element 2
in list 0
, which is list1[0][2]
:
>>> list1 = [[10,13,17],[3,5,1],[13,11,12]]
>>> list1[0][2]
17
So, your example would be
50 - list1[0][0] + list1[0][1] - list1[0][2]
这篇关于访问列表列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!