【问题标题】:Detect where characters are in multiple lists检测字符在多个列表中的位置
【发布时间】:2016-10-02 04:43:34
【问题描述】:

我希望能够在字符串中搜索不同列表中的字符。

例如:

list1=['a']

list2=['b']

例如,如果用户输入“a”。 Python 应该打印 'list1'

但是,如果用户输入的是 'ab',那么 Python 应该打印 list1 和 list2

现在我正在尝试使用这个函数来查询列表,但是我必须在两个单独的 if 语句中查询每个列表,就像这样。

def char_search(input_str):
    for char in input_str:
        if char in list1:
            print("lower")
        if char in list2:
            print("Upper")

【问题讨论】:

  • 你为什么取消标记答案

标签: python list if-statement


【解决方案1】:

例子

list_1 = ['a', 'b', 'c']
list_2 = ['d', 'e', 'f']

input = 'b'

if input in list_1: 
    print "list1"

if input in list_2: 
    print "list2"

输出将是 “列表1”

【讨论】:

    【解决方案2】:
    list1 = ['a']
    list2 = ['b'] 
    
    if (input[0] in list1 or input[1] in list1)  and (input[0] in list2 or input[1] in list2): 
        print "Choice listed"
    

    如果您想检查两个字符是否都在至少一个列表中,则此方法有效。对于更多输入字符,您可以添加一些简单的循环逻辑

    【讨论】:

    • 您好,我必须取消标记您的答案,因为我需要确切知道每个字符列在哪个列表中,并且不限制字符数...
    【解决方案3】:
    choice = input("choose a letter")
    
    if choice in list1:
        print(list1)
    elif choice in list2:
        print(list2)
    else:
        print("choice not listed")
    

    【讨论】:

    • 如果choiceab,则此代码将打印"choice not listed",但OP 指定它应打印两个列表。
    【解决方案4】:

    所以,你有一些列表:

    list1 = [ ... ]
    list2 = [ ... ]
    ...
    

    首先您需要能够将所有列表收集在一起,然后检查每个列表是否包含指定的项目:

    for lst in list1, list2, list3:
        if mychar in lst:
            print(lst)
            break
    else:
        print('Specified lists do not contain', mychar)
    

    请注意,一旦在其中一个列表中找到项目,它就会停止。而且,它不会打印列表的名称——如果你想要它,你需要将列表命名为:

    class NamedList(list):
        def __init__(self, *args, name=None, **kwargs):
            self.name = name
            super().__init__(*args, **kwargs)
    
        def __str__(self):  # only if you need that specific use
            if self.name is not None: 
                return str(self.name)
            return super().__str__()
    
    # Then use as follows
    std = NamedList(['ISO', 'GOST', 'etc'], name='Standards')
    sizes = NamedList(['A4', 'Letter'], name='Paper sizes')
    
    smthng = 'Letter'
    
    for nlst in std, sizes:
        if smthng in nlst:
            print(nlst)  # gives you 'Paper sizes'
    

    更正:兼容python 2.x/3.x,使用this

    【讨论】:

    • 我什至没有提到玩 vars()globals() ——这是一种非常混乱的非 Python 方式,但是是的,你可以在整个命名空间中搜索列表,检查是否指定对象在其中之一,并打印与该列表关联的变量名称。这将是一个糟糕的坏方法,但它是对所提出的确切问题的确切答案。
    • 您好,我正在努力让您的答案正常工作,但我遇到了一个语法错误,您已经开始在 NamedList(list) 类下定义一个函数...
    • @JoshuaStokes ,我认为这是因为您使用的是 python 2.x。原始问题中没有提到版本,所以我在答案中使用 3.x 。如果你想让它在 2.x 下工作,请使用这个:pastebin.com/G7A8a987
    • 啊,是的,我没有意识到“python2.7”有一个单独的标签。我只是假设“python”标签的意思是 2.7...
    • 好的,唯一的问题是这个脚本只适用于一个字符。这个想法基本上是遍历字符串并打印该字符串中出现任何字符的列表。例如,如果我的字符串是 'abc' 和 list1 = 'a',list2 = 'b' 和 list3 = 'c'。我希望 python 从字面上打印 list1 = 'a'、list2 = 'b' 和 list3 = 'c'。但是,如果字符串是'abd',那么我只希望它打印list1和list2,因为list1包含'a,list2包含'b',但是由于list3包含'c'并且我没有指定'c'在我的字符串,那么 list3 需要被忽略...
    猜你喜欢
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多