zingy
zingy

Reputation: 811

mathematical operations on the contents of a list

I have got a list which is the output of my code and want to carry some more operations in this list. I have written the code to convert the ranges in the input list to a single value with 3 choices.

newlist = [[('s', [(0.0, 0.3), (0.1, 0.8), (0.0, 1.0), (0.0, 0.0), (0.0, 0.5)]), 
            ('aa', [(0.0, 0.3), [0.1, 0.8], (0.0, 1.0), [0.0, 1.0], (0.0, 0.5)])], 
          [('m', [(0.0, 0.0), (0.0, 0.0), (0.1, 0.5), (0.0, 0.8), (0.0, 0.0)]), 
           ('ih', [(0.0, 0.0), (0.1, 0.8), (0.1, 0.5), (0.0, 0.4), (0.0, 0.0)])]] 

e = int(raw_input("\n Choose the energy level of the speaker: \n '1' for low \n '2' for normal \n '3' for high \n"))

if e == 1 :
    pList = [(i[0], [j[0] for j in i[1]]) for i in newlist]

elif e == 2:
    pList = [(i[0], [(float(j[0]) + float(j[1])) / 2.0 for j in i[1]]) for i in newlist]

elif e == 3:
    pList = [(i[0], [j[1] for j in i[1]]) for i in newlist]    

print pList

with choice 1 the output should be as,

pList = [[('s', [(0.0, 0.1, 0.0, 0.0, 0.0)]), 
          ('aa', [(0.0, 0.1, 0.0, 0.0, 0.0)])], 
        [('m', [(0.0, 0.0, 0.1, 0.0, 0.0)]), 
         ('ih', [(0.0, 0.1, 0.1, 0.0, 0.0)])]]  

with choice 2 the output should be,

pList = [[('s', [(0.15, 0.45, 0.5, 0.0, 0.25)]), 
          ('aa', [(0.15, 0.45, 0.5, 0.5, 0.25)])], 
        [('m', [0.0, 0.0, 0.3, 0.4, 0.0)]), 
         ('ih', [(0.0, 0.45, 0.3, 0.2, 0.0)])]] 

and with choice 3 the output should look like,

pList = [[('s', [(0.3, 0.8, 1.0, 0.0, 0.5)]), 
          ('aa', [(0.3, 0.8, 1.0, 1.0, 0.5)])], 
        [('m', [(0.0, 0.0, 0.5, 0.8, 0.0)]), 
         ('ih', [(0.0, 0.8, 0.5, 0.4, 0.0)])]] 

None of the choices is working. I think I have made mistakes with the indices. Choice 2 gives an error as,

"ValueError: invalid literal for float(): a"

Thank you.

Upvotes: 0

Views: 258

Answers (3)

Raymond Hettinger
Raymond Hettinger

Reputation: 226734

It's a simple unpacking change. Replace for i in newlist with for s, i in newlist[e]:

>>> newlist = [[('s', [(0.0, 0.3), (0.1, 0.8), (0.0, 1.0), (0.0, 0.0), (0.0, 0.5)]),
            ('aa', [(0.0, 0.3), [0.1, 0.8], (0.0, 1.0), [0.0, 1.0], (0.0, 0.5)])],
          [('m', [(0.0, 0.0), (0.0, 0.0), (0.1, 0.5), (0.0, 0.8), (0.0, 0.0)]),
           ('ih', [(0.0, 0.0), (0.1, 0.8), (0.1, 0.5), (0.0, 0.4), (0.0, 0.0)])]]
>>> e = 1
>>> [(s, [t[0] for t in lot]) for s, lot in newlist[e]]
[('m', [0.0, 0.0, 0.1, 0.0, 0.0]), ('ih', [0.0, 0.1, 0.1, 0.0, 0.0])]

P.S. This kind of mathematical analysis becomes much more readable if you use named tuples.

Upvotes: 3

meson10
meson10

Reputation: 1954

This should solve your problem:

print [[(j, [(float(x[0]) + float(x[1])) / 2.0 for x in e]) for j,e in i] for i in newlist]

Generated Output is:

~$ python ~/test.py 
[[('s', [0.15, 0.45, 0.5, 0.0, 0.25]), ('aa', [0.15, 0.45, 0.5, 0.5, 0.25])], [('m', [0.0, 0.0, 0.3, 0.4, 0.0]), ('ih', [0.0, 0.45, 0.3, 0.2, 0.0])]]

TIP

Use import pprint; pprint.pprint for pretty printing complex data structures

import pprint; pprint.pprint([[(j, [(float(x[0]) + float(x[1])) / 2.0 for x in e]) for j,e in i] for i in newlist])

Will print it like

~$ python ~/test.py 
[[('s', [0.15, 0.45, 0.5, 0.0, 0.25]), ('aa', [0.15, 0.45, 0.5, 0.5, 0.25])],
 [('m', [0.0, 0.0, 0.3, 0.4, 0.0]), ('ih', [0.0, 0.45, 0.3, 0.2, 0.0])]]

Upvotes: 2

silvado
silvado

Reputation: 18197

So newlist is a list of lists of tuples, and also the desired output is a list of lists of tuples. But your expressions for pList generate lists of tuples. It looks like you just have to wrap one more list comprehension around it.

Upvotes: 0

Related Questions