37,720
社区成员
发帖
与我相关
我的任务
分享
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> myseq = '''[a:1,b:2,c:3]
[a:3,b:3,c:8]
[a:7,c:4,m:7,r:4]
[a:2,c:4,m:6,r:4]
[a:3,b:2,c:4,o:5]'''
>>> from collections import OrderedDict
>>> def func(dic, line):
key = tuple(map(lambda x:x.split(':')[0], line.strip('[]').split(',')))
if key not in dic:
dic[key] = line
return dic
>>> ','.join(reduce(func, myseq.splitlines(), OrderedDict()).values())
'[a:1,b:2,c:3],[a:7,c:4,m:7,r:4],[a:3,b:2,c:4,o:5]'
>>>