Hello,
I was trying to create a flattened list of dictionary values where each
value is a list, and I was hoping to do this in some neat functionally
style, in some brief, throwaway line so that it would assume the
insignificance that it deserves in the grand scheme of my program.
I had in mind something like this:
>>interleave([1, 2, 3], [4,5], [7, 8, 9])
[1, 4, 7, 2, 5, 8, 3, 9]
I played for a while with zip(), [some newfangled python keyword, that
I was truly shocked to find has been hiding at the bottom of the list
built in functions since version 2.0], before giving up and going back
to trusty old map(), long celebrated for making code hard to read:
>>map(None, [1, 2, 3], [4,5], [7, 8, 9])
[(1, 4, 7), (2, 5, 8), (3, None, 9)]
This is basically it. It then becomes:
>>filter(None, flatten(map(None, [1, 2, 3], [4,5], [7, 8, 9])))
[1, 4, 7, 2, 5, 8, 3, 9]
filter(None, - my brain parses that automatically now. This is not so
bad. Flatten is snitched from ASPN/Cookbook/Python/Recipe/363051,
thank you Jordan Callicoat, Mike C. Fletcher:
def flatten(l, ltypes=(list, tuple)):
i = 0
while (i < len(l)):
while (isinstance(l[i], ltypes)):
l[i:i+1] = list(l[i])
i += 1
return l
Trouble is then getting map() to play with the result of dict.values().
I only worked this out while writing this post, of course.
Given a dictionary like d = { "a" : [1, 2, 3], "b" : [4, 5], "c" : [7,
8, 9]} - I was hoping to do this:
map(None, d.values())
But instead I (finally worked out I could) do this:
apply(map, tuple([None] + d.values()))
So... my bit of code becomes:
filter(None, flatten(map(None, apply(map, tuple([None] +
d.values())))))
It fits on one line, but it feels far more evil than I set out to be.
The brackets at the end are bad for my epilepsy.
Surely there is there some nice builtin function I have missed?
--
| John J. Lehmann, j1o1h1n(@)gmail.com
+ [lost-in-translation] "People using public transport look stern, and
handbag
+ snatchers increase the ill feeling." A Japanese woman, Junko, told
the paper:
+ "For us, Paris is the dream city. The French are all beautiful and
elegant
+ And then, when we arrive..." 5 1875 j1*****@gmail.com wrote:
[...]
I had in mind something like this:
>>>interleave([1, 2, 3], [4,5], [7, 8, 9])
[1, 4, 7, 2, 5, 8, 3, 9]
[...]
But instead I (finally worked out I could) do this:
apply(map, tuple([None] + d.values()))
apply is deprecated, it should be map(None, *d.values())
So... my bit of code becomes:
filter(None, flatten(map(None, apply(map, tuple([None] +
d.values())))))
It fits on one line, but it feels far more evil than I set out to be.
The brackets at the end are bad for my epilepsy.
Surely there is there some nice builtin function I have missed?
You have missed the "it doesn't need to be a one-liner" motto :)
>>def interleave(*args):
.... result = []
.... for items in map(None, *args):
.... result.extend([x for x in items if x is not None])
.... return result
....
>>interleave([1, 2, 3], [4,5], [7, 8, 9])
[1, 4, 7, 2, 5, 8, 3, 9]
Cheers,
--
Roberto Bonvallet
On 2006-11-21, j1*****@gmail.com <j1*****@gmail.comwrote:
I had in mind something like this:
>>>interleave([1, 2, 3], [4,5], [7, 8, 9])
[1, 4, 7, 2, 5, 8, 3, 9]
[...] before giving up and going back to trusty old map(), long
celebrated for making code hard to read:
>>>map(None, [1, 2, 3], [4,5], [7, 8, 9])
[(1, 4, 7), (2, 5, 8), (3, None, 9)]
This is basically it. It then becomes:
>>>filter(None, flatten(map(None, [1, 2, 3], [4,5], [7, 8, 9])))
[1, 4, 7, 2, 5, 8, 3, 9]
You can use itertools.chain instead of flatten:
>>from itertools import chain filter(None, chain(*map(None, [1, 2, 3], [4, 5], [7, 8, 9])))
[1, 4, 7, 2, 5, 8, 3, 9]
Trouble is then getting map() to play with the result of dict.values().
I only worked this out while writing this post, of course.
Given a dictionary like d = { "a" : [1, 2, 3], "b" : [4, 5], "c" : [7,
8, 9]} - I was hoping to do this:
map(None, d.values())
You need the * to "unpack" the list. It has generally taken the
place of apply.
>>map(None, *d.values())
[(1, 7, 4), (2, 8, 5), (3, 9, None)]
So... my bit of code becomes:
>>filter(None, chain(*map(None, *d.values())))
[1, 7, 4, 2, 8, 5, 3, 9]
I don't think that the specific ordering that's achieved has any
valuable significance. You might be just as happy with the
simpler:
>>list(chain(*d.values()))
[1, 2, 3, 7, 8, 9, 4, 5]
--
Neil Cerutti
Weight Watchers will meet at 7 p.m. Please use large double door at the side
entrance. --Church Bulletin Blooper
d = {"a": [1, 2, 3], "b": [4, 5], "c": [7, 8, 9]}
# Simple solution:
result = []
for l in d.itervalues():
result.extend(l)
print result
# Alternative:
from itertools import chain
print list( chain(*d.itervalues()) )
I don't know what's the faster solution, the first one seem more
readable and it's probably fast enough.
Bye,
bearophile j1*****@gmail.com wrote:
filter(None, - my brain parses that automatically now. This is not so
bad. Flatten is snitched from ASPN/Cookbook/Python/Recipe/363051,
thank you Jordan Callicoat, Mike C. Fletcher:
def flatten(l, ltypes=(list, tuple)):
i = 0
while (i < len(l)):
while (isinstance(l[i], ltypes)):
l[i:i+1] = list(l[i])
i += 1
return l
On a side note, that version of flatten doesn't handle an empty list as
the last element of a list...
>>flatten([1, 2, 3, []])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in flatten
IndexError: list index out of range
You need a try: in there or something...
def flatten(l, ltypes=(list, tuple)):
i = 0
while(i < len(l)):
try:
while(isinstance(l[i], ltypes)):
l[i:i+1] = list(l[i])
i += 1
except IndexError:
pass
return l
noah j1*****@gmail.com wrote:
Hello,
I was trying to create a flattened list of dictionary values where each
value is a list, and I was hoping to do this in some neat functionally
style, in some brief, throwaway line so that it would assume the
insignificance that it deserves in the grand scheme of my program.
I had in mind something like this:
>>>>interleave([1, 2, 3], [4,5], [7, 8, 9])
[1, 4, 7, 2, 5, 8, 3, 9]
I played for a while with zip(), [some newfangled python keyword, that
I was truly shocked to find has been hiding at the bottom of the list
built in functions since version 2.0], before giving up and going back
to trusty old map(), long celebrated for making code hard to read:
>>>>map(None, [1, 2, 3], [4,5], [7, 8, 9])
[(1, 4, 7), (2, 5, 8), (3, None, 9)]
This is basically it. It then becomes:
>>>>filter(None, flatten(map(None, [1, 2, 3], [4,5], [7, 8, 9])))
[1, 4, 7, 2, 5, 8, 3, 9]
filter(None, - my brain parses that automatically now. This is not so
bad. Flatten is snitched from ASPN/Cookbook/Python/Recipe/363051,
thank you Jordan Callicoat, Mike C. Fletcher:
def flatten(l, ltypes=(list, tuple)):
i = 0
while (i < len(l)):
while (isinstance(l[i], ltypes)):
l[i:i+1] = list(l[i])
i += 1
return l
Trouble is then getting map() to play with the result of dict.values().
I only worked this out while writing this post, of course.
Given a dictionary like d = { "a" : [1, 2, 3], "b" : [4, 5], "c" : [7,
8, 9]} - I was hoping to do this:
map(None, d.values())
But instead I (finally worked out I could) do this:
apply(map, tuple([None] + d.values()))
So... my bit of code becomes:
filter(None, flatten(map(None, apply(map, tuple([None] +
d.values())))))
It fits on one line, but it feels far more evil than I set out to be.
The brackets at the end are bad for my epilepsy.
Surely there is there some nice builtin function I have missed?
--
| John J. Lehmann, j1o1h1n(@)gmail.com
+ [lost-in-translation] "People using public transport look stern, and
handbag
+ snatchers increase the ill feeling." A Japanese woman, Junko, told
the paper:
+ "For us, Paris is the dream city. The French are all beautiful and
elegant
+ And then, when we arrive..."
What about :
>>d = { "a" : [1, 2, 3], "b" : [4, 5], "c" : [7, 8, 9]} L=[] for x in d.values(): L.extend(x)
....
>>L
[1, 2, 3, 7, 8, 9, 4, 5]
or a little curious:
>>L=[] map(L.extend, d.values())
[None, None, None]
>>L
[1, 2, 3, 7, 8, 9, 4, 5]
TV This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: brianobush |
last post by:
#
# My problem is that I want to create a
# class, but the variables aren't known
# all at once. So, I use a dictionary to
# store the values in temporarily.
# Then when I have a complete set, I...
|
by: Raymond Hettinger |
last post by:
I would like to get everyone's thoughts on two new dictionary methods:
def count(self, value, qty=1):
try:
self += qty
except KeyError:
self = qty
def appendlist(self, key, *values):
try:
|
by: jg |
last post by:
I was trying to get custom dictionary class that can store generic or
string; So I started with the example given by the visual studio 2005 c#
online help for simpledictionay object
That seem...
|
by: john wright |
last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I
am including the code for the dictionary object.
Here is the error I am getting:
"System.Exception: Complex...
|
by: Andrew Robinson |
last post by:
I need to serialize a dictionary so I can encode the contents. I have the
following working but the size seems large. I am guessing that I am
serializing the entire object not just the data. Is...
|
by: vatamane |
last post by:
This has been bothering me for a while. Just want to find out if it
just me or perhaps others have thought of this too: Why shouldn't the
keyset of a dictionary be represented as a set instead of a...
|
by: Rich Shepard |
last post by:
I need to learn how to process a byte stream from a form reader where each
pair of bytes has meaning according to lookup dictionaries, then use the
values to build an array of rows inserted into a...
|
by: O.B. |
last post by:
I need the ability to parse through the values of a Dictionary and
remove certain ones depending on their attribute values. In the example
below, an InvalidOperationException is thrown in the...
|
by: Gustaf |
last post by:
This is two questions in one really. First, I wonder how to convert the
values in a Dictionary to an array. Here's the dictionary:
private Dictionary<Uri, Schemaschemas = new Dictionary<Uri,...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
header("Location:".$urlback);
Is this the right layout the...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
| |