473,804 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Iterate through a dictionary of lists one "line" at a time

Here is my code:

listing = {'id': ['a', 'b', 'c'], 'name': ['Joe', 'Jane', 'Bob']}

I need to output:

id name
a Joe
b Jane
c Bob

I could do:

print 'id', 'name'
for id, name in zip(listing['id'], listing['name']): print id, name

but that only works if there are two entries in the dictionary, id and
name, and I know what they are. My problem is I don't know how many of
these entries there will be. Thanks for any help you can give!

Apr 18 '07 #1
4 2056
On Apr 18, 7:39 pm, wswilson <wswil...@gmail .comwrote:
Here is my code:

listing = {'id': ['a', 'b', 'c'], 'name': ['Joe', 'Jane', 'Bob']}

I need to output:

id name
a Joe
b Jane
c Bob

I could do:

print 'id', 'name'
for id, name in zip(listing['id'], listing['name']): print id, name

but that only works if there are two entries in the dictionary, id and
name, and I know what they are. My problem is I don't know how many of
these entries there will be. Thanks for any help you can give!
You can use zip(*sequence_o f_sequences)
eg zip(*[[1,2,3],[4,5,6]) returns [[1,4], [2,5], [3,6]]
(You can think of it as a transposition function)

For example:

keys, item_lists = zip(*listing.it eritems())
print " ".join(keys )
for items in zip(*item_lists ):
print " ".join(item s)

would work.

HTH

--
Arnaud

Apr 18 '07 #2
wswilson wrote:
Here is my code:

listing = {'id': ['a', 'b', 'c'], 'name': ['Joe', 'Jane', 'Bob']}

I need to output:

id name
a Joe
b Jane
c Bob

I could do:

print 'id', 'name'
for id, name in zip(listing['id'], listing['name']): print id, name

but that only works if there are two entries in the dictionary, id and
name, and I know what they are. My problem is I don't know how many of
these entries there will be. Thanks for any help you can give!
>>listing = {'id': ['a', 'b', 'c'], 'name': ['Joe', 'Jane', 'Bob']}
for (id, name) in zip(listing['id'], listing['name']):
... print id, name
...
a Joe
b Jane
c Bob
>>>
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 18 '07 #3
On Apr 18, 2:54 pm, Arnaud Delobelle <arno...@google mail.comwrote:
On Apr 18, 7:39 pm, wswilson <wswil...@gmail .comwrote:
Here is my code:
listing = {'id': ['a', 'b', 'c'], 'name': ['Joe', 'Jane', 'Bob']}
I need to output:
id name
a Joe
b Jane
c Bob
I could do:
print 'id', 'name'
for id, name in zip(listing['id'], listing['name']): print id, name
but that only works if there are two entries in the dictionary, id and
name, and I know what they are. My problem is I don't know how many of
these entries there will be. Thanks for any help you can give!

You can use zip(*sequence_o f_sequences)
eg zip(*[[1,2,3],[4,5,6]) returns [[1,4], [2,5], [3,6]]
(You can think of it as a transposition function)

For example:

keys, item_lists = zip(*listing.it eritems())
print " ".join(keys )
for items in zip(*item_lists ):
print " ".join(item s)

would work.

HTH

--
Arnaud
That works perfectly. Thanks so much.

Apr 18 '07 #4
wswilson a écrit :
Here is my code:

listing = {'id': ['a', 'b', 'c'], 'name': ['Joe', 'Jane', 'Bob']}

I need to output:

id name
a Joe
b Jane
c Bob

I could do:

print 'id', 'name'
for id, name in zip(listing['id'], listing['name']): print id, name

but that only works if there are two entries in the dictionary, id and
name, and I know what they are. My problem is I don't know how many of
these entries there will be. Thanks for any help you can give!

The most simple and generic I see, it could be even more simple if you
don't care of memory usage :

Let's fill a random dict :

In [118]: d=dict(zip((str (e) for e in xrange(10)), ([i**e for e in
xrange(5)] for i in xrange(10))))

In [119]: d
Out[119]:
{'0': [1, 0, 0, 0, 0],
'1': [1, 1, 1, 1, 1],
'2': [1, 2, 4, 8, 16],
'3': [1, 3, 9, 27, 81],
'4': [1, 4, 16, 64, 256],
'5': [1, 5, 25, 125, 625],
'6': [1, 6, 36, 216, 1296],
'7': [1, 7, 49, 343, 2401],
'8': [1, 8, 64, 512, 4096],
'9': [1, 9, 81, 729, 6561]}

go on :

In [146]: sorted_keys = tuple(sorted(d. keys()))

In [147]: from itertools import izip, chain

In [148]: sorted_keys = tuple(sorted(d. keys()))

In [149]: sorted_values = ( d[k] for k in sorted_keys )

In [150]: for vals in chain([sorted_keys], izip(*sorted_va lues)) :
.....: print '%5s'*len(d) % vals
.....:
.....:
0 1 2 3 4 5 6 7 8 9
1 1 1 1 1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9
0 1 4 9 16 25 36 49 64 81
0 1 8 27 64 125 216 343 512 729
0 1 16 81 256 625 1296 2401 4096 6561

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 4 26 88 00 97
Mobile: +33 6 32 77 00 21
Apr 19 '07 #5

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

Similar topics

2
4969
by: leroybt.rm | last post by:
I don't understand why this does not work: <FILE1> test1.py #Import Packages import string # data=0 data=data+1
2
1805
by: Andr? Roberge | last post by:
I want to "import and execute" a python program (input_test.py below) within another program (execute_test.py below) and "watch it" being executed. By watching it, I mean to display the file input_test.py in a window (GUI) and highlighting the line being executed. I am *simulating* this here by printing the line being executed with the corresponding line number and it works as expected for "simple" programs.
3
3015
by: Colleyville Alan | last post by:
I am constructing a SQL command from a function. Some code builds the WHERE clause in a looping structure and passes that as an argument to the SQL-building function. But the results do not always execute as I envisioned they should. When I trap the finished SQL statement in the Immediates window and paste it to a query SQL window, I can see in the QBE why I am not getting what I want. Basically, I want the QBE window to have the OR...
10
1764
by: Rafi B. | last post by:
I'm running this on a Linux/CentOs/cPanel server, trying to add caching to my PHP5 code, using ADOdb. I have two Linux servers, one of them is running perfect, the second one, just crashes without any error, and just gives a blank screen. I narrowed down the problem to: $line.serialize($rs2) Am I missing something maybe in the PHP5 configuration?
4
4147
by: Hexes | last post by:
Ok i am making program wicth has from txt file put lines from first to last. well If i code it like this: ---- nFileNum = FreeFile Open "C:/VB/VB.txt" For Input As nFileNum FreeNum = 1
5
10812
by: Nathan Sokalski | last post by:
I have an ASP.NET application which is giving the following JavaScript error: 'theForm' is undefined However, when I do a View Source one of the <scriptelements is as follows: <script type="text/javascript"> <!-- var theForm = document.forms;
12
1604
by: Dom | last post by:
VB had a "line" control, just a simple line that let you separate controls without the wasted space of a Groupbox. Did CSharp drop this? Dom
4
3095
by: Chris Seymour | last post by:
Hi All, I am working on a python script for my colleague that will walk a directory and search in the different files for a specific string. These pieces I am able to do. What my colleague wants is that when she runs the script the filename is replaced by the current file that is being processed. So instead of seeing a series of files being listed down the page, she only wants to see the file currently being processed. I am not sure...
0
9706
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
10569
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
10325
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10315
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
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...
1
7615
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
6847
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();...
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
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.