Python 试图浏览tweets,检查twython上是否已经存在tweets

Python 试图浏览tweets,检查twython上是否已经存在tweets,python,dictionary,if-statement,twitter,twython,Python,Dictionary,If Statement,Twitter,Twython,我想看看我将要发布的引用是否已经发布。ExistString和randomQuote匹配一次(我知道我在randomQuote中调用:16,但不知何故:15和:16等于相同的输出),但randomQuote没有改变 非常感谢您的帮助,谢谢 您的代码结构有点混乱(我猜您没有正确缩进?),我不确定像printUntil这样的东西要做什么,但我建议是这样的: for tweets in ExistingTweets: ExistString = tweets['text'] E

我想看看我将要发布的引用是否已经发布。ExistString和randomQuote匹配一次(我知道我在randomQuote中调用:16,但不知何故:15和:16等于相同的输出),但randomQuote没有改变


非常感谢您的帮助,谢谢

您的代码结构有点混乱(我猜您没有正确缩进?),我不确定像printUntil这样的东西要做什么,但我建议是这样的:

    for tweets in ExistingTweets:
    ExistString = tweets['text']
    ExistString = ExistString[:15]
    if randomQuote[:16] == ExistString:
        randomQuote = AllQuotes[randint(0,5)].getText()
        randomQuote = randomQuote[printUntil:]
如果你有一个大的列表,上面列出了你已经发过推特的引文,这会容易得多;然后,您可以在现有tweets中使用
if randomQuote
作为测试,而根本不需要
for
循环


正如另一位评论者所说,您不应该使用不同的值(
[:15]
[:16]
)-它们可能在某些特定情况下给出相同的结果(例如字符串短于17个字符),但在其他情况下不会,如果您尝试从列表末尾开始切片,它将返回切片,直到列表末尾。这可能就是为什么您使用
:15
:16
@CarlesMitjans获得相同的输出,因为randomQuote在开始时多了一个字符。。。愚蠢的错误!谢谢你的快速回答。这样做已经更有意义了!然而。。。还是不行!我在for语句之后放了两个print语句,用于打印tweet和randomquote。它为tweet打印前15个字符,但为randomquote打印前14个字符!我也用了不同的数字,同样的东西。随机引用似乎总是短一个字符。有什么想法吗?!好吧,我想出来了,printUntil在字符串中添加了一个空格。。。我少了一个字符!初学者的错误哈哈哈。谢谢你的回答,我很感激!
randomQuote = AllQuotes[randint(0,5)].getText()    # Pick your first random quote
while True:
    already_used = False
    for tweets in ExistingTweets:
        if randomQuote[:15] == tweets['text'][:15]:
            already_used = True
            break
    if already_used:
        randomQuote = AllQuotes[randint(0,5)].getText() # Pick another and try again.
    else:
        break

print(randomQuote)