跳至內容
選單
此問題已被標幟
3 回覆
6806 瀏覽次數

here is my python list when i debug it

list: [[4, 1, False], [4, 2, False], [4, 3, False], [4, 4, False], [4, 5, False]]

now i need to convert it to this type list

list: [1,2,3,4,5]

please advice me to implement this.

頭像
捨棄
最佳答案

Why not use a one-liner?

>>> source = [[4, 1, False], [4, 2, False], [4, 3, False], [4, 4, False], [4, 5, False]]
>>> result = [x[1] for x in source]
>>> print result
[1, 2, 3, 4, 5]
頭像
捨棄

intelligent ! but for the visibility of code .!!

作者

thanks dear Daniel..seems its more easier :-)

最佳答案

hey priyanka do one think :

 list=[[4, 1, False], [4, 2, False], [4, 3, False], [4, 4, False], [4, 5, False]]
 m=[]
 for i in range(0,len(list)):
     m.extend(list[0])
 m=[4, 1, False, 4, 2, False, 4, 3, False, 4, 4, False, 4, 5, False]

thanks

頭像
捨棄
最佳答案

Hi;

list= [[4, 1, False], [4, 2, False], [4, 3, False], [4, 4, False], [4, 5, False]]      
my_list=[]
for object in list : 
    my_list.append(object[1])


print my_list # give [1, 2, 3, 4, 5]
頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
0
3月 25
1178
4
4月 24
173760
0
12月 23
1944
5
7月 25
226936
1
12月 22
3008