Python 为字典中的每个数值添加一个值的函数
编写一个函数,为字典中的每个数值添加一个。如果值不是数字(即不是int或float),则不要添加任何内容,而是保持相同的值。函数应将字典作为参数,并返回修改后的字典 请特别注意使用items()遍历字典中的键和值。与“items()”一起使用的模式如下:Python 为字典中的每个数值添加一个值的函数,python,dictionary,Python,Dictionary,编写一个函数,为字典中的每个数值添加一个。如果值不是数字(即不是int或float),则不要添加任何内容,而是保持相同的值。函数应将字典作为参数,并返回修改后的字典 请特别注意使用items()遍历字典中的键和值。与“items()”一起使用的模式如下: 这是我目前所得到的,但它不起作用。我是初学者。如何一次修改字典中的所有值?我认为您目前缺少的是检查类型的isinstance,即 if not (isinstance(v, int) or isinstance(v, float)): 就实际
这是我目前所得到的,但它不起作用。我是初学者。如何一次修改字典中的所有值?我认为您目前缺少的是检查类型的
isinstance
,即
if not (isinstance(v, int) or isinstance(v, float)):
就实际值分配而言,您可以执行以下操作:
dictionary[k] = new_v
def addtovalue(dictionary):
"Add one to each numeric value of the dictionary, return the dict"
for k,v in dictionary.items():
if not (isinstance(v, int) or isinstance(v, float)):
print(v)
else:
new_v = v + 1
print(new_v)
dictionary[k] = new_v
return dictionary
dictionary = { 'fred': 3.3, 'marie': '5', 'jean': 14, 'angus': 44, 'amine': 'blue'}
addtovalue(dictionary)
# {'fred': 4.3, 'marie': '5', 'jean': 15, 'angus': 45, 'amine': 'blue'}
def addtovalue(dictionary):
"Add one to each numeric value of the dictionary, return the dict"
for k,v in dictionary.items():
if (isinstance(v, int) or isinstance(v, float)):
dictionary.update({k: v+1})
return dictionary
或
此外,您可能已经意识到了这一点,但要使代码包含在函数中,您需要缩进函数中所需的代码。最后,这可能看起来像:
dictionary[k] = new_v
def addtovalue(dictionary):
"Add one to each numeric value of the dictionary, return the dict"
for k,v in dictionary.items():
if not (isinstance(v, int) or isinstance(v, float)):
print(v)
else:
new_v = v + 1
print(new_v)
dictionary[k] = new_v
return dictionary
dictionary = { 'fred': 3.3, 'marie': '5', 'jean': 14, 'angus': 44, 'amine': 'blue'}
addtovalue(dictionary)
# {'fred': 4.3, 'marie': '5', 'jean': 15, 'angus': 45, 'amine': 'blue'}
def addtovalue(dictionary):
"Add one to each numeric value of the dictionary, return the dict"
for k,v in dictionary.items():
if (isinstance(v, int) or isinstance(v, float)):
dictionary.update({k: v+1})
return dictionary
您可以通过以下方式调用:
dictionary[k] = new_v
def addtovalue(dictionary):
"Add one to each numeric value of the dictionary, return the dict"
for k,v in dictionary.items():
if not (isinstance(v, int) or isinstance(v, float)):
print(v)
else:
new_v = v + 1
print(new_v)
dictionary[k] = new_v
return dictionary
dictionary = { 'fred': 3.3, 'marie': '5', 'jean': 14, 'angus': 44, 'amine': 'blue'}
addtovalue(dictionary)
# {'fred': 4.3, 'marie': '5', 'jean': 15, 'angus': 45, 'amine': 'blue'}
def addtovalue(dictionary):
"Add one to each numeric value of the dictionary, return the dict"
for k,v in dictionary.items():
if (isinstance(v, int) or isinstance(v, float)):
dictionary.update({k: v+1})
return dictionary
正如@Jonas Palačionis所指出的,你可以将其简化为:
dictionary[k] = new_v
def addtovalue(dictionary):
"Add one to each numeric value of the dictionary, return the dict"
for k,v in dictionary.items():
if not (isinstance(v, int) or isinstance(v, float)):
print(v)
else:
new_v = v + 1
print(new_v)
dictionary[k] = new_v
return dictionary
dictionary = { 'fred': 3.3, 'marie': '5', 'jean': 14, 'angus': 44, 'amine': 'blue'}
addtovalue(dictionary)
# {'fred': 4.3, 'marie': '5', 'jean': 15, 'angus': 45, 'amine': 'blue'}
def addtovalue(dictionary):
"Add one to each numeric value of the dictionary, return the dict"
for k,v in dictionary.items():
if (isinstance(v, int) or isinstance(v, float)):
dictionary.update({k: v+1})
return dictionary
使用字典理解和数字,你可以简化更多。数字
,就像@rasjani指出的那样,如下所示:
import numbers
def addtovalue(dictionary):
"Add one to each numeric value of the dictionary, return the dict"
dictionary.update({k: v+1
for (k,v) in d.items()
if isinstance(v, numbers.Number)})
return dictionary
您不会“一次修改字典中的所有值”,而是一个键一个键地修改它们。修改给定键的值的简单方法是将值分配给键:
d = {"a":1, "b": 2}
d["a"] = 42
print(d)
因此,这里需要的不是打印更新的值,而是更新dict本身
还有几件事:
if v is not float or int:
这是大错特错。首先,要对变量进行类型检查,您不需要对类型进行身份测试,您可以使用isinstance(var,cls)
(推荐)或type(var)is cls
(如果您确实想将测试限制在确切的类中)。然后,表达式“x==y或z”不比较“x”和“y”,然后比较“z”。它的计算结果为“(x==y)或(z)”,这是完全不同的:
>>> x = 42
>>> x == 33 or 22
22
>>> x == 42 or 22
True
>>> x == 22 or 42
42
>>>
因此,您必须编写显式比较:
>>> x == 22 or x == 42
True
>>> x == 42 or x == 22
True
>>> x == 33 or x == 22
False
>>>
最后,重新阅读您的规范,它们明确告诉您1/您应该处理传递给函数的dict,2/您应该从函数返回修改后的dict。使用以下方法:
dictionary = {'fred': 3.3, 'marie': '5',
'jean': 14, 'angus': 44, 'amine': 'blue'}
def new_dict(my_dict):
for k, v in my_dict.items():
if (isinstance(v, int) or isinstance(v, float)):
my_dict.update({k: v+1})
return my_dict
print(new_dict(dictionary))
您可以遍历字典,检查每对的值是否为int
或float
,如果是,您可以使用.update({k,v+1})
您的任务要求您编写一个函数,而不是编写一个函数。可能有更高效、更易于阅读的解决方案,但dict理解可用于生成单独的dict,该dict更新了所有数值,然后新的dict可以更新为现有的dict:
import numbers
d = { 'fred': 3.3, 'marie': '5', 'jean': 14, 'angus': 44, 'amine': 'blue'}
d.update({ k: v+1 for (k,v) in d.items() if isinstance(v, numbers.Number) })
结果如下所示:
>>> print(d)
{'fred': 4.3, 'marie': '5', 'jean': 15, 'angus': 45, 'amine': 'blue'}
>>>