473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Iterate using tuple as index

Hello,

Its not obvious to me how to do this. I would like to iterate using a tuple as
an index. Say I have two equivalently sized arrays, what I do now seems
inelegant:

for index, list1_item in enumerate(firstlist):
do_something(list1_item, secondlist[index])

I would like something more like this:

for list1_item, list2_item in (some_kind_of_expression):
do_something(list1_item, list2_item)

Practically, I'm not so sure B is better than A, but the second would be a
little more aesthetic, to me, at least.

Any thoughts on what "some_kind_of_expression" would be?

James

--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
Jul 18 '05 #1
4 1616
Le Thu, 10 Mar 2005 13:12:31 -0800, James Stroud a écrit :
Hello,

Its not obvious to me how to do this. I would like to iterate using a tuple as
an index. Say I have two equivalently sized arrays, what I do now seems
inelegant:

for index, list1_item in enumerate(firstlist):
do_something(list1_item, secondlist[index])

I would like something more like this:

for list1_item, list2_item in (some_kind_of_expression):
do_something(list1_item, list2_item)

Practically, I'm not so sure B is better than A, but the second would be a
little more aesthetic, to me, at least.

Any thoughts on what "some_kind_of_expression" would be? It is called "zip"
for list1_item, list2_item in zip(firstlist, secondlist):
....
try: help(zip)
James

Jul 18 '05 #2
James Stroud wrote:
Hello,

Its not obvious to me how to do this. I would like to iterate using a
tuple as an index. Say I have two equivalently sized arrays, what I do now
seems inelegant:

for index, list1_item in enumerate(firstlist):
do_something(list1_item, secondlist[index])

I would like something more like this:

for list1_item, list2_item in (some_kind_of_expression):
do_something(list1_item, list2_item)

Practically, I'm not so sure B is better than A, but the second would be a
little more aesthetic, to me, at least.

Any thoughts on what "some_kind_of_expression" would be?

James


for item1, item2 in zip(list1, list2):
do_something(item1, item2)

perhaps?

--
Website: www DOT jarmania FULLSTOP com
Jul 18 '05 #3
On Thu, 10 Mar 2005 13:12:31 -0800, James Stroud <js*****@mbi.ucla.edu> wrote:
Hello,

Its not obvious to me how to do this. I would like to iterate using a tuple as
an index. Say I have two equivalently sized arrays, what I do now seems
inelegant:

for index, list1_item in enumerate(firstlist):
do_something(list1_item, secondlist[index])

I would like something more like this:

for list1_item, list2_item in (some_kind_of_expression):
do_something(list1_item, list2_item)

Practically, I'm not so sure B is better than A, but the second would be a
little more aesthetic, to me, at least.

Any thoughts on what "some_kind_of_expression" would be?

zip?
firstlist = [1,2,3]
secondlist = 'a b c'.split()
firstlist, secondlist ([1, 2, 3], ['a', 'b', 'c']) for list1_item, list2_item in zip(firstlist, secondlist): ... print list1_item, list2_item
...
1 a
2 b
3 c

Or if your lists are very long, you could use an iterator from itertools, e.g.,
import itertools
for list1_item, list2_item in itertools.izip(firstlist, secondlist):

... print list1_item, list2_item
...
1 a
2 b
3 c
Regards,
Bengt Richter
Jul 18 '05 #4
James Stroud <js*****@mbi.ucla.edu> wrote:
I would like something more like this:

for list1_item, list2_item in (some_kind_of_expression):
do_something(list1_item, list2_item)


I believe you want:

for list1_item, list2_item in zip (list1, list2):
blah
Jul 18 '05 #5

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

Similar topics

50
by: Will McGugan | last post by:
Hi, Why is that a tuple doesnt have the methods 'count' and 'index'? It seems they could be present on a immutable object. I realise its easy enough to convert the tuple to a list and do this,...
3
by: BJörn Lindqvist | last post by:
I like tuples alot. But in some situations you seem to be forced to access the elements of a tuple via its indexes and that is pretty ugly. For example: # make_color() returns a rgb tuple (r, g,...
15
by: Steve M | last post by:
Hello, I'm trying to figure out the index position of a tuple member. I know the member name, but I need to know the members index position. I know that if I use the statement print tuple that...
5
by: Bas Scheffers | last post by:
Hi, I have a table with about 100K rows, on which I have created a btree index of the type table_name(int, int, int, timestamp). At first postgres was using it for my AND query on all four...
6
by: groups.20.thebriguy | last post by:
I've noticed that there's a few functions that return what appears to be a tuple, but that also has attributes for each item in the tuple. For example, time.localtime() returns a time.time_struct,...
6
by: alainpoint | last post by:
Hello, I have got a problem that i can't readily solve. I want the following: I want to create a supertuple that behaves both as a tuple and as a class. It should do the following:...
13
by: stef mientki | last post by:
hello, I generate dynamically a sequence of values, but this "sequence" could also have length 1 or even length 0. So I get some line in the form of: line = '(2,3,4)' line = '' line = '(2)'...
1
by: prathna | last post by:
Hi .. I have a logic:iterate tag which will display 5 rows each row with a drop downlist and 2 textfields.now by default all the rows will be shown.how do i hide all the rows except the first...
6
by: nielsp | last post by:
Hello! How can I iterate over a tuple (using C++0x)? I tried the following, but that doesn't work: for(int i=0; i<std::tuple_size<T...>::value; ++i) std::get<i>(my_tuple).do_sth(); ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.