-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexercises_3.py
45 lines (32 loc) · 1.43 KB
/
exercises_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Take a list, say for example this one:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
and write a program that prints out all the elements of the list that are less than 5.
Extras:
- Instead of printing the elements one by one,
make a new list that has all the elements less than 5 from this list in it and print out this new list.
- Write this in one line of Python.
- Ask the user for a number and return a list that contains only elements from the original list that are smaller than the number given by the user.
'''
numbers = input('Please input a group of numbers seperated by ",": ')
number_list = [int(number.strip()) for number in numbers.split(',')]
print('Those are numbers in the group smaller than 5: ')
for number in number_list:
if number < 5:
print(number)
##########################
# For Extras:
##########################
print('Filter numbers smaller than 5 to a new list: ')
# Method 1:
new_number_list = [number for number in number_list if number<5]
print('filter list in Method 1: ', list(new_number_list))
# Method 2:
new_number_list2 = filter(lambda x:x<5, number_list)
print('filter list in Method 2: ', list(new_number_list2))
ceiling = input('Please input a custom filter number: ')
ceiling = int(ceiling)
new_number_list3 = filter(lambda x:x<ceiling, number_list)
print('The following are numbers that are smaller than {0}: '.format(ceiling), list(new_number_list3))