简体   繁体   English

给定输入值时,从列表列表中返回列表

[英]Return lists from a list of lists when given an input value

I've been hammering my head for some time over this issue but can't seem find a solution for this, hence I ask for the assistance. 我已经在这个问题上坚持了一段时间,但似乎无法找到解决方案,因此我请求帮助。 PS: still a bit new to programming PS:编程还是有点新鲜

I have lists in a list: 我在列表中有列表:

[(2012, 'january', 'monday'), (2012, 'february', 'monday'), (2012, 'january', 'tuesday')]

What I want is a new list with lists, when giving in the input "monday': 我想要的是一个带有列表的新列表,当输入“monday”时:

[(2012, 'january', 'monday'), (2012, 'february', 'monday')]

So far my code: 到目前为止我的代码:

lists = [(2012, 'january', 'monday'), (2012, 'february', 'monday'), (2012, 'january', 'tuesday')]

day = input("Give day: ") #monday

def select_monday(lists, day):
    list2 = []
    for list in lists:
        if list[2] == day: #from here I'm stuck and do not know how to continue
            list2.append(list[2])
        else:
            return None
    return list2

Result: None 结果: None

I have no clue how to get all the lists with a certain value 我不知道如何获得具有特定值的所有列表

Your code was fine, except, you don't need else statement, because otherwise during next iteration you will loose the results from the previous steps; 你的代码很好,除了你不需要else语句,因为否则在下一次迭代中你会丢失前面步骤的结果; also you should actually call your function: 你也应该实际调用你的函数:

lists = [(2012, 'january', 'monday'), (2012, 'february', 'monday'), (2012, 'january', 'tuesday')]

day = input("Give day: ") #monday

def select_monday(lists, day):
    list2 = []
    for list in lists:
        if list[2] == day: #from here I'm stuck and do not know how to continue
            list2.append(list)
    return list2

print(select_monday(lists, day))

And here's a more compact function: 这是一个更紧凑的功能:

def select_monday_2(lists, day):
    return list(filter(lambda x: x[2] == day, lists))

print(select_monday_2(lists, day))

You return None if the day is not Monday. 如果当天不是星期一,则返回“ None ”。 This works better: 这效果更好:

def select_monday(lists, day):
    list2 = []
    for lst in lists:
        if lst[2] == day:
            list2.append(lst)
    return list2

Furthermore, append the whole list, not only the weekday. 此外,附上整个清单,而不仅仅是工作日。 Finally, better not use list as variable name because it is a built-in. 最后,最好不要使用list作为变量名,因为它是内置的。

Now: 现在:

>>> select_monday(lists, day)
[(2012, 'january', 'monday'), (2012, 'february', 'monday')]

Shorter alternative, using a list comprehension: 更短的替代方案,使用列表理解:

>>> [x for x in lists if x[2] == day]
[(2012, 'january', 'monday'), (2012, 'february', 'monday')]

The problem with this is the else statement. 这个问题是else语句。 If even one of the checks is false then it will return None . 如果其中一个检查是假的,那么它将return None Remove the else statement and get rid of the [2] on the list in the append statement, unless you just want to append the day. 删除else语句并删除append语句中列表中的[2] ,除非您只想追加当天。

lists = [(2012, 'january', 'monday'), (2012, 'february', 'monday'), (2012, 'january', 'tuesday')]

day = input("Give day: ") #monday

def select_monday(lists, day):
    list2 = []
    for list in lists:
        if list[2] == day: #from here I'm stuck and do not know how to continue
            list2.append(list)
    return list2

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

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