To step through a list, the python style is avoid an explicit index.
But what if the same hidden index is to be used for more than one list
for example:-
for key,value in listKeys,listValues :
newdict[key]=value
won't work as it is a tuple of lists, as opposed to a list of tuples.
Is there an elegant solution to this? Is there a way to merge lists
into a list of tuples to allow moving through multiple lists, or is
the for i in range(len(listkeys)): the only solution?
Any suggestions? 8 1310 ia*************@saltmob.com schrieb:
To step through a list, the python style is avoid an explicit index.
But what if the same hidden index is to be used for more than one list
for example:-
for key,value in listKeys,listValues :
newdict[key]=value
won't work as it is a tuple of lists, as opposed to a list of tuples.
Is there an elegant solution to this? Is there a way to merge lists
into a list of tuples to allow moving through multiple lists, or is
the for i in range(len(listkeys)): the only solution?
Any suggestions?
for a, b in zip(lista, listb):
...
Diez ia*************@saltmob.com wrote:
To step through a list, the python style is avoid an explicit index.
But what if the same hidden index is to be used for more than one list
for example:-
for key,value in listKeys,listValues :
newdict[key]=value
won't work as it is a tuple of lists, as opposed to a list of tuples.
Is there an elegant solution to this? Is there a way to merge lists
into a list of tuples to allow moving through multiple lists, or is
the for i in range(len(listkeys)): the only solution?
Any suggestions?
zip() creates a list of tuples, or better, itertools.izip() lazily creates
tuples as you go.
Peter ia*************@saltmob.com wrote:
To step through a list, the python style is avoid an explicit index.
But what if the same hidden index is to be used for more than one list
for example:-
for key,value in listKeys,listValues :
newdict[key]=value
won't work as it is a tuple of lists, as opposed to a list of tuples.
Is there an elegant solution to this? Is there a way to merge lists
into a list of tuples to allow moving through multiple lists, or is
the for i in range(len(listkeys)): the only solution?
Any suggestions?
Yes. The builtin function zip does just that: merging separate lists
into a list of tuples.
See: http://docs.python.org/lib/built-in-funcs.html#l2h-81
Gary Herron
On May 10, 4:20 pm, "Diez B. Roggisch" <d...@nospam.web.dewrote:
for a, b in zip(lista, listb):
...
You don't even need the for loop nowadays. Just pass the zipped list
to a dictionary constructor thusly:
newdict = dict(zip(listKeys,listValues))
Asun
thank you everybody....very well answered.....just one question
remains....
where do i find documentation on zip ...i was looking for a function
like this, but could not even find a relevant list of functions!!
On May 10, 6:00 pm, ian.team.pyt...@saltmob.com wrote:
thank you everybody....very well answered.....just one question
remains....
where do i find documentation on zip ...i was looking for a function
like this, but could not even find a relevant list of functions!!
ooops...even that was answered. again, thanks
On May 10, 6:51 am, ian.team.pyt...@saltmob.com wrote:
....
into a list of tuples to allow moving through multiple lists, or is
the for i in range(len(listkeys)): the only solution?
Any suggestions?
For the specific case of indexing lists, the following is cleaner than
the 'for i in range...' solution above, and works in cases where
zipping the lists may not be appropriate:
for i, item in enumerate(mylist):
print "%s) My item: %s; My other item: %s" % (i, item,
my_non_iterable_object.thing_at(i))
--
Ant. ia*************@saltmob.com a écrit :
To step through a list, the python style is avoid an explicit index.
But what if the same hidden index is to be used for more than one list
for example:-
for key,value in listKeys,listValues :
newdict[key]=value
newdict = dict(zip(listKeys, listValues)) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ville Vainio |
last post by:
I don't know if you have seen this before, but here goes:
http://text.userlinux.com/white_paper.html
There is a jab at Python, though, mentioning that Ruby is more
"refined".
--
Ville...
|
by: Thomas Reichelt |
last post by:
Moin,
short question: is there any language combining the syntax, flexibility and
great programming experience of Python with static typing? Is there a
project to add static typing to Python?
...
|
by: Lad |
last post by:
Is anyone capable of providing Python advantages over PHP if there are
any?
Cheers,
L.
|
by: Sunburned Surveyor |
last post by:
I was thinking of a way I could make writing Python Class Files a
little less painful. I was considering a Ptyhon script that read a
file with a list of property names and method names and then...
|
by: Derek Martin |
last post by:
On Fri, Jul 18, 2008 at 12:21:49PM -0700, mark floyd wrote:
One wonders why... :)
Even if Python didn't offer a way to write a for loop in a similar
fashion (someone else replied about...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |