比较 python 中的两个列表并获得不相等

Compare two lists in python and obtain non-equality

这段代码理论上是要比较两个有tweet ID的列表,如果在这个比较中已经存在于丝印中,否则不存在。 但是我打印的全部还是没有被列出来。 有什么建议可以比较这两个 ID 列表,如果不是第二个列表中第一个列表的 ID,则打印它? 对不起,效率低的代码。 (和我的英语)

我所追求的实际上是在我已经拥有的情况下不重复转发(转推)。我使用 Tweepy 库,我阅读了时间线,并制作了推文 RT 我没有做 RT

def analizarRT():
    timeline = []
    temp = []
    RT = []
    fileRT = openFile('rt.txt')
    for status in api.user_timeline('cnn', count='6'):
        timeline.append(status)
    for i in range(6):
        temp.append(timeline[i].id)
    for a in range(6):
        for b in range(6):
            if str(temp[a]) == fileRT[b]:
                pass
            else:
                RT.append(temp[a])
    for i in RT:
        print i

已解决添加此功能!

def estaElemento(tweetId, arreglo):
    encontrado = False
    for a in range(len(arreglo)):
         if str(tweetId) == arreglo[a].strip():
            encontrado = True
            break
return encontrado

您的 else 语句与 for 语句关联,您可能需要再添加一个缩进以使其适用于 if 语句。

这是一个简单的程序,不要把它复杂化。根据您的评论,有两个列表:)

1.时间轴

2。文件RT

现在,您想比较这两个列表中的 id's。在这样做之前,您必须了解这两个列表的性质。

我的意思是,列表中的数据类型是什么?

Is it

  1. list of strings? or
  2. list of objects? or
  3. list of integers?

所以,找出它,调试它,或者在你的代码中使用 print 语句。或者请在您的问题中添加这些详细信息。所以,你可以给出一个完美的答案。

同时,试试这个:

if timeline.id == fileRT.id 应该可以。

已编辑:

def analizarRT():
    timeline = []
    fileRT = openFile('rt.txt')
    for status in api.user_timeline('cnn', count='6'):
        timeline.append(status)
    for i in range(6):
        for b in range(6):
            if timeline[i].id == fileRT[b].id:
                pass
            else:
                newlist.append(timeline[i].id)
    print newlist

按照你的问题,你是想得到它们是吧。我已将它们附加到 newlist 中。现在你可以说 print newlist 来查看项目