存取清单清单中的项目 [英] Access item in a list of lists
本文介绍了存取清单清单中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个列表列表,并且只想操纵该列表中的单个项目,那我该怎么做呢?
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?
例如:
List1 = [[10,13,17],[3,5,1],[13,11,12]]
如果我想取一个值(例如50)并仅查看List1
中的第一个子列表,然后减去10(第一个值),然后再加上13,然后减去17,该怎么办?
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?
推荐答案
您可以通过以下方式访问列表列表中的元素:首先指定您要关注的列表,然后指定所需列表的元素.例如,17
是列表0
中的元素2
,它是list1[0][2]
:
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
因此,您的示例将是
50 - list1[0][0] + list1[0][1] - list1[0][2]
这篇关于存取清单清单中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文