1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> inputs = ("JZFJIZJDZJ A", "ZDJZZFJZIZ A", "ZFKZOFZJK B", "ZFJZFOPZF B", "KFZOFJZFZ B", "FIOZJOFIJZ C")
>>> inputs
('JZFJIZJDZJ A', 'ZDJZZFJZIZ A', 'ZFKZOFZJK B', 'ZFJZFOPZF B', 'KFZOFJZFZ B', 'FIOZJOFIJZ C')
>>> inputs = [i.split() for i in inputs] # je découpe selon l'espace
>>> inputs
[['JZFJIZJDZJ', 'A'], ['ZDJZZFJZIZ', 'A'], ['ZFKZOFZJK', 'B'], ['ZFJZFOPZF', 'B'], ['KFZOFJZFZ', 'B'], ['FIOZJOFIJZ', 'C']]
>>> result = dict() # j'initialise un dico vide
>>> for string, id in inputs: # j'itère sur mes couples (chaîne, ID)
... if string not in result: # si le dico n'a pas encore d'entrée pour cette chaîne, on l'initialise avec une liste vide
... result[string] = []
... result[string].append(id) # on ajoute l'iD dans la liste
...
>>> result
{'FIOZJOFIJZ': ['C'], 'ZFKZOFZJK': ['B'], 'KFZOFJZFZ': ['B'], 'JZFJIZJDZJ': ['A'], 'ZDJZZFJZIZ': ['A'], 'ZFJZFOPZF': ['B']} |