比较python中的两个列表并获得质量

2024-07-03 08:20:14 发布

您现在位置:Python中文网/ 问答频道 /正文

理论上,这段代码必须比较两个具有tweet ID的列表,在这个比较中,如果它已经存在于丝网印刷中,否则就不存在了。 但我打印了所有的或没有被列出。 有什么建议来比较这两个ID的列表,如果不是第一个列表的ID在第二个然后打印出来? 抱歉,代码效率太低了。(还有我的英语)

我所寻求的其实是不做RT(转发)重复当我已经有。我用Tweepy库,我读时间线,做tweet 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

Tags: 代码inid列表fordefrangetemp
2条回答

如果else语句与for语句相关联,则可能需要再添加一个缩进以使其在if语句上工作。你知道吗

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

<强>1。时间线

<强>2。文件

现在,您要比较这两个列表中的id。在你这么做之前,你必须知道这两个列表的性质。你知道吗

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

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

根据你的问题,你想得到它们,对吗?。我已将它们添加到新列表中。现在您可以说print newlist来查看项目

相关问题 更多 >