以下是我目前的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']
我需要根据最后一个属性将列表分成两个列表。 &LT; = 50K&#39;或者&#39; &GT; 50K&#39;
我想从两个列表中排除最后一个属性
我尝试了下面的代码,我得到了以下内容:
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 :(得分:1)
lstrip()
。lstrip()
将删除前导空格。 " <=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"]
答案 1 :(得分: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']]
答案 2 :(得分: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']]