如何解决通过功能合并用户输入中的两个字典
我是Python的新手,我正在编写一个函数,该函数合并了来自不同用户输入的两个字典。它有效,但是在我看来,我的代码既不必要又冗长。有没有办法使它更简单和流畅?这里的代码:
key1 = int(input("Give an integer as first key"))
key2 = int(input("Give an integer as second key"))
value1 = input("Give a a first value")
value2 = input("Give a second value")
class_list1 = {}
class_list2 = {}
class_list1[key1] = value1
class_list2[key2] = value2
def merge_dictionaries(x,y):
z = {**x,**y}
print("The merged dictionary is : ")
return z
print(merge_dictionaries(class_list1,class_list2))
输出:
Give an integer as first key 1
Give an integer as second key 2
Give a a first value value1
Give a second value value2
The merged dictionary is :
{1: 'value1',2: 'value2'}
解决方法
尝试以下
key1 = int(input("Give an integer as first key"))
key2 = int(input("Give an integer as second key"))
x= y= {}
x[key1]=input("Give a a first value")
y[key2]=input("Give a second value")
print({**x,**y})
,
您可以将用户输入直接保存在字典中而无需合并:
userStorage = {}
for inputNum in range(2):
# Temporary variables
_key,_value = None,None
while not (_key and _value):
# both variables must have a value!
# 'validate' at least the key as integer
try:
_key = int(input("Give an integer as key#%d:" % inputNum))
except:
print("No integer entered!")
continue
_value = input("Give a value for key#%d:" % inputNum)
if _key and _value:
userStorage[_key] = _value
break
print(userStorage)
输出:
Give an integer as key#0:asd
No integer entered!
Give an integer as key#0:9
Give a value for key#0:foo
Give an integer as key#1:10
Give a value for key#1:bar
{9: 'foo',10: 'bar'}
请注意,“最短”版本可能是(但现在代码容易出现错误的用户输入):
userInput = lambda x,y: int(input("Give an integer as key#%d:" % x)) if y == 0 else input("Give a value for key#%d:" % x)
userStorage = {userInput(x,0): userInput(x,1) for x in range(2)}
print(userStorage)
输出:
Give an integer as key#0:87
Give a value for key#0:foo
Give an integer as key#1:88
Give a value for key#1:baz
{87: 'foo',88: 'baz'}
,
您的代码不太长,没有被重构。
看一下代码重构。
创建一个用于获取输入的功能,一个用于创建字典并使用主要功能的功能
def merge_dictionaries(x,y):
z = {**x,**y}
print("The merged dictionary is : {0} ".format(z))
return z
def main:
class_list1,class_list2 = function_input_dictionary() #function that returns dicts
key1,key2 = function_input_key() #function that returns input of key
value1,value2 = function_input_value() # function that returns values
print(merge_dictionaries(class_list1,class_list2))
#look for design patterns and double return of parameters
,
以下是对原始文档的一些修整(简化功能并在可能的情况下包装input
行):
class_list1 = {}
class_list2 = {}
key1 = int(input("Give an integer as first key"))
key2 = int(input("Give an integer as second key"))
class_list1[key1] = input("Give a a first value")
class_list2[key2] = input("Give a second value")
def merge_dictionaries(x,y):
return {**x,**y}
print("The merged dictionary is : {}".format(merge_dictionaries(class_list1,class_list2)))
以上是我在保留原始代码的逻辑的同时要保存行的方法。
如果您愿意采用其他方法,则可以选择一个input
,然后使用split
将其切成创建字典所需的部分。这里的代码更短,并且可以采用可变数量的key:value对,但是input
的结构更复杂:
s = input('Enter int:string pairs,separated by commas\n').split(',')
d = {int(p.split(':')[0]) : p.split(':')[-1] for p in s}
print("The merged dictionary is : {}".format(d))
因此input
中的1:a,2:b,3:c
给出了{1: 'a',2: 'b',3: 'c'}
尝试以下一项,您会发现它很有用,因为在我的示例中,您可以通过修改名为length
的变量来简单地合并两个以上的词典
示例
length = 2
key = 0
value = ''
class_list = {}
for i in range(1,length):
key = int(input(f"Give an integer as {i} key: "))
value = input(f"Give a a {i} value: ")
class_list[key] = value
def merge_dictionaries(x,y):
print("The merged dictionary is : ")
return {**x,**y}
print(merge_dictionaries(class_list,class_list))
,
尝试像这样创建字典,而不要使用其他变量。并且不会更改预期的输出。
class_list1[int(input("Give an integer as first key"))] = input("Give a a first value")
class_list2[int(input("Give an integer as second key"))] = input("Give a second value")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。