Skip to content

Instantly share code, notes, and snippets.

Created March 20, 2017 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/c6225d9299726b47ae4e03189ad8ad0f to your computer and use it in GitHub Desktop.
Save anonymous/c6225d9299726b47ae4e03189ad8ad0f to your computer and use it in GitHub Desktop.
キーの重複した辞書の比較
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import copy
class DuplicatedDict():
'''
DuplicatedDict([(key1, value1), (key2, value2),...])
'''
def __init__(self, list):
keys = []
values = []
for (i, j) in list:
keys.append(i)
values.append(j)
self.keys = keys
self.values = values
def comp(self, target):
data = copy.deepcopy(self)
for key in target.keys:
indexes = [i for i, x in enumerate(self.keys) if x == key]
if indexes == []:
continue
for i in indexes:
data.values[i] += ':' + target.values[target.keys.index(key)]
return data
def disp(self):
for (key, value) in zip(self.keys, self.values):
print str(key) + ':' + value + ' ',
print '\n',
def main():
a = DuplicatedDict([(1, 'a'), (2, 'b'), (3, 'c'), (1, 'd')])
a.disp()
b = DuplicatedDict([(1, 'e'), (2, 'f')])
b.disp()
c = a.comp(b)
c.disp()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment