+ 3
Trying to solve animal lifetimes in intermediate python
This is my code: ages = [3, 1, 9, 0.4, 7, 12, 2, 1.7, 5.7, 42, 6.7, 14.5, 21] num=int(input()) res=len(list(filter(lambda x:x>num==0,ages))) print(res) Only one test case seem to come out write
17 odpowiedzi
+ 4
ages = [3, 1, 9, 0.4, 7, 12, 2, 1.7, 5.7, 42, 6.7, 14.5, 21]
num=int(input())
res=len(list(filter(lambda x:x>num,ages)))
print(res) this is the right answer
+ 1
What is the problem?
+ 1
Ya, like I said your lambda is incorrect. Remove the ==0 part from the return value of your lambda.
+ 1
Slick
The object for this practice is to use map and or filter.
+ 1
Slick
Ya I just looked where the practice was in the course. Lol
+ 1
ages = [3, 1, 9, 0.4, 7, 12, 2, 1.7, 5.7, 42, 6.7, 14.5, 21]
a=int(input())
result = len(list(filter(lambda x : x>a,ages)))
print(result)
+ 1
# You have two ways:
ages = [3, 1, 9, 0.4, 7, 12, 2, 1.7, 5.7, 42, 6.7, 14.5, 21]
det = int(input())
def older(x):
return x > det
filter_data = list(filter(older, ages))
print(len(filter_data))
filters = list(filter(lambda x: x > det, ages))
print(len(filters))
0
While I can't see the practice challenge as it is for pro subscription, I can see that the lambda in your filter is most likely incorrect.
x>num==0
This is the same as
x > num and num == 0
Is this what you intended?
0
The problem is that only one test case comes out write
0
No, explain the problem you are trying to solve with code. You have an output so it's right to me. Unless we know some expected inputs and their cooresponding outputs, it will be hard to help.
0
You are analyzing a data set of animals. The data is a list of numbers, which represent the ages of animals.
You need to take a number as input and output how many of the animals are older than the given number.
This is the problem
0
Thanks it worked
0
ChaoticDawg gotcha, that wasn't explained
0
This is what I have tried and it works:
1. you create an number input which has to be an integer
2. you filter out the number of animals over x whose age is greater than your input using lambda that is (lambda x: x> number, age) as shown below:
NB. remember to find the length of the list you have created using len(lits()) function.
ages = [3, 1, 9, 0.4, 7, 12, 2, 1.7, 5.7, 42, 6.7, 14.5, 21]
number=int(input())
animal_number=len(list(filter(lambda x: x>number, ages)))
print(animal_number )
0
s,i=[3,1,9,0.4,7,12,2,1.7,5.7,42,6.7,14.5,21],int(input())
print(len((list(filter(lambda x:x>i,s)))))
- 1
oh okay.
- sort the list.
- find index of searched number.
- then get the length of the list AFTER the index
- 1
def animals(ages):
return
number = int(input())
ages = [3, 1, 9, 0.4, 7, 12, 2, 1.7, 5.7, 42, 6.7, 14.5, 21]
older = list(filter(lambda x: x>number, ages))
print(len(older))