How can I restore it from the zip?

Asked 1 years ago, Updated 1 years ago, 69 views

I use the zip like this

list1 = ['a', 'b', 'c', 'd']
list2 = [1, 2, 3, 4]

result = zip(list1, list2) #[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

The opposite of the zip [('a', 1), ('b', 2), ('c', 3), ('d', 4)] (['a', 'b', 'c', 'd'], [1, 2, 3, 4]) How can I change it?

list python matrix unzip transpose

2022-09-22 14:42

1 Answers

Use the * operator to decompose the zip.

myzip = [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
list1, list2 = zip(*myzip)
#list1, list2 = same as zip (('a', 1), ('b', 2), ('c', 3), ('d', 4))

print list1
print list2

Output:

('a', 'b', 'c', 'd')
(1, 2, 3, 4)


2022-09-22 14:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.