【问题标题】:Issue with equality between tuple elements and constant values元组元素和常量值之间的相等问题
【发布时间】:2015-07-01 05:50:24
【问题描述】:
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。这真的很奇怪,对于第一个输出,您可以看到月份和日期完全相同,但是在比较日期时却说它们不一样。这对我来说真的是一个大脑破坏者。如果有人能告诉我是什么导致了这个问题,那将不胜感激。我觉得这很简单,但它太简单了,我完全忽略了这个问题。

【问题讨论】:

  • 您能否编辑您的代码,使其与您运行的代码完全相同?包括对sameDate的实际调用?在您的输出中,您正在打印“到达这里”,但在您正在打印的功能中,您正在打印“YO WE HERE”。
  • 我编辑了我的帖子。感谢您的观看。
  • 您编辑的代码没有 self.sameDay 方法或 self.monthAr。
  • 道歉。我修好了。
  • 如果没有任何效果,您可以尝试从 dateup[1] 中减去 day 并检查它是否为 0。而不是 day == int(datetup[1])。只是尝试另一种表达方式。但它应该像 atlspin 已经指出的那样工作。

标签: python tuples


【解决方案1】:

解决方案:

我的月份和日期是 unicode。只需转换为 int 即可解决问题。

【讨论】:

    【解决方案2】:

    我运行了您的代码,它对我来说运行良好。我添加了一些括号,因为我正在运行 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
    >>>
    

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

    【讨论】:

    • 我正在传递一个包含三个元素的元组,这不应该改变事情,因为我只是在访问元素,对吧?
    猜你喜欢
    • 2017-08-20
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    相关资源
    最近更新 更多