473,327 Members | 1,920 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,327 software developers and data experts.

Iterating over tuple entries in a list

Hi,

I have the following problem:

I have a list with tuples in it, looking like this:

myList = [(1.1, "file1"), (1.2,"file2), (1.9, "file3")]

(I want to store a number calculated from a file together with the
file.) Than I want to use functions expecting a list of floats. Is there
an easy way to slice myList? I know, that:

myList[2][0]

gives:
1.9

But:

myList[0:2][0]

does _not_ give something like:

[1.1, 1.2, 1.9]

By now I loop over the list, select the numbers and store them in a new
list. But I don't like that way very much. Is there a "better" way?
Best,
Dirk
Jul 18 '05 #1
2 2217
Dirk Zimmermann wrote:
Hi,

I have the following problem:

I have a list with tuples in it, looking like this:

myList = [(1.1, "file1"), (1.2,"file2), (1.9, "file3")]

(I want to store a number calculated from a file together with the
file.) Than I want to use functions expecting a list of floats. Is there
an easy way to slice myList? I know, that:

myList[2][0]

gives:
1.9

But:

myList[0:2][0]

does _not_ give something like:

[1.1, 1.2, 1.9]

By now I loop over the list, select the numbers and store them in a new
list. But I don't like that way very much. Is there a "better" way?


If you are really lazy:
myList = [(1.1, "file1"), (1.2, "file2"), (1.9, "file3")]
zip(*myList)[0] (1.1000000000000001, 1.2, 1.8999999999999999)


Peter

Jul 18 '05 #2
Peter Otten wrote:
If you are really lazy:

myList = [(1.1, "file1"), (1.2, "file2"), (1.9, "file3")]
zip(*myList)[0]
(1.1000000000000001, 1.2, 1.8999999999999999)


Or you could do it:
myList = [(1.1, "file1"), (1.2, "file2"), (1.9, "file3")]
map(lambda x:x[0], myList)

[1.1000000000000001, 1.2, 1.8999999999999999]

--
George
Jul 18 '05 #3

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

Similar topics

9
by: Yomanium Yoth Taripoät II | last post by:
HI, 1) what are the differences between list and tuple? 2) how to concatenate tuple and list? no method, no opérator? 3) im looking the fucking manual, and cant add value in my tuple, when it...
3
by: Patrick von Harsdorf | last post by:
I want to iterate over a collection and delete all unwanted entries. for item in collection: del item of course doesn´t do anything useful. Two things come to mind: a) iterating backwards...
4
by: Fernando Rodríguez | last post by:
Hi, While iterating through a list I'd like to know not just the current element, but also its index. Is there a better way than this: i = 0 newList = for element in aList:...
2
by: Ishwar Rattan | last post by:
I am a little confused about a list and a tuple. Both can have dissimilar data-type elements, can be returned by functions. The only difference that I see is that list is mutable and tuple is...
8
by: Captain Dondo | last post by:
I have an array(?) (sorry, I'm new* to python so I'm probably mangling the terminology) that looks like this: I want to replace every instance of 'tooth.seiner.lan' with 'localhost'. There...
77
by: Nick Maclaren | last post by:
Why doesn't the tuple type have an index method? It seems such a bizarre restriction that there must be some reason for it. Yes, I know it's a fairly rare requirement. Regards, Nick...
0
by: chirnag | last post by:
I have and audit table to collection changes happening on an entity. This will be taken care by triggers. During a batch test, I found that out of 1000 inserts into the main table, I got only 997...
4
by: Samir | last post by:
Is there a way to loop or iterate through a list/tuple in such a way that when you reach the end, you start over at the beginning? For example, suppose I define a list "daysOfWeek" such that: ...
13
by: kj | last post by:
Is there a special pythonic idiom for iterating over a list (or tuple) two elements at a time? I mean, other than for i in range(0, len(a), 2): frobnicate(a, a) ?
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
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...
1
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)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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 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.