仅获取 python 中嵌套列表中的下一个列表
Get just the very next list within a nested list in python
如何在 python 的嵌套列表中获取下一个列表?
我有几个列表:
charLimit = [101100,114502,124602]
conditionalNextQ = [101101, 101200, 114503, 114504, 124603, 124604]`
response = [[100100,4]
,[100300,99]
,[1100500,6]
,[1100501,04]
,[100700,12]
,[100800,67]
,[100100,64]
,[100300,26]
,[100500,2]
,[100501,035]
,[100700,9]
,[100800,8]
,[101100,"hello"]
,[101101,"twenty"] ... ]
for question in charLimit:
for limitQuestion in response:
limitNumber = limitQuestion[0]
if question == limitNumber:
print(limitQuestion)
上面的代码正在执行我想要的操作,即在 response
中包含 charlimit
中的数字之一时打印列表实例。但是,我还希望它在 response
中打印下一个值。
例如 response
中的倒数第二个值包含 101100
(charlimit
中的值)所以我希望它不仅打印
101100,"hello"
(正如代码目前所做的那样)
但下一个列表也是(而且只有下一个)
101100,"hello"
101101,"twenty"
在此先感谢您的帮助。请注意,response
是一个非常长的列表,因此我希望尽可能使事情变得相当高效,尽管它在这项工作的上下文中并不重要。我可能遗漏了一些非常简单的东西,但找不到任何人在不使用非常小的列表中的特定索引的情况下这样做的例子。
您可以使用enumerate
例如:
charLimit = [101100,114502,124602]
conditionalNextQ = [101101, 101200, 114503, 114504, 124603, 124604]
response = [[100100,4]
,[100300,99]
,[1100500,6]
,[1100501,04]
,[100700,12]
,[100800,67]
,[100100,64]
,[100300,26]
,[100500,2]
,[100501,035]
,[100700,9]
,[100800,8]
,[101100,"hello"]
,[101101,"twenty"]]
l = len(response) - 1
for question in charLimit:
for i, limitQuestion in enumerate(response):
limitNumber = limitQuestion[0]
if question == limitNumber:
print(limitQuestion)
if (i+1) <= l:
print(response[i+1])
输出:
[101100, 'hello']
[101101, 'twenty']
我会消除 charLimit
上的循环并改为 response
上的循环。在这个循环中使用 enumerate
允许我们通过索引访问下一个元素,在我们想要打印它的情况下:
for i, limitQuestion in enumerate(response, 1):
limitNumber = limitQuestion[0]
# use the `in` operator to check if `limitNumber` equals any
# of the numbers in `charLimit`
if limitNumber in charLimit:
print(limitQuestion)
# if this isn't the last element in the list, also
# print the next one
if i < len(response):
print(response[i])
如果 charLimit
很长,您应该考虑将其定义为 set
,因为集合的成员资格测试比列表快:
charLimit = {101100,114502,124602}
如何在 python 的嵌套列表中获取下一个列表?
我有几个列表:
charLimit = [101100,114502,124602]
conditionalNextQ = [101101, 101200, 114503, 114504, 124603, 124604]`
response = [[100100,4]
,[100300,99]
,[1100500,6]
,[1100501,04]
,[100700,12]
,[100800,67]
,[100100,64]
,[100300,26]
,[100500,2]
,[100501,035]
,[100700,9]
,[100800,8]
,[101100,"hello"]
,[101101,"twenty"] ... ]
for question in charLimit:
for limitQuestion in response:
limitNumber = limitQuestion[0]
if question == limitNumber:
print(limitQuestion)
上面的代码正在执行我想要的操作,即在 response
中包含 charlimit
中的数字之一时打印列表实例。但是,我还希望它在 response
中打印下一个值。
例如 response
中的倒数第二个值包含 101100
(charlimit
中的值)所以我希望它不仅打印
101100,"hello"
(正如代码目前所做的那样)
但下一个列表也是(而且只有下一个)
101100,"hello"
101101,"twenty"
在此先感谢您的帮助。请注意,response
是一个非常长的列表,因此我希望尽可能使事情变得相当高效,尽管它在这项工作的上下文中并不重要。我可能遗漏了一些非常简单的东西,但找不到任何人在不使用非常小的列表中的特定索引的情况下这样做的例子。
您可以使用enumerate
例如:
charLimit = [101100,114502,124602]
conditionalNextQ = [101101, 101200, 114503, 114504, 124603, 124604]
response = [[100100,4]
,[100300,99]
,[1100500,6]
,[1100501,04]
,[100700,12]
,[100800,67]
,[100100,64]
,[100300,26]
,[100500,2]
,[100501,035]
,[100700,9]
,[100800,8]
,[101100,"hello"]
,[101101,"twenty"]]
l = len(response) - 1
for question in charLimit:
for i, limitQuestion in enumerate(response):
limitNumber = limitQuestion[0]
if question == limitNumber:
print(limitQuestion)
if (i+1) <= l:
print(response[i+1])
输出:
[101100, 'hello']
[101101, 'twenty']
我会消除 charLimit
上的循环并改为 response
上的循环。在这个循环中使用 enumerate
允许我们通过索引访问下一个元素,在我们想要打印它的情况下:
for i, limitQuestion in enumerate(response, 1):
limitNumber = limitQuestion[0]
# use the `in` operator to check if `limitNumber` equals any
# of the numbers in `charLimit`
if limitNumber in charLimit:
print(limitQuestion)
# if this isn't the last element in the list, also
# print the next one
if i < len(response):
print(response[i])
如果 charLimit
很长,您应该考虑将其定义为 set
,因为集合的成员资格测试比列表快:
charLimit = {101100,114502,124602}