Python|将字典转换为元组列表
给定一个字典,编写一个Python程序将给定的字典转换为元组列表。
例子:
Input: { 'Geeks': 10, 'for': 12, 'Geek': 31 }
Output : [ ('Geeks', 10), ('for', 12), ('Geek', 31) ]
Input: { 'dict': 11, 'to': 22, 'list_of_tup': 33}
Output : [ ('dict', 11), ('to', 22), ('list_of_tup', 33) ]
以下是将字典转换为元组列表的各种方法。
方法#1:使用列表推导
Python3
# Python code to convert dictionary into list of tuples
# Initialization of dictionary
dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }
# Converting into list of tuple
list = [(k, v) for k, v in dict.items()]
# Printing list of tuple
print(list)
Python3
# Python code to convert dictionary into list of tuples
# Initialization of dictionary
dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }
# Converting into list of tuple
list = list(dict.items())
# Printing list of tuple
print(list)
Python3
# Python code to convert dictionary into list of tuples
# Initialization of dictionary
dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }
# Using zip
list = zip(dict.keys(), dict.values())
# Converting from zip object to list object
list = list(list)
# Printing list
print(list)
Python3
# Python code to convert dictionary into list of tuples
# Initialization of dictionary
dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }
# Initialization of empty list
list = []
# Iteration
for i in dict:
k = (i, dict[i])
list.append(k)
# Printing list
print(list)
Python3
# Python code to convert dictionary into list of tuples
# Initialization of dictionary
dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }
# Importing
import collections
# Converting
list_of_tuple = collections.namedtuple('List', 'name value')
lists = list(list_of_tuple(*item) for item in dict.items())
# Printing list
print(lists)
输出:
[('Geek', 31), ('for', 12), ('Geeks', 10)]
方法 #2:使用 items()
Python3
# Python code to convert dictionary into list of tuples
# Initialization of dictionary
dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }
# Converting into list of tuple
list = list(dict.items())
# Printing list of tuple
print(list)
输出:
[('for', 12), ('Geeks', 10), ('Geek', 31)]
方法 #3:使用 zip
Python3
# Python code to convert dictionary into list of tuples
# Initialization of dictionary
dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }
# Using zip
list = zip(dict.keys(), dict.values())
# Converting from zip object to list object
list = list(list)
# Printing list
print(list)
输出:
[('Geek', 31), ('Geeks', 10), ('for', 12)]
方法#4:使用迭代
Python3
# Python code to convert dictionary into list of tuples
# Initialization of dictionary
dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }
# Initialization of empty list
list = []
# Iteration
for i in dict:
k = (i, dict[i])
list.append(k)
# Printing list
print(list)
输出:
[('Geeks', 10), ('for', 12), ('Geek', 31)]
方法#5:使用集合
Python3
# Python code to convert dictionary into list of tuples
# Initialization of dictionary
dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }
# Importing
import collections
# Converting
list_of_tuple = collections.namedtuple('List', 'name value')
lists = list(list_of_tuple(*item) for item in dict.items())
# Printing list
print(lists)
输出:
[List(name='for', value=12), List(name='Geek', value=31), List(name='Geeks', value=10)]