【问题标题】:Removing duplicates from the list of unicode strings从 unicode 字符串列表中删除重复项
【发布时间】:2013-08-20 04:32:06
【问题描述】:

我正在尝试从 unicode 字符串列表中删除重复项而不更改其中出现的元素的顺序(所以,我不想使用集合)。

计划:

result = [u'http://google.com', u'http://www.catb.org/esr/faqs/hacker-howto.html', u'http://www.catb.org/~esr/faqs/hacker-howto.html',u'http://amazon.com', u'http://www.catb.org/esr/faqs/hacker-howto.html', u'http://yahoo.com']
result.reverse()
for e in result:
    count_e = result.count(e)
    if count_e > 1:
        for i in range(0, count_e - 1):
            result.remove(e)
result.reverse()
print result

输出:

[u'http://google.com', u'http://www.catb.org/esr/faqs/hacker-howto.html', u'http://www.catb.org/~esr/faqs/hacker-howto.html', u'http://amazon.com', u'http://yahoo.com']

预期输出:

[u'http://google.com', u'http://catb.org/~esr/faqs/hacker-howto.html', u'http://amazon.com', u'http://yahoo.com']

那么,有什么方法可以做到尽可能简单。

【问题讨论】:

  • 您确定更新了实际输出吗?非 unicode 仍然存在

标签: python list unicode-string


【解决方案1】:

您的列表中实际上没有重复项。一次是http://catb.org,另一次是http://www.catb.org

你必须想办法确定 URL 前面是否有www.

【讨论】:

  • 哎呀...复制粘贴时出错了,实际上它有 www 请现在检查
  • @Balakrishnan 一个有~esr 另一个有esr
  • 哦...是的...对不起。所以,我现在必须解决这个问题。有什么简单的方法可以比较网址是否指向同一页面。
  • @Balakrishnan 你可以试试看urlparse
【解决方案2】:

您可以创建一个新列表并将项目添加到其中(如果它们尚未在其中)。

result = [ /some list items/]
uniq = []
for item in result:
    if item not in uniq:
        uniq.append(item)

【讨论】:

  • 不,它不工作它仍然返回[u'http://google.com', u'http://catb.org/~esr/faqs/hacker-howto.html', u'http://www.catb.org/~esr/faqs/hacker-howto.html', u'http://amazon.com', 'http://www.catb.org/esr/faqs/hacker-howto.html', u'http://yahoo.com']
【解决方案3】:

您可以使用一个集合,然后按原始索引对其进行排序:

sorted(set(result), key=result.index)

这是因为 index 返回第一次出现(因此它会根据原始列表中的第一次出现保持它们的顺序)

我还注意到原始字符串中的一个字符串不是 unicode 字符串。所以你可能想做这样的事情:

u = [unicode(s) for s in result]
return sorted(set(u), key=u.index)

编辑:'http://google.com''http://www.google.com' 不是字符串重复项。如果你想这样对待它们,你可以这样做:

def remove_www(s):
    s = unicode(s)
    prefix = u'http://'
    suffix = s[11:] if s.startswith(u'http://www') else s[7:]
    return prefix+suffix

然后将之前的代码替换为

u = [remove_www(s) for s in result]
return sorted(set(u), key=u.index)

【讨论】:

  • 不,它不工作它仍然返回[u'http://google.com', u'http://catb.org/~esr/faqs/hacker-howto.html', u'http://www.catb.org/~esr/faqs/hacker-howto.html', u'http://amazon.com', 'http://www.catb.org/esr/faqs/hacker-howto.html', u'http://yahoo.com']
  • 另外,正如其他人指出的那样,www.foo.comfoo.com 默认情况下不会被视为相同的字符串。
  • 我的最后一条评论是正确的。它正确地剥离了重复的字符串,只是没有任何重复的字符串。所以海德罗的回答是正确的。
【解决方案4】:

这是一个在原地修改result的方法:

result = [u'http://google.com', u'http://catb.org/~esr/faqs/hacker-howto.html', u'http://www.catb.org/~esr/faqs/hacker-howto.html',u'http://amazon.com', 'http://www.catb.org/esr/faqs/hacker-howto.html', u'http://yahoo.com']
seen = set()
i = 0
while i < len(result):
    if result[i] not in seen:
        seen.add(result[i])
        i += 1
    else:
        del result[i]

【讨论】:

    猜你喜欢
    • 2019-05-17
    • 1970-01-01
    • 2014-08-02
    • 2023-01-09
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多