Python3, using zip and map on nested list

This is the place for queries that don't fit in any of the other categories.

Python3, using zip and map on nested list

Postby hrs » Sat May 25, 2013 10:40 pm

I've got a list with values that contains user input from a gui. It looks something like this but longer:
Code: Select all
my_list = [['Pressure', 101325, 'Pa'], ['Temperature', 300, 'K'], ['Speed', 100, 'm/s']]

Now I want to get the numerical value of each list item. This works:
Code: Select all
list(map(float, list(zip(*my_list))[1]))

but I'm not too happy with it. Is this just how it is with Python3 or is there a better way?

edit: I think the numerical values are actually strings. They are retrieved from a tkinter entry using the .get() method. In case that makes any difference.
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: Python3, using zip and map on nested list

Postby metulburr » Sat May 25, 2013 10:50 pm

but I'm not too happy with it.

if the data maintains that format, where the int is index 1 of the nested list, than you could just use a simple for loop.
Code: Select all
my_list = [['Pressure', 101325, 'Pa'], ['Temperature', 300, 'K'], ['Speed', 100, 'm/s']]

for index in my_list:
    print(index[1])
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1122
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Python3, using zip and map on nested list

Postby hrs » Sat May 25, 2013 11:08 pm

Well, I need the values for further processing so I would need
Code: Select all
my_list = [['Pressure', 101325, 'Pa'], ['Temperature', 300, 'K'], ['Speed', 100, 'm/s']]

return_vals = []
for index in my_list:
    return_vals.append(index[1])

So that's 3 lines. The input is in blocks which I would like to keep separate so I need to perform this 4 times. What would have been 4 clean one liners in python2 is now 4 less clean lines in python3
Code: Select all
        vals1 = list(map(float, list(zip(*self.list1))[1]))
        vals2 = list(map(float, list(zip(*self.list2))[1]))
        vals3 = list(map(float, list(zip(*self.list3))[1]))
        vals4 = list(map(float, list(zip(*self.list4))[1]))
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: Python3, using zip and map on nested list

Postby metulburr » Sat May 25, 2013 11:28 pm

So that's 3 lines. The input is in blocks which I would like to keep separate so I need to perform this 4 times. What would have been 4 clean one liners in python2 is now 4 less clean lines in python3

So your just looking for one liner code? If so, then just convert the for loop 3 lines into a list comp

Code: Select all
my_list = [['Pressure', 101325, 'Pa'], ['Temperature', 300, 'K'], ['Speed', 100, 'm/s']]
   
vals = [index[1] for index in my_list]
print(vals)
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1122
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Python3, using zip and map on nested list

Postby hrs » Sat May 25, 2013 11:36 pm

Brilliant, I didn't think of that. That looks much nicer to me.
Thanks.
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests