比较字符串列表以查找序列的缺失值

时间:2014-07-02 21:04:03

标签: python list comparison

我有一周中可能包含假日的日期列表,例如独立日,而不是星期五的日期名称。 (进行天气数据解析)寻找一种方法将我的列表与一周中的天数列表进行比较,找到丢失的一个(或更多)。我提出了以下内容,只要没有连续2个,它就可以正常工作,但我真的希望找到一个更好的方法,使其更加坚固,并能够在以后重新使用它。可能有一个标准的库方法吗?

daysOfTheWeek = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]

days = ["NOTADAY", "saturday", "NOTADAY", "monday"]

repaired = []
for i, day in enumerate(days):
    try:
        dayIndex = daysOfTheWeek.index(day.lower())
    except ValueError:
        try:
            next = indices[i+1]
            dayIndex = next-1
        except IndexError:
            dayIndex = 6
    if dayIndex > 6:
        dayIndex = 0
    repaired.append(daysOfTheWeek[dayIndex])      

for r in repaired:
    print r.capitalize()

2 个答案:

答案 0 :(得分:1)

假设您要在days中找到至少一个包含在daysOfTheWeek的旋转模式中的字符串,以便

模式
days = ["NOTADAY", "sunday", "NOTADAY", "NOTADAY", "NOTADAY", "NOTADAY"]

变为:

['Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday']

这有效:

if any(day in daysOfTheWeek for day in days):
    for i, day in enumerate(days):
        if day in daysOfTheWeek:
            j=daysOfTheWeek.index(day)
            repaired=[daysOfTheWeek[k % len(daysOfTheWeek)].capitalize() 
                                         for k in range(j-i, j+len(days)-i)]
            print days
            print repaired 
            break
else:
    print 'cannot be repaired'    

答案 1 :(得分:0)

在python中,一般方法是使用集合。

set(daysOfTheWeek).difference(set(days))

将返回以下内容:

In [3]: set(daysOfTheWeek).difference(set(days))
Out[3]: {'friday', 'sunday', 'thursday', 'tuesday', 'wednesday'}

可以将可散列对象的集合转换为集合。字符串,元组,整数都是可以清洗的。