简体   繁体   English

看不到输出

[英]cannot see the output

items = [("A",5,6),("A",4,5),("A",3,8),("B",6,9),("B",7,4),("C",9,2)]
q2= filter(lambda x: x.count("A"),items)
q4= map(lambda x: x[1], items)
list(q4)
print("Q2 = {}".format(q2))
print("Q4 = {}".format(q4))

I cannot understand why the out put dos not show ?!我不明白为什么输出不显示?! I try to count how many A in the list and print the 2nd items Q2 = Q4 =我尝试计算列表中有多少 A 并打印第二项 Q2 = Q4 =

You are filtering the items correctly but then you should apply your next step on q2 because filter does not change items .您正在正确过滤items ,但是您应该在q2上应用您的下一步,因为过滤器不会更改items It returns the result and you are assigning it to q4 .它返回结果,您将其分配给q4 So:所以:

items = [("A",5,6),("A",4,5),("A",3,8),("B",6,9),("B",7,4),("C",9,2)]
q2= filter(lambda x: x.count("A"),items)
q4= map(lambda x: x[1], q2)
list(q4)
print("Q2 = {}".format(q2))
print("Q4 = {}".format(q4))

For your first question:对于你的第一个问题:

print map(lambda x: x[0],items).count('A')

Or,或者,

print [i[0] for i in items if i[0]=='A']

For your second question:对于你的第二个问题:

print map(lambda x:x[1],items)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM