Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- num = int(input())
- numbers = []
- for i in range(num):
- numbers.append(int(input()))
- command = input()
- if command == 'even':
- numbers = [x for x in numbers if x % 2 == 0]
- elif command == 'odd':
- numbers = [x for x in numbers if x % 2 == 1]
- elif command == 'positive':
- numbers = [x for x in numbers if x >= 0]
- elif command == 'negative':
- numbers = [x for x in numbers if x < 0]
- print(numbers)
- ИЛИ:
- num = int(input())
- numbers = []
- for i in range(num):
- numbers.append(int(input()))
- command = input()
- numbers = [x for x in numbers if (x % 2 == 0 if command == 'even' else x % 2 == 1 if command == 'odd' else x < 0 if command == 'negative' else x >= 0)]
- print(numbers)
- Решение с речник:
- num = int(input())
- numbers = []
- for i in range(num):
- numbers.append(int(input()))
- command = input()
- numbers = [x for x in numbers if {'even': x % 2 == 0,
- 'odd': x % 2 == 1,
- 'positive': x >= 0,
- 'negative': x < 0}[command]]
- print(numbers)
Add Comment
Please, Sign In to add comment