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

2024-05-15 01:52:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python的初学者,有一个问题。 我正在使用python3.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']

假设我想找到列表的名称,第三个位置有5个,第四个位置有2个。如何以及如何编码以获取列表的名称?你知道吗


Tags: 名称列表c2c1初学者c3c5c9
3条回答

Find the name of the list which has 5 in the third position and 2 in the 4 position. How and what do I code to get the name of the list?

您可以检查每个列表名称中的内容,以查看该列表是否包含所需的条件。你知道吗

但是,我假设您不想一直键入“C1”、“C2”到“C25”。所以这里可以使用关键字:“eval”。你知道吗

这样你就不必把你的小列表串联成一个大列表。你知道吗

Sample Usage:

>>> x = 1

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

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!")

你是说这个吗?你知道吗

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!")

将这些列表放入一个列表中,并使用循环对其进行迭代。你知道吗

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

相关问题 更多 >

    热门问题