以下是我目前的training_list;將列表劃分爲兩個列表
Training_list = [37, 0.7173543689320389, None, None, 9, 0.3351132686084142, 0.05165857605177993, 0.2942961165048544, 0.8373381877022654, 0.6119741100323625, 0, 0, 45, None, ' <=50K'], [46, 0.7173543689320389, None, None, 13, 0.03673139158576052, 0.13199838187702265, 0.3013349514563107, 0.8373381877022654, 0.38802588996763754, 0, 0, 25, None, ' <=50K'], [44, 0.7173543689320389, None, None, 9, 0.1610032362459547, 0.12823624595469255, 0.3013349514563107, 0.8373381877022654, 0.6119741100323625, 0, 0, 40, None, ' >50K']
我需要劃分基礎上,最後一個屬性「< = 50K」或列表分爲兩個列表「> 50K」
我想從兩個列表中排除的最後一個屬性爲好。
我嘗試下面的代碼,我發現了以下:
under 50k list
[]
代碼如下;
over_50k = []
under_50k = []
for row in training_list:
if row[-1].lstrip() == " <=50K":
under_50k.append(row[:-1])
elif row [-1].lstrip() == " >50K":
over_50k.append(row[:-1])
print("under 50k list")
print(under_50k)
任何幫助極大的讚賞。
謝謝比爾奏效 – 2014-11-21 21:26:57