因爲我是Python新手,所以不敢和我在一起。 我想,但只有基於文本複製在字典中刪除重複值在二維數組中刪除重複項python
所以比如我想刪除重複的鳴叫名單:
{'text': 'Dear Conservatives: comprehend, if you can RT Iran deal opponents have their "death panels" lie, and it\'s a whopper http://t.co/EcSHCAm9Nn', 'id': 634092907243393024L}
{'text': 'RT Iran deal opponents now have their "death panels" lie, and it\'s a whopper http://t.co/ntECOXorvK via @voxdotcom #IranDeal', 'id': 634068454207791104L}
{'text': 'RT : Iran deal quietly picks up some GOP backers via https://t.co/65DRjWT6t8 catoletters: Iran deal quietly picks up some GOP backers \xe2\x80\xa6', 'id': 633631425279991812L}
{'text': 'RT : Iran deal quietly picks up some GOP backers via https://t.co/QD43vbJft6 catoletters: Iran deal quietly picks up some GOP backers \xe2\x80\xa6', 'id': 633495091584323584L}
{'text': "RT : Iran Deal's Surprising Supporters: https://t.co/pUG7vht0fE catoletters: Iran Deal's Surprising Supporters: http://t.co/dhdylTNgoG", 'id': 633083989180448768L}
{'text': "RT : Iran Deal's Surprising Supporters - Today on the Liberty Report: https://t.co/PVHuVTyuAG RonPaul: Iran Deal'\xe2\x80\xa6 https://t.co/sTBhL12llF", 'id': 632525323733729280L}
{'text': "RT : Iran Deal's Surprising Supporters - Today on the Liberty Report: https://t.co/PVHuVTyuAG RonPaul: Iran Deal'\xe2\x80\xa6 https://t.co/sTBhL12llF", 'id': 632385798277595137L}
{'text': "RT : Iran Deal's Surprising Supporters: https://t.co/hOUCmreHKA catoletters: Iran Deal's Surprising Supporters: http://t.co/bJSLhd9dqA", 'id': 632370745088323584L}
{'text': '#News #RT Iran deal debate devolves into clash over Jewish stereotypes and survival - W... http://t.co/foU0Sz6Jej http://t.co/WvcaNkMcu3', 'id': 631952088981868544L}
{'text': '"@JeffersonObama: RT Iran deal support from Democratic senators is 19-1 so far....but...but Schumer...."', 'id': 631951056189149184L}}
得到這個:
{'text': 'Dear Conservatives: comprehend, if you can RT Iran deal opponents have their "death panels" lie, and it\'s a whopper http://t.co/EcSHCAm9Nn', 'id': 634092907243393024L}
{'text': '"@JeffersonObama: RT Iran deal support from Democratic senators is 19-1 so far....but...but Schumer...."', 'id': 631951056189149184L}}
到目前爲止,我主要是找到基於'正常'字典的答案,其中重複的鍵/值是相同的。在我的情況下,它是一個合併的字典。由於轉推,文字鍵是相同的,但相應的推特ID是不同的
這是整個代碼,以更有效的方式在csv文件中編寫推文的任何提示(使刪除重複項更容易)比歡迎。
import csv
import codecs
tweet_text_id = []
from TwitterSearch import TwitterSearchOrder, TwitterUserOrder, TwitterSearchException, TwitterSearch
try:
tso = TwitterSearchOrder()
tso.set_keywords(["Iran Deal"])
tso.set_language('en')
tso.set_include_entities(False)
ts = TwitterSearch(
consumer_key = "aaaaa",
consumer_secret = "bbbbb",
access_token = "cccc",
access_token_secret = "dddd"
)
for tweet in ts.search_tweets_iterable(tso):
tweet_text_id.append({'id':tweet['id'], 'text': tweet['text'].encode('utf8')});
fieldnames = ['id', 'text']
tweet_file = open('tweets.csv', 'wb')
csvwriter = csv.DictWriter(tweet_file, delimiter=',', fieldnames=fieldnames)
csvwriter.writerow(dict((fn,fn) for fn in fieldnames))
for row in tweet_text_id:
csvwriter.writerow(row)
tweet_file.close()
except TwitterSearchException as e:
print(e)
感謝您的幫助!
你有什麼是字典列表,而不是一個 「合併字典」。無論如何,你的例子並不清楚。您只想保留第一個和最後一個條目,但其他條目不全是完全重複的。 –