Spocoman

05. Numbers Filter

Jan 21st, 2022 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. num = int(input())
  2. numbers = []
  3.  
  4. for i in range(num):
  5.     numbers.append(int(input()))
  6.  
  7. command = input()
  8. if command == 'even':
  9.     numbers = [x for x in numbers if x % 2 == 0]
  10. elif command == 'odd':
  11.     numbers = [x for x in numbers if x % 2 == 1]
  12. elif command == 'positive':
  13.     numbers = [x for x in numbers if x >= 0]
  14. elif command == 'negative':
  15.     numbers = [x for x in numbers if x < 0]
  16.  
  17. print(numbers)
  18.  
  19. ИЛИ:
  20.  
  21. num = int(input())
  22. numbers = []
  23.  
  24. for i in range(num):
  25.     numbers.append(int(input()))
  26.  
  27. command = input()
  28.  
  29. 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)]
  30.  
  31. print(numbers)
  32.  
  33.  
  34. Решение с речник:
  35.  
  36. num = int(input())
  37. numbers = []
  38.  
  39. for i in range(num):
  40.     numbers.append(int(input()))
  41.  
  42. command = input()
  43.  
  44. numbers = [x for x in numbers if {'even': x % 2 == 0,
  45.                                   'odd': x % 2 == 1,
  46.                                   'positive': x >= 0,
  47.                                   'negative': x < 0}[command]]
  48.  
  49. print(numbers)
  50.  
Add Comment
Please, Sign In to add comment