如何将数组列表转换为两个列表?

时间:2019-04-25 02:12:19

标签: python arrays list numpy nested

对于下面的数组列表,我试图将其重塑为2个列表的预期结果。在将数字String转换为Float时,如何在Python中实现?非常感谢!

运行此代码的原始列表:

list=[['654546', '1', '1', '1', '1', '2', '1', '1', '1', '8', '2'],['654546', '1', '1', '1', '1', '2', '1', '1', '1', '8', '2'],['695091', '5', '10', '10', '5', '4', '5', '4', '4', '1', '4']]

 list[0]=['654546', '1', '1', '1', '1', '2', '1', '1', '1', '8', '2']
 list[1]=['654546', '1', '1', '1', '3', '2', '1', '1', '1', '1', '2']
 list[2]=['695091', '5', '10', '10', '5', '4', '5', '4', '4', '1', '4']
 ....

预期结果:

listOne: with nested lists of Float numbers from position 1 to 9 (second to last)

listOne[0] = [1, 1, 1, 1, 2, 1, 1, 1, 8]
listOne[1] = [1, 1, 1, 3, 2, 1, 1, 1, 1]
listOne[2] = [5, 10, 10, 5, 4, 5, 4, 4, 1] 

ListTwo: The last item in each list[x] in the original list
ListTwo[0] = [2],
ListTwo[1] =[2],
ListTwo[2] = [4]

2 个答案:

答案 0 :(得分:1)

我认为您想要这样的东西:

list=[['654546', '1', '1', '1', '1', '2', '1', '1', '1', '8', '2'],['654546', '1', '1', '1', '1', '2', '1', '1', '1', '8', '2'],['695091', '5', '10', '10', '5', '4', '5', '4', '4', '1', '4']]

listone=list[0][1:-1]
listtwo_1=list[0][-1]
listtwo_2=list[1][-1]
listtwo=[listtwo_1,listtwo_2]

或:

ListOne=[]
ListTwo=[]
for ii in range(len(list)):
  ListOne.append(list[ii][1:-1])
  ListTwo.append(list[ii][-1])



print(ListOne)
print(ListTwo)

答案 1 :(得分:1)

#_*_ coding:utf-8 _*_

list = [[],[],[]]
list[0]=['654546', '1', '1', '1', '1', '2', '1', '1', '1', '8', '2']
list[1]=['654546', '1', '1', '1', '3', '2', '1', '1', '1', '1', '2']
list[2]=['695091', '5', '10', '10', '5', '4', '5', '4', '4', '1', '4']

listOne = []
listTwo = []

for l in list:
    l = l[1:]
    l = [int(i) for i in l]
    listOne.append(l[0:9])
    listTwo.append(l[-1:])

'''
print(listOne[0])
print(listOne[1])
print(listOne[2])

print(listTwo[0])
print(listTwo[1])
print(listTwo[2])

[1, 1, 1, 1, 2, 1, 1, 1, 8]
[1, 1, 1, 3, 2, 1, 1, 1, 1]
[5, 10, 10, 5, 4, 5, 4, 4, 1]
[2]
[2]
[4]
'''