Converting tuple of lists of variable length into dictionary

Beppe giuseppecostanzi at gmail.com
Sun Oct 18 06:17:18 EDT 2015


hi to everybody, I must turn a tuple of lists into a dictionary.

something like

(['a', 'b', 'c', 'd', 'e', 'f'], ['g', 'h', 'i'], ['l', 'm', 'n', 'o'])

note that the length af each list is variable

must return

a ['b', 'c', 'd', 'e', 'f']
b ['a', 'c', 'd', 'e', 'f']
c ['a', 'b', 'd', 'e', 'f']
d ['a', 'b', 'c', 'e', 'f']
e ['a', 'b', 'c', 'd', 'f']
f ['a', 'b', 'c', 'd', 'e']
g ['h', 'i']
h ['g', 'i']
i ['g', 'h']
l ['m', 'n', 'o']
m ['l', 'n', 'o']
n ['l', 'm', 'o']
o ['l', 'm', 'n']

I have done this but it doesn't convince a lot me.

my_tuple = (['a','b','c','d','e','f'],
            ['g','h','i',],
            ['l','m','n','o'],)

my_dict = {}
LETTERS = []

for letters in my_tuple:
    LETTERS = letters[:]
    for letter in sorted(LETTERS):
        if my_dict.has_key(str(letter)):
            my_dict.append(letters)
        else:
            letters.remove(str(letter))
            my_dict[letter]= letters
            letters = LETTERS[:]
            
for k,v in sorted(my_dict.iteritems()):
    print k,v
    
Suggestions?

regards 
beppe



More information about the Python-list mailing list