A simple utility function to merge several lists in a single one, using the functional programming tools:
def merge(*input): return reduce(list.__add__, input, list())
Example:
>>> a = [0, 1, 2] >>> b = [2, 3, 4] >>> c = [4, 5, 6] >>> merge(a, b, c) [0, 1, 2, 2, 3, 4, 4, 5, 6]
The same but removing duplicates:
def merge_uniq(*input): return list(reduce(set.union, input, set()))
The same but removing duplicates later:
def merge_uniq(*input): return list(set(merge(input)))
Other (easier) ways to achieve the same?
Comments
Do you really need it?
You really do not need it:
a.extend(b).extend(c)
Or sorted:
a.extend(b).extend(c).sort()
Or unique:
list(set(a.extend(b).extend(c)))
Miguel Ángel García
http://magmax.org
Otro merge, más fácil aún
http://stackoverflow.com/a/6632253/722624
No soy portavoz de ningún colectivo, grupo o facción. Mi opinión es personal e intransferible.
Python2.6 provides support
For this useful feature with chain
or from a sequence of sequences:
Note that
chain()does not remove duplicates and returns an iterable, not a list.No soy portavoz de ningún colectivo, grupo o facción. Mi opinión es personal e intransferible.
More about list flattening:
More about list flattening: http://utcc.utoronto.ca/~cks/space/blog/python/FlatteningLists
No soy portavoz de ningún colectivo, grupo o facción. Mi opinión es personal e intransferible.
hummmm
I'm curious, why write in english? I think it's the first post I see in that language (in that site of course). That's all.
"aviso: la dereferencia de punteros de tipo castigado romperá las reglas de alias estricto" --GCC 4.3.1
Copypaste
Je pense qu'il a écrit en Anglais parce qu'il l'a copié
Donc, tu as copié cette
Donc, tu as copié cette commentaire aussi?
Nacho
Maybe.
Maybe.
Cree el ladrón...
"Cree el ladrón que todos son de su condición" refranero español
No soy portavoz de ningún colectivo, grupo o facción. Mi opinión es personal e intransferible.
"You know now... ehhh"
It's a requirement of the international projection strategy of CRySoL.
Cheers
No soy portavoz de ningún colectivo, grupo o facción. Mi opinión es personal e intransferible.
Al guano!
...ahora que estoy difundiendo crysol en centroamérica venés vos con esa onda? híjole!
------------------------------------------------------------
$ python -c "print 'VG9udG8gZWwgcXVlIGxvIGxlYSA6KQ==\n'.decode('base64')"
------------------------------------------------------------
Yes, but reading English does
Yes, but reading English does make learning GNULinux a lot easier.
Yes
at least for english people.
No soy portavoz de ningún colectivo, grupo o facción. Mi opinión es personal e intransferible.
vaaaale
habrá que hacer contenido multilíngüe después de todo. ¡¡¡lucas!!!
No soy portavoz de ningún colectivo, grupo o facción. Mi opinión es personal e intransferible.