Patrice
Patrice

Reputation: 209

float argument must be a string or a number

I have data like this

xmax [[111.34999999999999], [111.3], [111.2], [111.09999999999999], [111.05], [111.05]]

I want xmax[0]

if i write

print xmax[0]

I have [111.34999999999999]

I would like to have 111.34999999999999

Upvotes: 0

Views: 441

Answers (3)

piRSquared
piRSquared

Reputation: 294308

Few ideas.

Option 0
This is agnostic of the depth of the nesting.

def first(x):
    try:
        return first(x[0])
    except:
        return x

first(xmax)

111.35

Option 1
This assumes one layer of nesting and flattens it. It's not efficient as it gets an entire flattened list just to return the first element.

[x for y in xmax for x in y][0]

111.35

Option 2
This also assumes one layer of nesting but is efficient in getting the first element.

from cytoolz import concat

next(concat(xmax))

111.35

Upvotes: 1

japester
japester

Reputation: 56

You need to index the nested list:

print xmax[0][0]
>>> 111.34999999999999

Upvotes: 0

Avión
Avión

Reputation: 8386

You have a list of list so in order to get just the first value you have to:

print xmax[0][0]

Upvotes: 2

Related Questions