Python merges 2 lists into a dictionary && string to list

Time:2022-11-25
# The number of key and value is the same
x = [1,2,3,4,5]
y = ['a','b','c','d','e']
res = dict(zip(y,x))
print(res)
print("===================")
# The number of key and value is inconsistent
## more keys
x = [1,2]
y = ['a','b','c']
res = dict(zip(y,x))
print(res)
print("===================")
## less key
x = [1,2,3,4]
y = ['a','b','c']
res = dict(zip(y,x))
print(res)
print("===================")
# convert string to list
temp_str = "['KFC','McDonald's','Wallace']"
temp_list = ast.literal_eval(temp_str)
print(temp_list)
print(type(temp_list))

output result:

{‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4, ‘e’: 5}

{‘a’: 1, ‘b’: 2}

{‘a’: 1, ‘b’: 2, ‘c’: 3}

[‘KFC’, ‘McDonald’, ‘Wallace’]
<class ‘list’>


Original link:https://blog.csdn.net/qq_1724…