2 回答
TA贡献1815条经验 获得超6个赞
nested = [['0.3, 0.4, 0.2', '0.5, 0.1, 0.3'], ['0.7, 0.4, 0.2'], ['0.4, 0.1, 0.3']]
new_nested = [[float(number) for strings in sublist for number in strings.split(', ')] for sublist in nested]
print(new_nested)
new_nested = list()
for sublist in nested:
sublist_new_nested = list()
for strings in sublist:
for number in strings.split(', '):
sublist_new_nested.append(float(number))
new_nested.append(sublist_new_nested)
print(new_nested)
输出:
[[0.3, 0.4, 0.2, 0.5, 0.1, 0.3], [0.7, 0.4, 0.2], [0.4, 0.1, 0.3]]
[[0.3, 0.4, 0.2, 0.5, 0.1, 0.3], [0.7, 0.4, 0.2], [0.4, 0.1, 0.3]]
TA贡献1878条经验 获得超4个赞
result = [[float(t) for s in sublist for t in s.split(', ')] for sublist in nested]
这相当于
result = []
for sublist in nested:
inner = []
for s in sublist:
for t in s.split(', '):
inner.append(float(t))
result.append(inner)
添加回答
举报