Python - 多个列表的元组列表
在本文中,我们将讨论如何将元组列表转换为多个列表。我们可以使用 map()函数将 lit 元组转换为多个列表
Syntax: map(list, zip(*list_of_tuples)
例子:
Input: [('a', 'b', 'c'), (1,2,3), ('1','3','4')]
Output: ['a', 'b', 'c'], [1, 2, 3], ('1', '3', '4')
示例 1:显示元组列表并显示它们的Python代码。
Python3
# list of tuple for student data
# with both integer and strings
a = [(1, 2,3,4,5),
("sravan","bobby","ojaswi","rohith","Gnanesh"),
(96,89,78,90,78)]
# display
print("Original list of tuple")
print(a)
# list of tuple for student data
# with both integer and strings
a = [(1, 2,3,4,5),
("sravan","bobby","ojaswi","rohith","Gnanesh"),
(96,89,78,90,78)]
# convert list of tuple to multiple lists
data = map(list, zip(*a))
print("")
# display
print("List")
for i in data:
print(i)
Python3
# list of tuple for student
# data with both integer and strings
a = [(1, 2,3,4,5),
("sravan","bobby","ojaswi","rohith","Gnanesh"),
(96,89,78,90,78),
("kakumanu","kakumanu","hyd","hyd","hyd")]
# convert list of tuple to multiple lists
data = map(list, zip(*a))
# display
for i in data:
print(i)
输出:
Original list of tuple
[(1, 2, 3, 4, 5), (‘sravan’, ‘bobby’, ‘ojaswi’, ‘rohith’, ‘Gnanesh’), (96, 89, 78, 90, 78)]
List
[1, ‘sravan’, 96]
[2, ‘bobby’, 89]
[3, ‘ojaswi’, 78]
[4, ‘rohith’, 90]
[5, ‘Gnanesh’, 78]
示例 2:将元组列表转换为多个列表的Python代码
蟒蛇3
# list of tuple for student
# data with both integer and strings
a = [(1, 2,3,4,5),
("sravan","bobby","ojaswi","rohith","Gnanesh"),
(96,89,78,90,78),
("kakumanu","kakumanu","hyd","hyd","hyd")]
# convert list of tuple to multiple lists
data = map(list, zip(*a))
# display
for i in data:
print(i)
输出:
[1, 'sravan', 96, 'kakumanu']
[2, 'bobby', 89, 'kakumanu']
[3, 'ojaswi', 78, 'hyd']
[4, 'rohith', 90, 'hyd']
[5, 'Gnanesh', 78, 'hyd']