我有一个字典,其中数字为键,字符串列表为值。例如:
my_dict = {
1: ['bush', 'barck obama', 'general motors corporation'],
2: ['george bush', 'obama'],
3: ['general motors', 'george w. bush']
}
我希望的是对每个键的每个列表中的每个项目进行比较,如果该项目是另一个项目的子字符串,则将其更改为更长的字符串。所以,这有点像非常肮脏的共指消解。我真的无法理解如何做到这一点。下面是我想法的伪代码:
for key, value in dict:
for item in value:
if item is substring of other item in any other key, value:
item = other item
因此,最终我的字典将会像这样:
my_dict = {
1: ['george w. bush', 'barck obama', 'general motors corporation'],
2: ['george w. bush', 'barck obama'],
3: ['general motors corporation', 'george w. bush']
}
如果我没有清楚地表达问题,对此感到抱歉。
g. w. bush
不会变成george w. bush
。 - AChampion