【问题标题】:python return duplicates in listpython返回列表中的重复项
【发布时间】:2014-04-11 13:55:01
【问题描述】:

如何从字符串列表中找到重复列表? 给出了清理函数

def clean_up(s):
""" (str) -> str

Return a new string based on s in which all letters have been
converted to lowercase and punctuation characters have been stripped 
from both ends. Inner punctuation is left untouched. 

>>> clean_up('Happy Birthday!!!')
'happy birthday'
>>> clean_up("-> It's on your left-hand side.")
" it's on your left-hand side"
"""

punctuation = """!"',;:.-?)([]<>*#\n\t\r"""
result = s.lower().strip(punctuation)
return result

这是我的复制函数。

def duplicate(text):
""" (list of str) -> list of str

>>> text = ['James Fennimore Cooper\n', 'Peter, Paul, and Mary\n',
'James Gosling\n']
>>> duplicate(text)
['james']
"""

cleaned = ''
non_duplicate = []
unique = []
for word in text:
    cleaned += clean_up(word).replace(",", " ") + " "
    words = cleaned.split()        
    for word in words:
         if word in unique:

我被困在这里.. 我不能使用字典或任何其他技术来计算文本中每个单词的频率。 请帮忙..

【问题讨论】:

    标签: python string list python-3.x duplicates


    【解决方案1】:

    你这里有问题:

    cleaned += clean_up(word).replace(",", " ") + " "
    

    这一行将新的“单词”添加到到目前为止所有单词的增长字符串中。因此,每次通过for 循环时,您都会重新检查到目前为止您看到的所有单词。

    相反,您需要这样做:

    for phrase in text:
        for word in phrase.split(" "):
            word = clean_up(word)
    

    这意味着您只处理每个单词一次。然后,您可能需要将其添加到您的一个列表中,具体取决于它是否已经在其中一个列表中。我建议您将您的列表称为seenduplicates,以便更清楚地了解发生了什么。

    【讨论】:

    • 好点。不过为了对 OP 公平起见 - clean_up 仅删除前导/尾随逗号(和其他标点符号)......似乎 re.findall('\w+', text) 将是一个更合适的标记器,但这取决于 OP。如果可能,OP 也可能希望考虑使用集合,而不是列表。
    猜你喜欢
    • 2020-07-22
    • 2015-10-04
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    相关资源
    最近更新 更多