【问题标题】:Find and Replace of Elements of List in Python 3.0?在 Python 3.0 中查找和替换列表元素?
【发布时间】:2019-02-20 04:12:54
【问题描述】:

我有 3 个大列表 L0L1L2,分别有 106756、106588 和 100 个字。

L0L1 将数据标记化为单词的标记,L2L0L1 列表共有的单词组成。

假设,

L1 = ['newnes', 'imprint', 'elsevier', 'corporate', 'drive', 'suite',
     'burlington', 'usa', 'linacre', 'jordan', 'hill', 'oxford', 'uk', 
     'elsevier', 'inc', 'right', 'reserved', 'exception', 'newness', 'uk', ...]

L2 = ['usa', 'uk', 'hill', 'drive', ... ]

正如您在 L1 列表中看到的那样,有 repetition of the words,例如 'newness''uk'

我需要的是,对于L2 中的每个discovered (found) 字,比如(比如'newness''uk'),我需要用它的modified injected form 替换它,比如在special character 处附加一个special character发现的单词的startend position。 此外,对于发现的单词(L2)的所有实例,应替换为L1 中相同单词的修改版本。例如,

假设,newness 这个词在L1 列表(比如说)中出现了 100 次,而新奇这个词也出现在 L2 中。同样,L2 中也有 100 个单词,L1 中也有多个单词。

然后,转换后的列表应该看起来像这样:

newness ------> $newness$

uk -----------> $uk$ 

...

如何在列表中实现这一点?请帮忙。我也是python的新手。我只是想知道python中是否存在一些命令来实现这一点?我不知道从哪里开始?

【问题讨论】:

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


    【解决方案1】:

    为了对列表中的事物进行计数,python 在其集合模块中提供了一个类似 dict 的 Counter() 类:Doku,它计算 O(n) 中的出现次数并将它们作为字典提供。

    from collections import Counter
    
    
    L1 = ['newnes', 'imprint', 'elsevier', 'corporate', 'drive', 'suite',
         'burlington', 'usa', 'linacre', 'jordan', 'hill', 'oxford', 'uk', 
         'elsevier', 'inc', 'right', 'reserved', 'exception', 'newness', 'uk', ...]
    
    L2 = ['usa', 'uk', 'hill', 'drive', ... ]
    
    
    c = Counter(L1)
    print(c)
    

    输出:

    Counter({'elsevier': 2, 'uk': 2, 'newnes': 1, 'imprint': 1, 'corporate': 1, 
             'drive': 1, 'suite': 1, 'burlington': 1, 'usa': 1, 'linacre': 1, 
             'jordan': 1, 'hill': 1, 'oxford': 1, 'inc': 1, 'right': 1, 'reserved': 1,
             'exception': 1, 'newness': 1, Ellipsis: 1})
    

    它提供了一种方便的方法来将结果排序为元组列表(key, count) 命名为most_common() - 如果您使用第一个,您将获得最常用的单词,您可以将其与列表推导一起使用来修改您的来源列表:

    word,_ = c.most_common()[0]  # get word mos often used
    
    # inplace modification of L1
    L1[:] = [ x if x != word else "#"+word+"#" for x in L1] # use x if not the most used word
    L2[:] = [ x if x != word else "#"+word+"#" for x in L2] # else pre-/append #
    
    print(L1)
    print(L2)
    

    输出:

    ['newnes', 'imprint', '#elsevier#', 'corporate', 'drive', 'suite', 'burlington', 
     'usa', 'linacre', 'jordan', 'hill', 'oxford', 'uk', '#elsevier#', 'inc', 
     'right', 'reserved', 'exception', 'newness', 'uk', Ellipsis]
    
    ['usa', 'uk', 'hill', 'drive', Ellipsis]
    

    Counter 中的项目顺序与原始列表中的顺序相关,L1 中有多个项目,计数为 2 - elsevier 是其中的第一个,因此它也是第一个使用most_common()


    编辑 4 条评论:

    from collections import Counter
    
    L1 = ['newnes', 'imprint', 'elsevier', 'corporate', 'drive', 'suite',
         'burlington', 'usa','imprint', 'linacre', 'jordan', 'hill', 'oxford', 'uk','uk', 
         'elsevier', 'inc', 'right', 'reserved','imprint', 'exception', 'imprint','newness', 'uk', "..."]
    
    L2 = ['usa', 'uk', 'hill', 'drive', "..."]
    
    
    c = Counter(L1) 
    
    
    substs = "#*+~-:;=)(/&%$§!"
    i = 0
    for word,count in c.most_common():
        temp = substs[i]*count # use the i-th char as substitute, apply it count times
        L1[:] = [ x if x != word else temp+word+temp for x in L1] # use x if not the most used word
        L2[:] = [ x if x != word else temp+word+temp for x in L2] # else pre-/append #
        i += 1
        i = i % len(substs) # wrap around
    
    print(L1)
    print(L2)
    

    输出:

    ['~newnes~', '####imprint####', '++elsevier++', '-corporate-', ':drive:', ';suite;', 
     '=burlington=', ')usa)', '####imprint####', '(linacre(', '/jordan/', '&hill&', 
     '%oxford%', '***uk***', '***uk***', '++elsevier++', '$inc$', '§right§', '!reserved!', 
     '####imprint####', '#exception#', '####imprint####', '*newness*', '***uk***', 
     '+...+']
    
    [')usa)', '***uk***', '&hill&', ':drive:', '+...+']
    

    【讨论】:

    • 上面的代码只变换List 1的最上面的词,频率最高。无论频率如何,如何在列表的所有单词中加入相同的内容?我的意思是如何一步转换L1的elsevieruk这两个词?
    • @MishraSiba 查看编辑。不是一步,但您可以遍历 Counter-results 并将您喜欢的任何更改应用于两个列表。对于计数器的所有结果,我使用i-th 字符并在前/后缀之前将其乘以计数。这不是一个步骤,而是一个接一个地修改一个列表的外观,替换一个单词,然后是下一个单词。在循环中使用print(L1) 来查看您的数据更改表单。
    • 你好帕特里克。如何以高效的方式做同样的事情。假设如果我有大数据。将单词等特殊字符注入#word# 需要更多时间。
    • @M_S 你可能需要问一个新问题,如果它是一个不同的问题 - 如果你尝试用这个答案解决它,请详细说明你认为你需要它工作的方式/什么不起作用。
    猜你喜欢
    • 2018-08-18
    • 2011-02-04
    • 2018-08-05
    • 1970-01-01
    • 2015-09-06
    • 2018-01-11
    • 2019-07-04
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多