2014-03-07 41 views
0

我开始学习Python的2天前,和我被困试图筛选特定的单词

我有50种随机颜色

f = ['black', 'red', 'blue', 'red', 'black', 'red', 'white', 'white', 'orange', 'black', 'orange', 'black', 'red', 'green', 'yellow', 'blue', 'blue', 'purple', 'white', 'yellow', 'green', 'black', 'orange', 'white', 'black', 'blue', 'blue', 'blue', 'orange', 'yellow', 'yellow', 'blue', 'red', 'white', 'yellow', 'blue', 'red', 'yellow', 'yellow', 'white', 'white', 'black', 'purple', 'red', 'orange', 'orange', 'blue', 'orange', 'black', 'red'] 

的清单,我想找回多少一种颜色的有在列表中,所以我尝试使用过滤器和Len

当我尝试

和过滤,

filter(lambda x:'red', f) 

它返回完整列表,所以当我使用len()它给了我50.

我在哪里出错我的过滤器?我试过浏览文档,但似乎无法找到任何东西,只能继续寻找。

任何提示?

我的作业规范规定,

“(优秀)使用地图上的颜色来计算(使用过滤器,并减少或LEN)每种颜色的子任务3.打印结果的结果往往是如何发生 。 “

虽然数量看起来更容易

回答

1

假设你的意思来筛选red所有的值,用filterlambda

>>> filter(lambda x: x != 'red', f) 
['black', 'blue', 'black', 'white', 'white', 'orange', 'black', 'orange', 'black 
', 'green', 'yellow', 'blue', 'blue', 'purple', 'white', 'yellow', 'green', 'bla 
ck', 'orange', 'white', 'black', 'blue', 'blue', 'blue', 'orange', 'yellow', 'ye 
llow', 'blue', 'white', 'yellow', 'blue', 'yellow', 'yellow', 'white', 'white', 
'black', 'purple', 'orange', 'orange', 'blue', 'orange', 'black'] 

使用列表理解:

>>> [x for x in f if x != 'red'] 

或发电机与list()一起表达:

>>> list(x for x in f if x != 'red') 

你与

filter(lambda x:'red', f) 

遇到的问题是,你不'red'比较f的元素。本质上,该函数返回一个新列表,其中包含来自f的所有元素。以上任何方法都将过滤列表中的所有'red'值。

如果你想用这种方法来获得'red'元素(而不是list.count你应该),那么数量:

>>> len(filter(lambda x: x == 'red', f)) 
8 
1

你的lambda函数实际上没有做任何的比较:它只是返回字符串'red'每次都是True,所以没有项目被过滤掉。您需要实际字符串以一个最被传递到拉姆达,即x比较:

filter(lambda x: x == 'red', f) 
+0

+1该死的是一个简单的错误.... – dhali

1

我想取回多少一种颜色出现在列表中,

您不必过滤,创建一个新列表,然后查找它的长度。只需使用list.count功能,这样

print f.count('red') 
1

你是故意使用filterlambda如果你想获得的项目数,您可以使用list类型的count方法:

>>> f = ['black', 'red', 'blue', 'red', 'black', 'red', 'white', 'white', 'orange', 'black', 'orange', 'black', 'red', 'green', 'yellow', 'blue', 'blue', 'purple', 'white', 'yellow', 'green', 'black', 'orange', 'white', 'black', 'blue', 'blue', 'blue', 'orange', 'yellow', 'yellow', 'blue', 'red', 'white', 'yellow', 'blue', 'red', 'yellow', 'yellow', 'white', 'white', 'black', 'purple', 'red', 'orange', 'orange', 'blue', 'orange', 'black', 'red'] 
>>> f.count("red") 
8 
1

字典将是最适合你的情况imho :)

f = ['黑色','蓝色','黑色','白色','白色','橙色','黑色','橙色' ,“黑色”,“绿色”,“黄色”,“蓝色”,“蓝色”,“紫色”,“白色” ,'黄','绿','黑','橙','白','黑','蓝','蓝','蓝','橙','黄','黄' ,'blue','white','yellow','blue','yellow','yellow','white','white','black','purple','orange','orange','蓝”,‘橙’,‘黑’]

dictionary = {} 
for i in f: 
    if i in dictionary: 
     dictionary[i] += 1 
    else: 
     dictionary[i] = 0 
1

count是好的一种颜色,但如果你想查询多次:

>>> from collections import defaultdict 

>>> def get_as_count_dict(ls): 
     res = defaultdict(int) 
     for word in ls: 
      res[word] += 1 
     return res 


>>> r = get_as_count_dict(['red', 'red', 'blue', 'pink']) 
>>> r['red'] 
2 

>>> r['nope'] 
0 

或者,我们可以直接使用Counter

>>> from collections import Counter 

>>> r = Counter(['red', 'red', 'blue', 'pink']) 
>>> r['red'] 
2 

>>> r['nope'] 
0 
+0

工程出色!,但不想复制粘贴你的答案将阅读文档 – dhali