473,881 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dict to "flat" list of (key,value)

Hi,

Forgive me if the answer is trivial, but could you tell me how to achieve
the following:

{k1:[v1,v2],k2:v3,...} --> [[k1,v1],[k1,v2],[k2,v3],...]

The subtle point (at least to me) is to "flatten" values that are lists.

Thanks in advance,
Nicolas

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Jul 18 '05 #1
11 14263
On Wed, 30 Jul 2003 21:26:19 +0200, Nicolas Girard <Ni************ *******@removet his.nerim.net> wrote:
Hi,

Forgive me if the answer is trivial, but could you tell me how to achieve
the following:

{k1:[v1,v2],k2:v3,...} --> [[k1,v1],[k1,v2],[k2,v3],...]

The subtle point (at least to me) is to "flatten" values that are lists.

Assuming v3 and others like it can't be references to lists
(or how could you tell it from the format of k1's list?):

(I quoted your names to avoid haing to define them separately)
d = {'k1':['v1','v2'],'k2':'v3'}
flat = []
for k,v in d.items(): ... if isinstance(v,li st):
... for v2 in v:
... flat.append([k,v2])
... else:
... flat.append([k,v])
... flat [['k2', 'v3'], ['k1', 'v1'], ['k1', 'v2']]

Or would you prefer an inscrutable one-liner ;-)

Note that there is no guarantee of any particular order in d.items(),
though you could sort them:
d = {'k1':['v1','v2'],'k2':'v3'}
flat = []
items = d.items()
items.sort() # on keys
for k,v in items: ... if isinstance(v,li st):
# Note that v is a list here, but does have pre-existing order you might want to keep.
# If you wanted to guarantee sorted output of the v2's, you could
# do v.sort() here, though you might surprise others holding references
# to those lists, since they would see the sorting. To avoid that, make a copy first, e.g.,
# v = v[:]; v.sort() #(untested, but I think it should work ;-)

... for v2 in v:
... flat.append([k,v2])
... else:
... flat.append([k,v])
... flat

[['k1', 'v1'], ['k1', 'v2'], ['k2', 'v3']]

Regards,
Bengt Richter
Jul 18 '05 #2
Someone will definitely come up with a better solution than this, but
this could work for you at least as a first try

import types
def flatten(d, iterables=(type s.ListType, types.TupleType )):
rv = []
for k, v in d.items():
if type(v) in iterables:
rv += [[k,x] for x in v]
else:
rv += [[k,v]]
return rv

If you want to change the types of things that get iterated over, just
pass in a second parameter of your own.

Hope that helps,
Brandon

"Nicolas Girard" <Ni************ *******@removet his.nerim.net> wrote in
message news:pa******** *************** ****@removethis .nerim.net...
Hi,

Forgive me if the answer is trivial, but could you tell me how to achieve the following:

{k1:[v1,v2],k2:v3,...} --> [[k1,v1],[k1,v2],[k2,v3],...]

The subtle point (at least to me) is to "flatten" values that are lists.
Thanks in advance,
Nicolas

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World!
100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via

Encryption =---
Jul 18 '05 #3
Nicolas Girard wrote:
Hi,

Forgive me if the answer is trivial, but could you tell me how to achieve
the following:

{k1:[v1,v2],k2:v3,...} --> [[k1,v1],[k1,v2],[k2,v3],...]

The subtle point (at least to me) is to "flatten" values that are lists.
Well, I can get you at least part way, but there is an ambiguity in your
original data structure which causes problems:

1. d.items() will get you

[(k1, [v1, v2]), (k2, v3), ...]

2. Now, you want to expand out the list elements where the second part
of each tuple is a list, which you can do with.

flatten = lambda u: map(lambda x: (u[0], x), u[1])

or, if the double lambda expression is confusing:

def flatten(u):
return map(lambda x: (u[0], x), u[1])

(I've used a tuple (u[0], x) instead of a list, because your [k1, vn]
are always pairs, but you can use a list if you prefer)

Then flatten will take the nested element

(k1, [v1, v2])

and convert it to a list of tuples

[(k1, v1), [k2, v2])]

3. You want to apply flatten to each element of d.items(), which you
can do with

lol = map(flatten, d.items())

which will give you a list of lists of tuples,

4. and then reduce that to a list of tuples with

reduce(operator .add, lol)
Unfortunately, this doesn't quite work with your example above, because
flatten won't work properly when applied to (k2, v3). If v3 is a
sequence, it will instead give you [(k2, v3[0]), (k2, v3[1]), ...],
which is probably not what you want (especially if v3 is a string - try
flatten manually and see what happens). If v3 is not a sequence, you'll
get a TypeError saying that argument 2 to map() must support iteration.

If you *know* that v3 will NEVER be a list, you can modify flatten to
handle the special case like so:

def flatten(u):
if isinstance(u[1], type([])):
return (u[1], [u[2]])
return map(lambda x: (u[0], x), u[1])

Alternatively, if you can modify how the initial dictionary is generated
to ensure that cases where a key has a single value always appear as
k2: [v3] instead of k2: v3, then you can use the steps above without
modification. This is probably better if it is feasible, because your
original data structure is inherently ambiguous unless you know that v3
will never be a list.

David


Thanks in advance,
Nicolas

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


Jul 18 '05 #4
David C. Fox wrote:

Oops - a mistake and an omission
4. and then reduce that to a list of tuples with

reduce(operator .add, lol)
You need to import the operator module first.
def flatten(u):
if isinstance(u[1], type([])):
return (u[1], [u[2]])
return map(lambda x: (u[0], x), u[1])


I got my cases reversed - that should be "if not isinstance...

Jul 18 '05 #5
Nicolas Girard wrote:
Forgive me if the answer is trivial, but could you tell me how to
achieve the following:

{k1:[v1,v2],k2:v3,...} --> [[k1,v1],[k1,v2],[k2,v3],...]

[list(i) for i in d.items()]

Mike


Jul 18 '05 #6
Mike Rovner wrote:
Nicolas Girard wrote:
Forgive me if the answer is trivial, but could you tell me how to
achieve the following:

{k1:[v1,v2],k2:v3,...} --> [[k1,v1],[k1,v2],[k2,v3],...]


[list(i) for i in d.items()]


Sorry, I missed v1,v2 flattening.


Jul 18 '05 #7

"Nicolas Girard" <Ni************ *******@removet his.nerim.net> wrote in message
news:pa******** *************** ****@removethis .nerim.net...
Hi,

Forgive me if the answer is trivial, but could you tell me how to achieve
the following:

{k1:[v1,v2],k2:v3,...} --> [[k1,v1],[k1,v2],[k2,v3],...]

The subtle point (at least to me) is to "flatten" values that are lists.


The other posters answered the question (as asked)
by showing a loop that differentiated the two cases
of inner lists vs single values. One further thought,
is that the original data structure could be improved
by building it so that every value is in a list:

{k1:[v1,v2],k2:[v3],...}

This is typically done by building the values with setdefault:

index = {}
for pagenum in range(len(pages )):
page = pages[pagenum]
for word in page:
index.setdefaul t(word, []).append(pagenu m)

The new structure is much more easily flattened:

[(k,v) for k, values in newstruct.iteri tems() for v in values]
it-all-starts-with-a-good-data-structure-ly yours,
Raymond Hettinger
Jul 18 '05 #8
On Sat, 02 Aug 2003 12:41:19 GMT, "Raymond Hettinger"
<vz******@veriz on.net> wrote:

it-all-starts-with-a-good-data-structure-ly yours,


Amen, brother.

Even worse: I recall seeing code somewhere that had a data structure
like this:

{k1: [v1,v2], k2: v3, k3: None, ...}
instead of
{k1: [v1,v2], k2: [v3], k3: [], ...}

I liked the elegant code example for building a book index. However in
practice the user requirement would be not to have duplicated page
numbers when a word occurs more than once on the same page. If you can
achieve that elegantly, please post it!

Cheers,
John
Jul 18 '05 #9
John Machin wrote:
On Sat, 02 Aug 2003 12:41:19 GMT, "Raymond Hettinger"
<vz******@veriz on.net> wrote:
[a perfectly fine chunk of code]it-all-starts-with-a-good-data-structure-ly yours,


I liked the elegant code example for building a book index. However in
practice the user requirement would be not to have duplicated page
numbers when a word occurs more than once on the same page. If you can
achieve that elegantly, please post it!


OK, I'll bite:
def genindex(pages) :
index = {}
for pagenum in range(len(pages )):
page = pages[pagenum]
for word in page:
index.setdefaul t(word, {})[pagenum] = None

or with 2.3:
def genindex(pages) :
index = {}
for pagenum, page in enumerate(pages ):
for word in page:
index.setdefaul t(word, {})[pagenum] = None
return index

With either one, flattening looks like:
def flatten(index):
flattened = [(word, list(pages)) for word, pages
in index.items()]
for word, pages in flattened:
pages.sort()
flattened.sort( )
return flatten

For fun, try:

book = ['this is a test'.split(),
'this is only a test'.split(),
'had this been a real function, you care a bit'.split()]
for word, pages in flatten(index(b ook)):
print word, pages

-Scott David Daniels
Sc***********@A cm.Org

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2748
by: bennett | last post by:
How can I do a loop from 1 to 5, where on line 1 I print 1 space followed by a button, on line 2 I print 2 spaces followed by a button, on line 3 I print 3 spaces followed by a button, etc.? I tried this code: for (int i = 1; i <= 5 ; ++i) { for (int j = 1; j <= i; ++j) {
3
2762
by: R. P. | last post by:
Subject: XSLT to transform a flat XML file into a structured text file I have an XML file that lists the PDF file segment names and titles of a larger document and looks something like this: <DOCUMENT> ...... ...... some lead elements ...... <SEGMENT_LIST>
1
7739
by: gelangov | last post by:
I am sorry, I am posting this message again, since I did not get any reply. I want to export a table into a "fixed width" file using SQL 2005 import export wizard. This is the version I have: SQL Server 2005 - 9.00.2047.00
15
1839
by: Richard | last post by:
Can anyone recommend a good online resource listing all C keywords, standard system calls, defines etc in a "flat" hierarchy. I wish to set up some bindings in order to bring up "hints and tips" on using such keywords/functions from ISO C in an IDE I am working on. So I would bring up a function and use the current word as the search criteria to locate an online library/faq e.g
17
2793
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are Bad. So maybe we've got over that. I suppose properties could have Bad consequences if a user doesn't know they exist and think that a certain property of an object is just an ordinary attribute. But that applies to
2
3987
by: jehugaleahsa | last post by:
Hello: I have a bunch of related items that are either parents, children or not directly related to each other. In my case, I have a bunch of database tables joined with foreign keys. Another example would be a family tree. I would like to take a flat list of these items and build the hierarchy. So, in other words, I would like to take one element and figure out its relationship to the other items. When I am all done, I
0
9930
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11103
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10403
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9555
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7953
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7113
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5978
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.