元组元素和常量值之间的相等问题

时间:2015-06-30 21:11:49

标签: python tuples

self.monthAr={"Jan":1,"Feb":2,"Mar":3,"Apr":4,"May":5,"Jun":6,"Jul":7,"Aug":8,"Sept":9,"Oct":10,"Nov":11,"Dec":12}

    def sameDate(self,month,day,datetup):
            print str(month) + " " + str(datetup[0]) + ": " + str(day) + " " + str(datetup[1])
            n= month == int(datetup[0])
            m=day == int(datetup[1])
            print n
            print m
            time.sleep(1)
            if (n and m):
                print "YO WE HERE"
                return True
            else:
                return False    

    def getPast(self, daysPast, apicalls=False):
                dic={}
                a=datetime.date.today()
                #will contain tuples of all dates of interest in form MONTH,DAY,YEAR
                b=[]
                for i in range(0,daysPast+1):
                    d=a- datetime.timedelta(days=i)
                    dateString = str(d.month) +'-' + str(d.day) + "-" +str(d.year)
                    b.append((d.month,d.day,d.year))
                    dic[dateString]= {}
                    dic[dateString]["tweetCount"]=0
                    if(i== daysPast):
                        self.tso.arguments.update({'since': '%s' % d.strftime('%Y-%m-%d')})



                try:


                    for tweet in self.ts.search_tweets_iterable(self.tso):
                        date=tweet["created_at"]
                        date=date.split()
                        print date
                        dateMonth= self.monthAr[date[1]]
                        print str(dateMonth) + " datmonth"
                        day=date[2]
                        for x in b:
                            print "got here"
                            #print str(dateMonth) + " " + str(x[0]) + ": " + str(day) + " " + str(x[1])
                            #print self.sameDate(dateMonth,day,x)
                            if(self.sameDate(dateMonth,day,x)):
                                print "got here 1"
                                accessDate=self.tupToString(x)
                                dic[accessDate]["tweetCount"]+=1
                                print "Plus 1 to : %s" % (accessDate,)
                                break

        x=searchTwitter("Bernie Sanders")
        x.getPast(10)

这里的一些示例输出可以与代码相关联。

6 6: 30 30
True
False
got here
6 6: 30 29
True
False
got here
6 6: 30 28
True
False
got here
6 6: 30 27
True
False
got here
6 6: 30 26
True
False
got here
6 6: 30 25
True
False
got here
6 6: 30 24
True
False
got here
6 6: 30 23
True
False
got here
6 6: 30 22
True
False
got here
6 6: 30 21
True
False
got here
6 6: 30 20
True
False

在使用元组比较月份和日期的两种情况下,我的函数都不会返回true。这在这里真的很奇怪,对于第一个输出,你可以看到几个月和几天是完全一样的,但是当比较它说的天数不一样时。这对我来说真的是一个大脑破坏者。如果有人能告诉我是什么导致了这个问题,那将非常感激。我觉得它很简单,但它很简单,我完全忽略了这个问题。

2 个答案:

答案 0 :(得分:1)

解决方案:

我的月和日都在unicode。只需转换为int即可解决问题。

答案 1 :(得分:0)

我运行了你的代码,它对我来说很好。我添加了一些括号,因为我正在运行python 3,但除此之外它是相同的:

import time

def sameDate(month,day,datetup):
    print (str(month) + " " + str(datetup[0]) + ": " + str(day) + " " +
        str(datetup[1]))
    n = month == int(datetup[0])
    m = day == int(datetup[1])
    print (n)
    print (m)
    time.sleep(1)
    if (n and m):
        print ("YO WE HERE")
        return True
    else:
        return False

以下是测试用例输出失败的原因:

>>> sameDate(6, 30, (6, 30))
6 6: 30 30
True
True
YO WE HERE
True
>>>

所以我想说的是,问题不在你提供的代码中。