iter.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from pprint import pprint
  2. """
  3. map function
  4. Возвращает итератор
  5. """
  6. def map_test():
  7. a = [-1, 2, -3, 4, -5]
  8. b = list(map(abs, a))
  9. print(b)
  10. # Обойти можно только один раз
  11. for value in map(abs, a):
  12. print(value)
  13. list_strings = ['hello', 'hi', 'good morning']
  14. b = list(map(list, list_strings))
  15. pprint(b)
  16. def increase_3(lst):
  17. return tuple(map(lambda x: 3*x, lst))
  18. def convert_to(values, type_to=int):
  19. return list(map(type_to, values))
  20. def get_letters(st):
  21. return list((map(lambda x: (x.upper(), x.lower()), st)))
  22. persons = [
  23. {
  24. 'birthday': '1983-10-25',
  25. 'job': 'Field seismologist',
  26. 'name': 'Andrew Hernandez',
  27. 'phone': '680-436-8521x3468'
  28. },
  29. {
  30. 'birthday': '1993-10-03',
  31. 'job': 'Pathologist',
  32. 'name': 'Paul Harmon',
  33. 'phone': '602.518.4130'
  34. },
  35. {
  36. 'birthday': '2002-06-11',
  37. 'job': 'Designer, multimedia',
  38. 'name': 'Gregory Flores',
  39. 'phone': '691-498-5303x079'
  40. },
  41. {
  42. 'birthday': '2006-11-28',
  43. 'job': 'Print production planner',
  44. 'name': 'Jodi Garcia',
  45. 'phone': '(471)195-7189'},
  46. {
  47. 'birthday': '2019-12-05',
  48. 'job': 'Warehouse manager',
  49. 'name': 'Elizabeth Cannon',
  50. 'phone': '001-098-434-5950x276'
  51. },
  52. {
  53. 'birthday': '1984-06-12',
  54. 'job': 'Visual merchandiser',
  55. 'name': 'Troy Lucas',
  56. 'phone': '+1-018-070-2288x18433'
  57. },
  58. {
  59. 'birthday': '1993-09-14',
  60. 'job': 'International aid/development worker',
  61. 'name': 'Laurie Sandoval',
  62. 'phone': '2930693269'
  63. },
  64. {
  65. 'birthday': '1999-05-25',
  66. 'job': 'Editor, film/video',
  67. 'name': 'Jack Clark',
  68. 'phone': '8643048473'
  69. },
  70. {
  71. 'birthday': '1985-09-11',
  72. 'job': 'Magazine journalist',
  73. 'name': 'Kimberly Johnson',
  74. 'phone': '+1-583-428-7663'
  75. },
  76. {
  77. 'birthday': '1990-10-07',
  78. 'job': 'Museum/gallery curator',
  79. 'name': 'Austin Liu PhD',
  80. 'phone': '714-879-5250'
  81. }
  82. ]
  83. def phones(lst):
  84. print(list(map(lambda x: x['phone'], lst)))
  85. names = [('Gerald', 'Tucker'), ('Tricia', 'Johnson'), ('Robert', 'Mendez'),
  86. ('Shawn', 'Gutierrez'), ('Gary', 'Ross'), ('Melanie', 'Warren'),
  87. ('Drew', 'May'), ('Jennifer', 'Carroll'), ('Ann', 'Lynn'), ('Ralph', 'Vazquez'),
  88. ('Brittany', 'Erickson'), ('Mark', 'Montoya'), ('Randall', 'Hicks'),
  89. ('Tyler', 'Miller'), ('Bryan', 'Brown'), ('Joshua', 'Sawyer'),
  90. ('Sarah', 'Phillips'), ('Donna', 'Davenport'), ('Rebekah', 'Johnson'),
  91. ('Andrew', 'Reynolds'), ('April', 'Turner'), ('Amanda', 'Ryan'), ('Jennifer', 'Poole'),
  92. ('Jonathan', 'Lane'), ('Laura', 'Stone'), ('Sara', 'Brown'), ('Alexander', 'Johnson'),
  93. ('Emily', 'Phillips'), ('Tyler', 'Smith'), ('Victor', 'Kelly'), ('Audrey', 'Thomas'),
  94. ('Melissa', 'Green'), ('Bethany', 'Holt'), ('Christopher', 'Kerr'), ('Gabrielle', 'Black'),
  95. ('Jennifer', 'Wade'), ('Douglas', 'Horton'), ('Steven', 'Welch'),
  96. ('Terri', 'Thompson'), ('Cassandra', 'Nelson'), ('Andrew', 'Jones'),
  97. ('James', 'Schultz'), ('Richard', 'Castillo'), ('Shaun', 'Logan'),
  98. ('Danielle', 'Lane'), ('Mark', 'Anderson'), ('Charles', 'Shaw'),
  99. ('Derrick', 'Grant'), ('Tracy', 'Pierce'), ('Robert', 'Washington')]
  100. def name(lst):
  101. new_names = list(map(lambda x: x[0] + ' ' + x[1], lst))
  102. print(new_names)
  103. def from_hex_to_rgb(color: str) -> tuple[int, int, int]:
  104. return int(color[1:3], 16), int(color[3:5], 16), int(color[5:7], 16)
  105. def convert_to_rgb(values: list[str]):
  106. return list(map(from_hex_to_rgb, values))
  107. def main():
  108. # map_test()
  109. # print(increase_3([16, -1, 8, 6, -5, -9, 22, 26, 7, -10]))
  110. # numbers = [116, -411, 448, 636, -254, -295, 220, 216, 187, -150]
  111. # print(convert_to(numbers, str))
  112. # numbers = ['-383', '-101', '121', '40', '278', '118', '-462']
  113. # print(convert_to(numbers))
  114. # print(get_letters('TykPe'))
  115. # phones(persons)
  116. # name(names)
  117. print(from_hex_to_rgb('#B22222'))
  118. if __name__ == '__main__':
  119. main()