PYTHON: 我的字典是:
abc = {[
["buy", "contract_type"], ["apple", "product"], ["from", "o"],
["Alex", "trader_name"], ["heeb", "trader_name"], ["of", "o"],
["APPLE", "counter_party"], ["INC", "counter_party"]]}
我想要的是, 基本上在字典中隐含相似键的值
new_abc = {[
["buy", "contract_type"], ["apple", "product"], ["from", "o"],
["Alex heeb", "trader_name"], ["of", "o"],
["APPLE INC", "counter_party"]]}
答案 0 :(得分:0)
尝试一下:
d = {}
for a, b in abc:
d.setdefault(b, []).append(a)
new_abc = [ [ ' '.join(d.pop(b)), b ] for a, b in abc if b in d ]
但是正如@Sayse已经指出的那样,这还将结合'o'
值'from'
和'of'
:
[['buy', 'contract_type'], ['apple', 'product'], ['from of', 'o'],
['Alex heeb', 'trader_name'], ['APPLE INC', 'counter_party']]
答案 1 :(得分:0)
尝试一下:
from itertools import groupby
abc = [
["buy", "contract_type"], ["apple", "product"], ["from", "o"],
["Alex", "trader_name"], ["heeb", "trader_name"], ["of", "o"],
["APPLE", "counter_party"], ["INC", "counter_party"]]
new= []
for k, g in groupby(sorted(abc, key=lambda x:x[1]),key =lambda x:x[1]):
new.append(['',k])
for i in g:
new[-1][0] += i[0]+ ' '
new
将是您的预期输出:
[['buy ', 'contract_type'],
['APPLE INC ', 'counter_party'],
['from of ', 'o'],
['apple ', 'product'],
['Alex heeb ', 'trader_name']]