Python 嵌套列表和迭代 [英] Python Nested lists and iteration
问题描述
我是 Python 新手,我进行了搜索,但找不到我要找的东西.如果有人问过这个问题,并且由于我不知道我想要达到的目标的名称而找不到它,我提前道歉.我很乐意阅读您可能建议的任何文件.
我有一个列表列表.例如=> [int, 'str']
t = [[0234, 'str_0'],[1267,'str_1'],[2445,'str_2']]
我想知道 a str
是否存在于 list t 列表之一的 index(1) 位置.我可以用一个包含 2 个 for 或 while 循环的函数来做到这一点,但我正在寻求的是,如果可能的话,使用一次迭代来实现这一点.我想学最短的函数.
对于输入 str('str_3'),我想得到 int(2)
(在它自己的第一个索引位置上有 str_3 的列表的索引)对于 str_1,我想得到 int(0)
对于 str_1234 我想得到 False,因为它不在 list t
中的任何列表中强>
作为新手,我通常会这样做:
for n in range(len(t)):如果 t[n][1] == 'str_1'返回 n返回错误
如果可能的话,我想要得到的是一种在一行代码中实现这一目标的更好、更短的方法,或者只是简单地了解是否有一种更聪明、更好或更 Pythonic 的方法可以让你们中的任何人肯定会推荐更有经验的人.
谢谢
[n for n, (i, s) in enumerate(t) if s == 'str_3']
说明:
<预><代码>>>>t = [[100, 'str_1'], [200, 'str_2'], [300, 'str_3']]# 使用 enumerate 获取每个列表项及其索引.>>>列表(枚举(t))[(0, [100, 'str_1']), (1, [200, 'str_2']), (2, [300, 'str_3'])]# 使用列表理解语法迭代枚举.>>>[n for n, (i, s) in enumerate(t)][0, 1, 2]# 可以在列表推导式中添加 if 条件.>>>[n for n, (i, s) in enumerate(t) if s == 'str_3'][2]>>>[n for n, (i, s) in enumerate(t) if s == 'str_1234'][]这将返回所有匹配的索引,因为可能有多个.
如果您知道只有一个索引,我可以建议使用字典而不是嵌套列表吗?使用字典,您可以使用非常简单的语法在恒定时间内查找元素,而不必进行迭代.
<预><代码>>>>t = {'str_1':100,'str_2':200,'str_3':300}>>>'str_3' 在 t真的>>>t['str_3']300>>>'str_1234' 在 t错误的>>>t['str_1234']回溯(最近一次调用最后一次):文件<stdin>",第 1 行,位于 <module>密钥错误:'str_1234'I am new to Python and I did my search but I could not find what I am looking for. I apologise in advance if this question has been asked and if I could not find it due to my lack of not knowing the name of what I am trying to achieve. I will gladly read any document you might suggest.
I have a list of lists. e.g. => [int, 'str']
t = [[0234, 'str_0'],
[1267, 'str_1'],
[2445, 'str_2']]
I want to find out if a str
exists in index(1) position of one of the lists of list t.
I can do this with a function containing 2 for or while loops but what I am seeking is to achieve this, if possible, using one single iteration. I want to learn the shortest function.
for input str('str_3'), I want to get int(2)
(index of the list which has str_3 on its own 1st index location)
for str_1, I want to get int(0)
and for str_1234 I want to get False as it is not in any of the lists within the list t
As a newbie, I would normally do:
for n in range(len(t)):
if t[n][1] == 'str_1'
return n
return False
What I am seeking to get is, if possible, a better and shorter way of achieving this in one line of a code or just simply to learn if there is a smarter, better or more pythonic way that any one of you who is surely more experienced would recommend.
Thank you
[n for n, (i, s) in enumerate(t) if s == 'str_3']
Explanation:
>>> t = [[100, 'str_1'], [200, 'str_2'], [300, 'str_3']]
# Use enumerate to get each list item along with its index.
>>> list(enumerate(t))
[(0, [100, 'str_1']), (1, [200, 'str_2']), (2, [300, 'str_3'])]
# Use list comprehension syntax to iterate over the enumeration.
>>> [n for n, (i, s) in enumerate(t)]
[0, 1, 2]
# An if condition can be added right inside the list comprehension.
>>> [n for n, (i, s) in enumerate(t) if s == 'str_3']
[2]
>>> [n for n, (i, s) in enumerate(t) if s == 'str_1234']
[]
This will return all of the matching indices, since there could be more than one.
If you know there will only be one index, may I suggest using a dict instead of a nested list? With a dict you can lookup elements in constant time using very straightforward syntax, rather than having to iterate.
>>> t = {'str_1': 100, 'str_2': 200, 'str_3': 300}
>>> 'str_3' in t
True
>>> t['str_3']
300
>>> 'str_1234' in t
False
>>> t['str_1234']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'str_1234'
这篇关于Python 嵌套列表和迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!