【问题标题】:combining thousands of list strings in python在python中组合数千个列表字符串
【发布时间】:2021-12-18 07:18:32
【问题描述】:

我有一个“爱丽丝梦游仙境”的 .txt 文件,需要去掉所有标点符号并使所有单词小写,这样我才能找到文件中唯一单词的数量。下面提到的wordlist 是书中所有单个单词作为字符串的列表,所以wordlist 看起来像这样


    ["Alice's", 'Adventures', 'in', 'Wonderland', "ALICE'S", 
    'ADVENTURES', 'IN', 'WONDERLAND', 'Lewis', 'Carroll', 'THE', 
    'MILLENNIUM', 'FULCRUM', 'EDITION', '3.0', 'CHAPTER', 'I', 
    'Down', 'the', 'Rabbit-Hole', 'Alice', 'was', 'beginning', 
    'to', 'get', 'very', 'tired', 'of', 'sitting', 'by', 'her', 
    'sister', 'on', 'the', 'bank,'

   

到目前为止我的解决方案代码是


from string import punctuation
def wordcount(book):

    for word in wordlist:
        no_punc = word.strip(punctuation)
        lower_case = no_punc.lower()
        newlist = lower_case.split()
        print(newlist)

这适用于去除标点符号并使所有单词小写,但是newlist = lower_case.split() 会为每个单词创建一个单独的列表,因此我无法遍历一个大列表来查找唯一单词的数量。我这样做.split() 的原因是,当迭代时,python 不会将任何字母视为一个单词,而是每个单词都保持完整,因为它是它自己的列表项。关于如何改进这个或更有效的方法的任何想法?这是输出的示例


    ['down']
    ['the']
    ['rabbit-hole']
    ['alice']
    ['was']
    ['beginning']
    ['to']
    ['get']
    ['very']
    ['tired']
    ['of']
    ['sitting']
    ['by']
    ['her']

【问题讨论】:

    标签: string list concatenation strip txt


    【解决方案1】:

    这里是你的代码修改与输出

    from string import punctuation
    
    wordlist = "Alice fell down down down!.. down into, the hole."
    
    single_list = []
    for word in wordlist.split(" "):
        no_punc = word.strip(punctuation)
        lower_case = no_punc.lower()
        newlist = lower_case.split()
        #print(newlist)
        single_list.append(newlist[0])
    
    print(single_list)
    #to get the unique
    single_list_unique = set(single_list)
    print(single_list_unique)
    print(len(single_list_unique))
    

    这会产生:

    ['alice', 'fell', 'down', 'down', 'down', 'down', 'into', 'the', 'hole']
    

    和唯一的集合:

    {'fell', 'alice', 'down', 'into', 'the', 'hole'}
    

    以及唯一的长度:

    6
    

    (这可能不是最有效的方法,但它与您当前的代码很接近,并且足以容纳那本包含数千个元素的书。如果这是一个服务于多个请求的后端进程,您可以通过改进对其进行优化)

    编辑---------

    您可能正在使用传入列表的库从文件中导入,在这种情况下您会产生错误 AttributeError: 'list' object has no attribute 'split',或者您可能会因为空字符串而看到错误 IndexError: list index out of range。在这种情况下,您使用此修改:

    from string import punctuation
    
    wordlist2 = ["","Alice fell down down down!.. down into, the hole.", "There was only one hole for Alice to fall down into"]
    
    
    single_list = []
    for wordlist in wordlist2:
        for word in wordlist.split(" "):
            no_punc = word.strip(punctuation)
            lower_case = no_punc.lower()
            newlist = lower_case.split()
            #print(newlist)
            if(len(newlist) > 0):
                single_list.append(newlist[0])
    
    print(single_list)
    #to get the unique
    single_list_unique = set(single_list)
    print(single_list_unique)
    print(len(single_list_unique))
    

    制作:

    ['alice', 'fell', 'down', 'down', 'down', 'down', 'into', 'the', 'hole', 'there', 'was', 'only', 'one', 'hole', 'for', 'alice', 'to', 'fall', 'down', 'into']
    {'there', 'fall', 'fell', 'alice', 'for', 'down', 'was', 'into', 'the', 'to', 'only', 'hole', 'one'}
    13
    

    【讨论】:

    • 我忘了提到一件重要的事情,为了单独处理所有单词,它们存储在wordlist 中,作为单个list 的一个大strings 以便与wordlist,我必须将其作为 list 对象使用。所以运行你的代码@Vass 会导致错误list has no attribute split(因为wordlist 是一个列表)
    • @gavmross,在文本数据位于列表中的位置进行了编辑,该列表修复了您提到的错误(我复制了错误并修复了它)
    • 感谢您的编辑,但仍然无法正常工作。我编辑了我的问题,这样您就可以知道每个单词如何在列表中成为它自己的项目,所以我仍然得到list index out of rangesingle_list.append(newlist[0])。我猜这与newlist 为每个单词更新有关,所以它分配给的最后一个列表是['end'],但绝对不确定。 @瓦斯
    • @gavmross,我对数据wordlist2 = ["","Alice fell down down down!.. down into, the hole.", "There was only one hole for Alice to fall down into"] 进行了修改,它产生了错误,所以存在一个空字符串,可以通过测试长度来修复,将更新
    猜你喜欢
    • 2012-11-02
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2020-01-16
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多