如何从中删除重复项?

2024-04-18 10:18:19 发布

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

我将这样的值写入文件:

(u'fresh', 4.3557350075853982)  
(u'fresh', 6.6629604801359461)
(u'focus', 6.4398169288217364)


p= zip(vectorizer.get_feature_names(), idf)        #('object' , score)
with codecs.open("scores.txt","a") as t:
        for x in p:
            if not x:
                print>>t, x

我要做的是从文件中删除重复的内容。所以我试着检查x是否已经存在,而不是再写一次。既然它是一个压缩组件,我该怎么做呢


Tags: 文件getobjectnameswithopenzipfeature
1条回答
网友
1楼 · 发布于 2024-04-18 10:18:19

试试这个

"""(u'fresh', 4.3557350075853982)  
(u'fresh', 6.6629604801359461)
(u'focus', 6.4398169288217364)"""


p= zip(vectorizer.get_feature_names(), idf)        #('object' , score)
outputted = set()
with codecs.open("scores.txt","a") as t:
        for x in p:
            if x[0] in outputted: continue
            print>>t, x
            outputted.add(x[0])

这是假设您只想删除“object”的重复项如果您认为重复项与(object,score)对完全相同,请将答案中的x[0]替换为x

从本质上讲,这是做的,它是跟踪对象/分数你已经输出,而不是重新输出对象/分数已经输出了对象之前

相关问题 更多 >