I have a list of column headings and an array of values:
headings, values = ['a', 'b', 'c'], [1, 2, 3]
I want to construct a dictionary such that d['a'] = 1 and so on.
The first way I tried to do this was:
d = {}
for h,v in headings, values:
d[h] = v
It turns out this doesn't work, but it was worth a try. I ended up
falling back on the more C-like:
for i in range(len(headings)):
d[h[i]] = v[i]
Is there anything somewhat cleaner or more pythonesque I could do
instead? Thanks.
--
Michael T. Babcock
C.T.O., FibreSpeed Ltd. http://www.fibrespeed.net/~mbabcock 2 1490
In article <ma************************************@python.org >,
"Michael T. Babcock" <mb******@fibrespeed.net> wrote: I have a list of column headings and an array of values: headings, values = ['a', 'b', 'c'], [1, 2, 3]
I want to construct a dictionary such that d['a'] = 1 and so on.
d = dict(zip(headings,values))
--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
On Thu, 30 Oct 2003 14:45:48 -0500, "Michael T. Babcock" <mb******@fibrespeed.net> wrote: I have a list of column headings and an array of values: headings, values = ['a', 'b', 'c'], [1, 2, 3]
I want to construct a dictionary such that d['a'] = 1 and so on.
The first way I tried to do this was:
d = {} for h,v in headings, values: d[h] = v
It turns out this doesn't work, but it was worth a try. I ended up
IMO it would be interesting to make that work, but spelling it like a tuple unpacking
assignment with a 'for' in front of it, to make it step through the sequences on the right. E.g,
d = {}
for h,v = headings, values: # illegal now, proposed lazy parallel sequence unpacking
d[h] = v
would work as "expected", i.e., as if
for h,v in itertools.izip(headings,values):
d[h] = v
Regards,
Bengt Richter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: kbass |
last post by:
I am new to Python and I am attempting to retrieve data from a database and
I would like to place this data into a nested dictionary. After placing the
data into a dictionary, I would like to loop...
|
by: Kamilche |
last post by:
I have a routine that I really need, but it slows down processing
significantly. Can you spot any ineffeciencies in the code?
This code makes a critical function of mine run about 7x slower than...
|
by: seberino |
last post by:
I've been reading the beloved Paul Graham's "Hackers and Painters".
He claims he developed a web app at light speed using Lisp and lots
of macros.
It got me curious if Lisp
is inherently faster...
|
by: rudysanford |
last post by:
I just started messing with programming and started with Python. Part
of my first project deals with translating numerical values to letters.
I would like to be able to do the reverse as well,...
|
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: Venkat |
last post by:
Hi,
I would like to have a multiple key dictionary to find a value
based on the given key.
Here is the example to demonstrate what I mean.
|
by: Andrew Robinson |
last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v>
depending on input parameters and options to the method.
1. Is there any way to convert from a dictionary to a list...
|
by: John Nagle |
last post by:
Some faster Python implementations are under development.
JPython has been around for a while, and PyPy and ShedSkin
continue to move forward. It's worth thinking about what slows
down Python...
|
by: mk |
last post by:
Calvin Spealman wrote:
Well, basically nothing except I need to remember I have to do that.
Suppose one does that frequently in a program. It becomes tedious. I
think I will define some helper...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
|
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...
| |