简体   繁体   English

使用substring打印出多个列表

[英]Using substring to print out multiple lists

I have a dictionary with lists and want to create a function that can find lists by using "substring". 我有一个包含列表的字典,想要创建一个可以使用“substring”查找列表的函数。 So that if the list's name is "Things to do on Monday" you only have to type "Monday" to find the list. 因此,如果列表的名称是“周一要做的事情”,则只需键入“星期一”即可找到列表。

Here is what I have so far. 这是我到目前为止所拥有的。

A dictionary. 一本字典。

list_dict = {}
list_dict["Things to do on Monday"] = ['clean', 'workout', 'buy food']
list_dict["Grocery list"] = ['oranges', 'apples', 'milk']
list_dict["Monday outfit"] = ['jeans', 't-shirt', 'shoes']

A function. 一个功能。

def substring_function(substring):
    for key in list_dict.keys():
        if substring in key:
            return list_dict[key]

Then, I do for example: 然后,我做了例如:

print(substring_function("Monday"))

The problem is that as you can see I have the word "Monday" in two lists, and I can't figure out how to make it return BOTH lists. 问题是,你可以看到我在两个列表中有“星期一”这个词,我无法弄清楚如何让它返回两个列表。

I guess there is something wrong with my for-loop. 我想我的for循环有问题。

Can anyone help me out? 谁能帮我吗? Thanks a lot! 非常感谢!

You can make the function a generator by replacing the return statement with a yield statement instead: 您可以通过将return语句替换为yield语句来使该函数成为生成器:

def substring_function(substring):
    for key in list_dict.keys():
        if substring in key:
            yield list_dict[key]

so that list(substring_function("Monday")) will return a list of matches. 这样list(substring_function("Monday"))将返回一个匹配列表。

You need add every ocurrency in one var and return after the loop finish 您需要在一个var中添加每个字符,并在循环结束后返回

def substring_function(substring):
    rs = list()
    for key in list_dict.keys():
        if substring in key:
            rs.append(list_dict[key])
    return rs

You can try link this. 你可以尝试链接这个。

>>> d = {}
>>> d["Things to do on Monday"] = ['clean', 'workout', 'buy food']
>>> d["Grocery list"] = ['oranges', 'apples', 'milk']
>>> d["Monday outfit"] = ['jeans', 't-shirt', 'shoes']
>>> 
>>> d
{'Things to do on Monday': ['clean', 'workout', 'buy food'], 'Monday outfit': ['jeans', 't-shirt', 'shoes'], 'Grocery list': ['oranges', 'apples', 'milk']}
>>> 

>>> def substring_function(substring, list_dict):
...     final_list = []
...     for key in list_dict:
...         if substring in key:
...             final_list.append(list_dict[key])
...     return final_list
... 
>>> substring_function("Monday", d)
[['clean', 'workout', 'buy food'], ['jeans', 't-shirt', 'shoes']]
>>> 

Further, you can pass the returned object to any function as follows (by destructing it using * ). 此外,您可以将返回的对象传递给任何函数,如下所示(通过使用*对其进行破坏)。

>>> def print_items(*args):
...     for arg in args:
...         print(arg)
... 
>>> 
>>> print_items(*substring_function("Monday", d))
['clean', 'workout', 'buy food']
['jeans', 't-shirt', 'shoes']
>>> 
>>> # OR 
... 
>>> items = substring_function("Monday", d)
>>> print_items( *items )
['clean', 'workout', 'buy food']
['jeans', 't-shirt', 'shoes']
>>> 

How about just looking for the 'monday' key(or the key you want to search for): 如何只查找“星期一”键(或您要搜索的键):

for key in d.keys():
    if 'monday' in key.lower():
        print(key, d[key])
#Output:
Things to do on Monday ['clean', 'workout', 'buy food']
Monday outfit ['jeans', 't-shirt', 'shoes']

and if you want both lists in one: 如果你想要两个列表:

def substring(key_to_look_for):
    test = []
    for key in d.keys():
        if key_to_look_for in key.lower():
            test.append(d[key])
    return test

print(substring('monday'))
#Output:
[['clean', 'workout', 'buy food'], ['jeans', 't-shirt', 'shoes']]

With list comprehensions: 使用列表推导:

def substring(key_to_look_for):
    l = [d[key] for key in d.keys() if key_to_look_for in key.lower()]
    return l
print(substring('monday'))
#Output:
[['clean', 'workout', 'buy food'], ['jeans', 't-shirt', 'shoes']]

List comprehension with lambdas: 使用lambdas列出理解:

def substring(key_to_look_for):
    l = [d[i] for i in list(filter(lambda x: key_to_look_for in x.lower(), d.keys()))]
    return l
substring('monday')
#Output:
[['clean', 'workout', 'buy food'], ['jeans', 't-shirt', 'shoes']]

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

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