Python 如何将字典对象值从一个键复制到另一个键?
我必须用输入制作一个字典,它可能包含“键->值1,值2”或“键->键”。 键始终是字符串,值始终是整数,由逗号和空格分隔。 如果给定一个键和值,我必须将这些值存储到给定的键。 如果密钥已经存在,我必须将给定值添加到旧值中。 如果给定一个键和另一个键,我必须将另一个键的值复制到第一个键。 如果另一个键不存在,则必须忽略此输入行。 当我收到命令“end”时,我必须停止读取输入行,并且必须以以下格式打印所有键及其值: {key}==={value1,value2,value3}Python 如何将字典对象值从一个键复制到另一个键?,python,dictionary,copy,extend,Python,Dictionary,Copy,Extend,我必须用输入制作一个字典,它可能包含“键->值1,值2”或“键->键”。 键始终是字符串,值始终是整数,由逗号和空格分隔。 如果给定一个键和值,我必须将这些值存储到给定的键。 如果密钥已经存在,我必须将给定值添加到旧值中。 如果给定一个键和另一个键,我必须将另一个键的值复制到第一个键。 如果另一个键不存在,则必须忽略此输入行。 当我收到命令“end”时,我必须停止读取输入行,并且必须以以下格式打印所有键及其值: {key}==={value1,value2,value3} data=input(
data=input()
dict_ref={}
def为_int(s):
尝试:
int(s)
返回真值
除值错误外:
返回错误
而数据!=“结束”:
列表\数据=数据。拆分(“->”)
名称=列表\数据[0]
值=列表\数据[1]。拆分(“,”)
如果名称不在dict_ref中且为_int(值[0]):
dict_ref[name]=值
dict_ref中的elif值[0]:
dict_ref[name]=dict_ref[value[0]]
在dict_ref中的elif名称和is_int(值[0]):
dict_ref[name].extend(值)
数据=输入()
对于dict_ref中的项目:
打印(f“{item}==”,end=”“)
打印(“,”。连接(dict_ref[项目])
输入:
彼得->1,2,3
ISAC->Peter
彼得->4,5
结束
预期产出:
彼得===1,2,3,4,5
data = input()
dict_ref = {}
def is_int(s):
try:
int(s)
return True
except ValueError:
return False
while data != 'end':
list_data = data.split('->')
key = list_data[0].strip()
values = list_data[1].split(',')
for value in values:
value = value.strip()
if key not in dict_ref.keys() and is_int(value):
dict_ref[key] = [value]
elif key in dict_ref.keys() and is_int(value):
dict_ref[key].append(value)
else:
'''
With Python dictionaries, when you assign a key to the value of another, the refresh is done automatically.
For example, writing in this condition, dict_ref [key] = dict_ref [value], when the while loop will start with a new data value,
dict_ref [key] = dict_ref [value] will be dynamically updated with the new data as well.
That's why you should not make a direct assignment, you have to create a new variable that will contain Peter's values,
then you will assign to the key 'Isacc'.
'''
vals = list() #New List
for val in dict_ref[value]:
vals.append(val)
dict_ref[key] = vals
data = input()
for item in dict_ref:
print(f"{item} === ", end="")
print(", ".join(dict_ref[item]))
Isacc==1,2,3
实际产量:
彼得===1,2,3,4,5
data = input()
dict_ref = {}
def is_int(s):
try:
int(s)
return True
except ValueError:
return False
while data != 'end':
list_data = data.split('->')
key = list_data[0].strip()
values = list_data[1].split(',')
for value in values:
value = value.strip()
if key not in dict_ref.keys() and is_int(value):
dict_ref[key] = [value]
elif key in dict_ref.keys() and is_int(value):
dict_ref[key].append(value)
else:
'''
With Python dictionaries, when you assign a key to the value of another, the refresh is done automatically.
For example, writing in this condition, dict_ref [key] = dict_ref [value], when the while loop will start with a new data value,
dict_ref [key] = dict_ref [value] will be dynamically updated with the new data as well.
That's why you should not make a direct assignment, you have to create a new variable that will contain Peter's values,
then you will assign to the key 'Isacc'.
'''
vals = list() #New List
for val in dict_ref[value]:
vals.append(val)
dict_ref[key] = vals
data = input()
for item in dict_ref:
print(f"{item} === ", end="")
print(", ".join(dict_ref[item]))
Isacc==1,2,3,4,5
data = input()
dict_ref = {}
def is_int(s):
try:
int(s)
return True
except ValueError:
return False
while data != 'end':
list_data = data.split('->')
key = list_data[0].strip()
values = list_data[1].split(',')
for value in values:
value = value.strip()
if key not in dict_ref.keys() and is_int(value):
dict_ref[key] = [value]
elif key in dict_ref.keys() and is_int(value):
dict_ref[key].append(value)
else:
'''
With Python dictionaries, when you assign a key to the value of another, the refresh is done automatically.
For example, writing in this condition, dict_ref [key] = dict_ref [value], when the while loop will start with a new data value,
dict_ref [key] = dict_ref [value] will be dynamically updated with the new data as well.
That's why you should not make a direct assignment, you have to create a new variable that will contain Peter's values,
then you will assign to the key 'Isacc'.
'''
vals = list() #New List
for val in dict_ref[value]:
vals.append(val)
dict_ref[key] = vals
data = input()
for item in dict_ref:
print(f"{item} === ", end="")
print(", ".join(dict_ref[item]))
输入:
彼得->1,2,3
ISAC->Peter
彼得->4,5
结束
输出:
彼得===1,2,3,4,5
data = input()
dict_ref = {}
def is_int(s):
try:
int(s)
return True
except ValueError:
return False
while data != 'end':
list_data = data.split('->')
key = list_data[0].strip()
values = list_data[1].split(',')
for value in values:
value = value.strip()
if key not in dict_ref.keys() and is_int(value):
dict_ref[key] = [value]
elif key in dict_ref.keys() and is_int(value):
dict_ref[key].append(value)
else:
'''
With Python dictionaries, when you assign a key to the value of another, the refresh is done automatically.
For example, writing in this condition, dict_ref [key] = dict_ref [value], when the while loop will start with a new data value,
dict_ref [key] = dict_ref [value] will be dynamically updated with the new data as well.
That's why you should not make a direct assignment, you have to create a new variable that will contain Peter's values,
then you will assign to the key 'Isacc'.
'''
vals = list() #New List
for val in dict_ref[value]:
vals.append(val)
dict_ref[key] = vals
data = input()
for item in dict_ref:
print(f"{item} === ", end="")
print(", ".join(dict_ref[item]))
Isacc===1,2,3“预期输出”有哪些输入?在您的示例输入中,我看不到Peter和Isaac。很抱歉,我添加了它。非常感谢@Sekouba,这解决了我的问题。我只需添加检查dict.ref.keys()中是否存在“值”(如果是字符串),如果不存在则传递。