我有一個Training_list,它的列表列表例如從列表創建一個子列表
[[1,2,3,4,5,6,7,8,9,10,11,12,13,'<50k'],[1,2,3,4,5,6,7,8,9,10,11,12,13,'<50k'],
[1,2,3,4,5,6,7,8,9,10,11,12,13,'>50k'],[1,2,3,4,5,6,7,8,9,10,11,12,13,'>50k'],
[1,2,3,4,5,6,7,8,9,10,11,12,13,'<50k'],[1,2,3,4,5,6,7,8,9,10,11,12,13,'>50k'],
...
]
我想根據最後一個屬性將這個列表分成兩個子列表。 第一個列表應包含所有的< 50k記錄的under_50k列表清單,例如,
[[1,2,3,4,5,6,7,8,9,10,11,12,13], [1,2,3,4,5,6,7,8,9,10,11,12,13], ...]
第二個列表應包含所有> 50k記錄的over_50k列表列表,例如,
[[1,2,3,4,5,6,7,8,9,10,11,12,13], [1,2,3,4,5,6,7,8,9,10,11,12,13], ...]
一旦兩個列表創建然後我試圖加起來每個索引列表 例如
[1,2,3,4,5,6,7,8,9,10,11,12,13] + [1,2,3,4,5,6,7,8,9,10,11,12,13]
= [2,4,6,8,10,12,14,16,18,20,22,24,26]
似乎可以得到清單的細分工作。
def sums_list():
sums_list = []
try:
for index in range(15):
sums_list.append(under_50k_list[index]+over_50k_list[index])
except:
pass
return(sums_list)
def under_over_lists():
under_50k_list = [0]*14
under_50k_count = 0
over_50k_list = [0]*14
over_50k_count = 0
try:
for row in training_list:
if row[-1].lstrip() == '<=50K':
under_50k_list = sums_list(under_50k_list, row[:-1])
under_50k_count += 1
else:
if row[-1].lstrip() == '>50K':
over_50k_list = sums_list(over_50k_list, row[:-1])
over_50k_count += 1
except:
pass
print(under_50k_list)
return under_over_lists
任何幫助將不勝感激。謝謝 – saggart 2014-11-21 14:33:56
您應該提供額外的標籤,例如這是什麼編程語言。 – user1438038 2014-11-21 14:35:46
對不起,我是新來的堆棧溢出,它的python – saggart 2014-11-21 14:41:49