[英]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] 我想要xmax [0]
if i write 如果我写
print xmax[0]
I have [111.34999999999999] 我有[111.34999999999999]
I would like to have 111.34999999999999 我想要111.34999999999999
您具有列表列表,因此为了只获取第一个值,您必须:
print xmax[0][0]
Few ideas. 很少的想法。
Option 0 选项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 选项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 选项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
You need to index the nested list: 您需要索引嵌套列表:
print xmax[0][0]
>>> 111.34999999999999
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.