简体   繁体   English

如何在其他列表中查找特定列表

[英]How to find specific list among other lists

I am a beginner in python and have a question. 我是python的初学者,有个问题。 I am using python 3.6.0 How do I find the name of a list which contains specific items, among other lists? 我正在使用python 3.6.0,如何在其他列表中查找包含特定项目的列表的名称?

C1 = ['X', 'O', '1', '1']
C2 = ['X', 'O', '2', '1']
C3 = ['X', 'O', '3', '1']
C4 = ['X', 'O', '4', '1']
C5 = ['X', 'O', '5', '1']
C6 = ['X', 'O', '1', '2']
C7 = ['X', 'O', '2', '2']
C8 = ['X', 'O', '3', '2']
C9 = ['X', 'O', '4', '2']
C10 = ['X', 'O', '5', '2']
C11 = ['X', 'O', '1', '3']
C12 = ['X', 'O', '2', '3']
C13 = ['X', 'O', '3', '3']
C14 = ['X', 'O', '4', '3']
C15 = ['X', 'O', '5', '3']
C16 = ['X', 'O', '1', '4']
C17 = ['X', 'O', '2', '4']
C18 = ['X', 'O', '3', '4']
C19 = ['X', 'O', '4', '4']
C20 = ['X', 'O', '5', '4']
C21 = ['X', 'O', '1', '5']
C22 = ['X', 'O', '2', '5']
C23 = ['X', 'O', '3', '5']
C24 = ['X', 'O', '4', '5']
C25 = ['X', 'O', '5', '5']

Let's say I wanted to find the name of the list which has 5 in the third position and 2 in the 4 position. 假设我想查找列表的名称,该列表的第三位为5,第二位为4。 How and what do I code to get the name of the list? 如何以及如何编码才能获得列表的名称?

Put these lists in a list, and iterate over them using a loop. 将这些列表放在列表中,然后使用循环对其进行迭代。

for listItem in bigList:
    if listItem[2] == '5' and listItem[3] == '2'
         # do something with the listItem
         # this is the list satisfying the condition you wanted

Do you mean this ? 你是这个意思吗

cList = []
c9 = ['X', 'O', '5', '2']
c10 = ['X', 'O', '5', '2']
# .....
cList.append(c9)
cList.append(c10)
for cItem in cList:
    if '5' == cItem[2] and '2' == cItem[3]:
        print("YES!")

Find the name of the list which has 5 in the third position and 2 in the 4 position. 找到列表的名称,该列表的第三位置为5,第二位置为2。 How and what do I code to get the name of the list? 如何以及如何编码才能获得列表的名称?

You can check the content in each list name to see if that list contains the desired condition. 您可以检查每个列表名称中的内容,以查看该列表是否包含所需的条件。

However, I assume you don't want to type "C1", "C2" all the way to "C25". 但是,我假设您不想一直输入“ C1”,“ C2”到“ C25”。 So here you can use the keyword: " eval ". 因此,您可以在此处使用关键字:“ eval ”。

This way you don't have to concatenate your small lists into a big list. 这样,您不必将小列表连接成大列表。

Sample Usage: 样品用法:

>>> x = 1 >>> x = 1

>>> print eval('x+1') >>>打印eval('x + 1')

2 2

for i in range(1, 26):
    ListName = "C"+str(i)
    if eval(ListName)[2] == "5" and eval(ListName)[3] == "2":
        print(ListName, "is the list you are looking for!")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM