![](/img/trans.png)
[英]float() argument must be a string or a number, not 'Float'
[英]float argument must be a string or a number
我有這樣的數據
xmax [[111.34999999999999], [111.3], [111.2], [111.09999999999999], [111.05], [111.05]]
我想要xmax [0]
如果我寫
print xmax[0]
我有[111.34999999999999]
我想要111.34999999999999
您具有列表列表,因此為了只獲取第一個值,您必須:
print xmax[0][0]
很少的想法。
選項0
這與嵌套的深度無關。
def first(x):
try:
return first(x[0])
except:
return x
first(xmax)
111.35
選項1
假設有一層嵌套並將其展平。 這種方法效率不高,因為它只是為了返回第一個元素而獲得了一個完整的扁平化列表。
[x for y in xmax for x in y][0]
111.35
選項2
這也假定有一層嵌套,但是在獲取第一個元素時很有效。
from cytoolz import concat
next(concat(xmax))
111.35
您需要索引嵌套列表:
print xmax[0][0]
>>> 111.34999999999999
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.