Code: Alles auswählen
In [1]: a = {1:{11:111}, 2:{22:222}, 3:{}}
In [1]: print [i.values() for i in a.itervalues() if len(i) > 0]
[[111], [222]]
[111, 222]
mfg
Twilo
Code: Alles auswählen
In [1]: a = {1:{11:111}, 2:{22:222}, 3:{}}
In [1]: print [i.values() for i in a.itervalues() if len(i) > 0]
[[111], [222]]
Code: Alles auswählen
>>> from itertools import chain
>>> a = {1:{11:111}, 2:{22:222}, 3:{}}
>>> list(chain.from_iterable(a[i].values() for i in a))
[111, 222]
unter Python 2.4 scheint es die methode from_iterable nicht zu gebennumerix hat geschrieben:Code: Alles auswählen
>>> from itertools import chain >>> a = {1:{11:111}, 2:{22:222}, 3:{}} >>> list(chain.from_iterable(a[i].values() for i in a)) [111, 222]
Code: Alles auswählen
Python 2.4.6 (#2, Feb 18 2009, 04:39:14)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from itertools import chain
>>> a = {1:{11:111}, 2:{22:222}, 3:{}}
>>> list(chain.from_iterable(a[i].values() for i in a))
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: type object 'itertools.chain' has no attribute 'from_iterable'
>>>
Stimmt. Dann eben so:Twilo hat geschrieben:unter Python 2.4 scheint es die methode from_iterable nicht zu geben
Code: Alles auswählen
>>> from itertools import chain
>>> a = {1:{11:111}, 2:{22:222}, 3:{}}
>>> list(chain(*[a[i].values() for i in a]))
[111, 222]
Code: Alles auswählen
for b in list(chain.from_iterable(a[i].values() for i in a)):
xyz.append(b)
Code: Alles auswählen
b = list(chain.from_iterable(a[i].values() for i in a))
xyz.extend(b)
Code: Alles auswählen
xyz.extend(chain.from_iterable(a[i].values() for i in a))