我有一些数据
1 2 3 4
3 5 6 7
2 8 9 10
我的密码是
#!/bin/usr/python
file=open("fer.txt","r")
M=[]
P=[]
K=[]
def particle(M,P,K):
for line in file:
y=line.split()
M.append(y)
for i in range(3):
for j in range(4):
P.append(float(M[i][j]))
K.append(P)
return K
print(particle(M,P,K))
然后我得到了
[[1.0, 2.0, 3.0, 4.0, 3.0, 5.0, 6.0, 7.0, 2.0, 8.0, 9.0, 10.0], [1.0,
2.0, 3.0, 4.0, 3.0, 5.0, 6.0, 7.0, 2.0, 8.0, 9.0, 10.0], [1.0, 2.0, 3.0,
4.0, 3.0, 5.0, 6.0, 7.0, 2.0, 8.0, 9.0, 10.0]]
但我有或者我想要这样的东西
[[1.0, 2.0, 3.0, 4.0,], [3.0, 5.0, 6.0, 7.0],[ 2.0, 8.0, 9.0, 10.0]]
编辑为重复标志:我不明白这是一个如何重复的问题我。我在问不同的问题,我也在试图理解为什么我的代码不能工作。你知道吗
第一个答案是正确的,但如果你想在一行:
输出:
使用迭代。可以使用
map
将list的所有元素转换为float。你知道吗例如:
输出:
相关问题 更多 >
编程相关推荐