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

Home Posts Topics Members FAQ

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(first list):
do_something(li st1_item, secondlist[index])

I would like something more like this:

for list1_item, list2_item in (some_kind_of_e xpression):
do_something(li st1_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_e xpression" 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 1639
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(first list):
do_something(li st1_item, secondlist[index])

I would like something more like this:

for list1_item, list2_item in (some_kind_of_e xpression):
do_something(li st1_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_e xpression" 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(first list):
do_something(li st1_item, secondlist[index])

I would like something more like this:

for list1_item, list2_item in (some_kind_of_e xpression):
do_something(li st1_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_e xpression" would be?

James


for item1, item2 in zip(list1, list2):
do_something(it em1, 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.uc la.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(first list):
do_something(li st1_item, secondlist[index])

I would like something more like this:

for list1_item, list2_item in (some_kind_of_e xpression):
do_something(li st1_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_e xpression" 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.uc la.edu> wrote:
I would like something more like this:

for list1_item, list2_item in (some_kind_of_e xpression):
do_something(li st1_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
3698
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, I'm just curious why it is neccesary.. Thanks,
3
1738
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, b). I.e. (255, 0, 0) print "The red value is: ", make_color() Not nice at all. It is maybe OK for a rgb tuple that only has three elements, but for a tuple that has 10+ elements, index access is rediculous. In that case, unpacking wont helpe...
15
4047
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 it will print the contents of that location. What I don't understand is if I know that foo is a member of tuple, how do I get foo's index position. Thanks-in-Advance Steve
5
17330
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 columns, but after dropping it and creating different ones and testing, it suddenly stopped using it. Vaccuuming, reindexing, recreating the table and even recreating the database all didn't help.
6
1723
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, which looks like a tuple but also like a struct. That is, I can do: >>> time.localtime() (2006, 1, 18, 21, 15, 11, 2, 18, 0) >>> time.localtime() 21 >>> time.localtime().tm_hour
6
1570
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: Point=superTuple("x","y","z") # this is a class factory p=Point(4,7,9) assert p.x==p
13
2415
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)' (in fact these are not constant numbers, but all kind of integer
1
2375
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 one.i know how to show/hide a row when its not dynamic.but i dont know how to do it when its logic iterate tag. <logic:iterate indexId="index" id="phoneItem" name="nameForm" property="results"> <% String dropdownType = "results.dropdownType";...
6
8003
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(); Error 1: sorry, unimplemented: cannot expand ‘Listener ...’ into a fixed-length argument list. Error 2: i cannot appear in a constant expression.
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...
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();...
0
5519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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.