如何使用.index在列表中的列表中搜索项目的位置?

2024-04-26 20:56:45 发布

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

所以我现在正在创建一个程序,它要求我将调用函数格式化回类。我遇到的一个问题是,我无法返回一个项目在多个列表列表中的位置。我举个例子:

class Global():
    str_input = ""
    str_input_prv_word = ""
    str_input_nxt_word = ""

    prv_word = ""
    cur_word = ""
    nxt_word = ""

class Dog():
    wolf = [['fat', 'small'],[1, 3]]

input = "That's a small wolf"

def lis_str():
    ##### SPLIT INPUT STRING INTO DUPLICATE LISTS #####
    Global.str_input = input.split()
    Global.str_input_prv_word = input.split()
    Global.str_input_nxt_word = input.split()
lis_str()

def ya_done():
    ##### WHY CAN'T I FIND THE POSITION? :( #####
    print(Global.prv_word)
    print(Global.cur_word)
    print(Global.nxt_word)
    ##### PRINT POSITION OF ADJECTIVE DESCRIBING DOG IN DOG LIST #####
    exec(f"""if Global.prv_word in Dog.{Global.cur_word}:
    print(Dog.{Global.cur_word}.index('{Global.prv_word}'))""")


def create_words():
    ##### CREATE NEXT WORD FOR GLOBAL CLASS #####
    if len(Global.str_input_nxt_word) != 0:
        if len(Global.nxt_word) == 0:
            Global.nxt_word = Global.str_input_nxt_word.pop(1)
        if len(Global.nxt_word) != 0:
            Global.nxt_word = Global.str_input_nxt_word.pop(0)
    if len(Global.str_input_nxt_word) == 0:
        None
    ##### CREATE CURRENT WORD AND PREVIOUS WORD FOR GLOBAL CLASS #####
    if len(Global.str_input) != 0:
        if len(Global.cur_word) != 0:
            Global.prv_word = Global.str_input_prv_word.pop(0)
        Global.cur_word = Global.str_input.pop(0)
    if len(Global.str_input) != 0:
        create_words()
    ##### CREATE THE FINAL PREVIOUS WORD #####
    else:
        ya_done()

create_words()

现在这只是一个例子,我写得很快,但我仍然面临着完全相同的问题。现在如果我删除狗列表中的第二个列表,即[1,3]

示例:

class Dog():
    wolf = ['fat', 'small']

它运行良好,并返回值“1”,因为它应该为单词“small”

如何在列表中找到列表中项目的位置? 任何帮助都将不胜感激!非常感谢。你知道吗


Tags: 列表inputlenifpopglobalwordsmall
3条回答
>>> biglist = [['a', 'b', 'c'], [1, 2, 3]]
>>> print biglist[0]
['a', 'b', 'c']
>>> print biglist[1]
[1, 2, 3]
>>> print biglist[0].index('b')
1

这里的问题是,列表的实际元素是列表本身。如果你查看整个列表,你会发现他们的位置:

In [13]: a = [[1, 2, 3], ['fat', 'small']]

In [14]: a.index(['fat', 'small'])
Out[14]: 1

In [15]: a.index([1, 2, 3])
Out[15]: 0

如果您真正想要的是查看每个子列表,那么必须在每个子列表上使用.index。您可以使用for循环或理解:

In [16]: [l.index('small') for l in a if 'small' in l]
Out[16]: [1]

这将返回一个目标词的所有出现的列表。如果您知道它只出现一次,那么您可以从结果列表中获取一个数字:

In [17]: [l.index('small') for l in a if 'small' in l][0]
Out[17]: 1

这里有两个解决方案,你可以使用。你知道吗

第一个解决方案将收集每一行的位置与您的字和字的位置到他们。你知道吗

wolf = [['fat', 'small'],[1, 3]]

def get_positions(lists, word):
    i = 0
    for list in lists:
        if word in list:
            index = list.index(word)
            print "List Position: " + str(i) + "\nWord Postion: " + str(index) + "\n"
        i += 1

get_positions(wolf, "small")

输出:

List Position: 0
Word Postion: 1

下面是一个多行使用该单词的示例。你知道吗

example2 = [['fat', 'small'],[1, 'fat']]

get_positions(example2, "fat")

输出

List Position: 0
Word Postion: 0

List Position: 1
Word Postion: 1

第二种解决方案,必须提供数组、嵌套数组位置和单词,它将返回此嵌套数组的索引位置:

wolf = [['fat', 'small'],[1, 3]]

def get_positions(lists, list_index, word):
    result = False
    if word in lists[list_index]:
        result = lists[list_index].index(word)
    return result

print get_positions(wolf, 0, "small")

输出:

1

相关问题 更多 >