Python(Django)从选择列表中删除项目

时间:2018-03-21 15:57:08

标签: python django

我的网络表单列表已有,我需要根据minimummaximum分钟值从列表中删除一些选项。

例如,我想只显示60到180分钟的时间

Django表格:

time_list = (
        ('', 'Select'),
        ('15', '15 Minutes'),
        ('30', '30 Minutes'),
        ('45', '45 Minutes'),
        ('60', '60 Minutes'),
        ('75', '1:15'),
        ('90', '1:30'),
        ('105', '1:45'),
        ('120', '2:00'),
        ('135', '2:15'),
        ('150', '2:30'),
        ('165', '2:45'),
        ('180', '3:00'),
        ('240', '4:00'),
        ('300', '5:00'),
        ('360', '6:00'),
        ('420', '7:00'),
        ('480', '8:00'),
        ('540', '9:00'),
        ('600', '10:00'),
        ('660', '11:00'),
        ('720', '12:00'),
        ('1440', '24:00'),
    ) 

min_time = 60 #defined in DB
max_time = 180 #defined in DB

我在尝试过滤列表时失败了:

tmp = []
    for i in time_list:
        if i > min_time and i < max_time:
            tmp.append(i)
time_list =  tmp

2 个答案:

答案 0 :(得分:2)

尝试将其更改为

time_list = iter(time_list)
next(time_list)
tmp = []
    for i in time_list:
        if int(i[0]) > min_time and int(i[0]) < max_time:
            tmp.append(i)
time_list =  tmp

答案 1 :(得分:1)

在一行中:

time_list = list(filter(lambda x: min_time < int(x[0]) < max_time, time_list[1:]))

如果要包含空选项:

time_list = [time_list[0]] + list(filter(lambda x: min_time < int(x[0]) < max_time, time_list[1:]))