在python3.0中查找和替换列表元素?

2024-04-28 10:01:30 发布

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

我有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替换它,比如在发现的单词的startendposition处附加一个special character。 此外,对于发现的单词的所有实例(在L2),都应该替换为L1中相同单词的修改版本。例如

假设newness这个词在L1列表中出现了100次,newness这个词也出现在L2。类似地,在L2中也有100个单词,它们也出现在L1中,具有多个频率。你知道吗

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

newness ------> $newness$

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

。。。你知道吗

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


Tags: 数据标记l1列表drive单词ukusa
1条回答
网友
1楼 · 发布于 2024-04-28 10:01:30

为了统计列表中的内容,python在其collections模块中提供了一个类似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})

它提供了一种方便的方法,可以将结果排序为名为most_common()的元组列表(key, count)-如果使用第一个元组,则会得到最常用的单词,可以与列表理解一起使用,以修改源列表:

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:', '+...+']

相关问题 更多 >