zip.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from itertools import zip_longest
  2. def test_zip():
  3. a = [1, 4, 3, 5]
  4. b = [123, 323, 543, 234]
  5. c = 'asdf'
  6. # for i in range(4):
  7. # print(a[i], b[i])
  8. res = zip(a, b, c)
  9. col1, col2, col3 = zip(*res)
  10. print(col1, col2, col3)
  11. for i in res:
  12. print(i)
  13. def test_1():
  14. x = [1, 2, 3]
  15. y = [4, 5, 6]
  16. zipped = zip(x, y, strict=True)
  17. result = list(zipped)
  18. print(result)
  19. # test_1()
  20. def zip_longest_test():
  21. ids = [1, 2, 3, 4]
  22. leaders = ['Elon Mask', 'Tim Cook', 'Bill Gates', 'Yang Zhou']
  23. countries = ('Australia', 'USA')
  24. records = zip_longest(ids, leaders, countries)
  25. print(list(records))
  26. records_2 = zip_longest(ids, leaders, countries, fillvalue='Unknown')
  27. print(list(records_2))
  28. def unzip_test():
  29. record = [(1, 'Elon Mask', 'Australia'),
  30. (2, 'Tim Cook', 'USA'),
  31. (3, 'Bill Gates', 'USA'),
  32. (4, 'Yang Zhou', 'China')]
  33. ids, leaders, countries = zip(*record)
  34. print(ids)
  35. print(leaders)
  36. print(countries)
  37. # unzip_test()
  38. def zip_iter_1():
  39. employee_numbers = [2, 9, 18, 28]
  40. employee_names = ["Valentin", "Leonti", "Andrew", "Sasha"]
  41. salaries = (100, 200, 3000, 450)
  42. for name, number, salary in zip(employee_names, employee_numbers, salaries):
  43. print(name, number, salary)
  44. # zip_iter_1()
  45. def zip_dict():
  46. dict_one = {'name': 'John', 'last_name': 'Doe', 'job': 'Python Consultant'}
  47. dict_two = {'name': 'Jane', 'last_name': 'Doe', 'job': 'Community Manager'}
  48. for (k1, v1), (k2, v2) in zip(dict_one.items(), dict_two.items()):
  49. print(k1, '->', v1)
  50. print(k2, '->', v2)
  51. # zip_dict()
  52. def creat_dict():
  53. employee_numbers = [2, 9, 18]
  54. employee_names = ["Valentin", "Leonti", "Andrew"]
  55. numbers = dict(zip(employee_numbers, employee_names))
  56. employees = dict(zip(employee_names, employee_numbers))
  57. print(numbers)
  58. print('------')
  59. print(employees)
  60. # обновляе словаря
  61. other_num = [50, 63]
  62. other_names = ['Misha', 'Sergey']
  63. numbers.update(zip(other_num, other_names))
  64. print('------')
  65. print(numbers)
  66. # creat_dict()
  67. def zip_comp():
  68. employee_numbers = [2, 9, 18]
  69. employee_names = ["Valentin", "Leonti", "Andrew"]
  70. numbers = {num: name for num, name in zip(employee_numbers, employee_names)}
  71. names = [name[0]*num for num, name in zip(employee_numbers, employee_names)]
  72. print(numbers)
  73. print(names)
  74. # zip_comp()
  75. def zip_matrix():
  76. matrix = [[1, 2, 3, 4], [5, 6, 7, 8]]
  77. matrix_trans = [list(i) for i in zip(*matrix)]
  78. print(matrix_trans)
  79. # zip_matrix()
  80. def task_1():
  81. keys = ['Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety', 'One hundred']
  82. values = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
  83. result = dict(zip(keys, values))
  84. print(result)
  85. # task_1()
  86. def task_2():
  87. employees = ['Pankratiy', 'Innokentiy', 'Anfisa', 'Yaroslava', 'Veniamin',
  88. 'Leonti', 'Daniil', 'Mishka', 'Lidochka',
  89. 'Terenti', 'Vladik', 'Svetka', 'Maks', 'Yura', 'Sergei']
  90. identifiers = [77, 48, 88, 85, 76, 81, 62, 43, 5, 56, 17, 20, 37, 32, 96]
  91. # employees_data = dict(zip(identifiers, employees))
  92. employees_data = dict(zip(sorted(identifiers), sorted(employees)))
  93. print(employees_data)
  94. # task_2()
  95. def combine_strings(a: str, b: str) -> str:
  96. return a + b
  97. def get_sum_two_numbers(a: int, b: int) -> int:
  98. return a + b
  99. def get_sum_three_numbers(a: int, b: int, c: int) -> int:
  100. return a + b + c
  101. def zip_with_function(list_data, f):
  102. combined_list = list(zip(*list_data))
  103. combined_list = list(map(lambda args: f(*args), combined_list))
  104. return combined_list
  105. # zip_with_function([[1, 2, 3], [4, 5, 6], [7, 8, 9]], get_sum_three_numbers)
  106. # zip_with_function([[1, 2, 4], [3, 5, 8]], get_sum_two_numbers)