2014-11-21 158 views
1

以下是我目前的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) 

任何幫助極大的讚賞。

回答

0

我的解決辦法是:

Training_list = [11,22,33,44,55,77,99,0.333,None,"<=50K"],[33,0.3344,88,44,55,11,None,"<=50K"],[22,44,0.334455,11,44,66,">50K"] 

liste =[] 
under_50k =[] 
over_50k =[] 
i=0 
while 1: 
    try: 
     liste = Training_list[i] 
     i+=1 
    except: 
     break 
    if liste[len(liste)-1] == '<=50K': 
     under_50k.append(liste) 
    if liste[len(liste)-1] == '>50K': 
     over_50k.append(liste) 

print("under",under_50k) 
print("over",over_50k) 

而且輸出是:

under [[11, 22, 33, 44, 55, 77, 99, 0.333, None, '<=50K'], [33, 0.3344, 88, 44, 55, 11, None, '<=50K']] 
over [[22, 44, 0.334455, 11, 44, 66, '>50K']] 
1
  1. 您正在使用lstrip()
  2. lstrip()將刪除主要空白。
  3. 字符串" <=50K"具有領先的空白。

所以我們可以嘗試以比較不具有前導空格的字符串:

if row[-1].strip() == "<=50K": 

或許:

under_50k = [row[:-1] for row in training_list if row[-1].strip() == "<=50K"] 
over_50k = [row[:-1] for row in training_list if row[-1].strip() == ">50K"] 
+0

謝謝比爾奏效 – 2014-11-21 21:26:57

1

,如果你想優化:一個是可以在

under_50k = [] 
over_50k = [] 
for x in training_list: 
    if x[-1].strip() == "<=50k": 
     under_50k.append(x[:-1]) 
    if x[-1].strip() == ">=50k": 
     over_50k.append(x[:-1]) 

使用itertools.groupby做:

>>> for x,y in itertools.groupby(sorted(Training_list,key=lambda x:x[1]),key = lambda x:x[-1]=="<=50K" and "under" or "over"): 
...  print x,list(y) 
... 
under [[33, 0.3344, 88, 44, 55, 11, None, '<=50K'], [11, 22, 33, 44, 55, 77, 99, 0.333, None, '<=50K']] 
over [[22, 44, 0.334455, 11, 44, 66, '>50K']]