You can also do it in a more pythonic way but without generators :
# a = [[1,5,2], 8, 4] # l = [] # for item in a: # if isinstance(item, (int, long)): # l.append(item) # else: # l+=item # print dict([(item,i+1) for (i,item) in enumerate(l)])
It works in the same conditions as your original code (no nested lists)
A few other things :
- you don't have to type a comma for one-item lists : x = [x] works - you probably confused with tuples where you must do x=(x,)
- instead of
# for w in [y for y in x]:
just do
# for w in x:
- for "i = i+1" there is a shortcut : i+=1 (see "l+=item" above)
Regards, Pierre -- http://mail.python.org/mailman/listinfo/python-list