Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何对dict数组中的每个值应用相同的函数?_Python_Arrays_Python 3.x_Dictionary - Fatal编程技术网

Python 如何对dict数组中的每个值应用相同的函数?

Python 如何对dict数组中的每个值应用相同的函数?,python,arrays,python-3.x,dictionary,Python,Arrays,Python 3.x,Dictionary,我正在使用Python 3.7。我有一个dict数组,每个数组都有相同的键。比如说 arr_of_dicts = ({"st" : "il"}, {"st" : "IL"}, {"st" : "Il"}) 如何对每个dict中的某个键的值应用相同的函数?例如,我想将大写函数应用于进行上述操作的值 arr_of_dicts = ({"st" : "IL"}, {"st" : "IL"}, {"st" : "IL"}) ?你可能也喜欢熊猫。 如果你做了很多像这样的事情,你应该检查一下它。它可以使

我正在使用Python 3.7。我有一个dict数组,每个数组都有相同的键。比如说

arr_of_dicts = ({"st" : "il"}, {"st" : "IL"}, {"st" : "Il"})
如何对每个dict中的某个键的值应用相同的函数?例如,我想将大写函数应用于进行上述操作的值

arr_of_dicts = ({"st" : "IL"}, {"st" : "IL"}, {"st" : "IL"})

你可能也喜欢熊猫。

如果你做了很多像这样的事情,你应该检查一下它。它可以使这类事情尽快发生:

df=df.apply(你的函数)
这是一个非常强大的工具,如果你喜欢的话,你有时可以制作一些圆滑方便的单行程序

使用,可以使转换函数接受一个键进行转换并返回一个lambda,它充当映射方法。通过使用先前传递的键(
k
)和传入的字典(
d
),您可以返回一个新字典,该字典的值转换为大写:

arr_of_dicts = ({"st" : "il"}, {"st" : "IL"}, {"st" : "Il"})

upper = lambda k: lambda d: {k: d[k].upper()} # your func
res = map(upper('st'), arr_of_dicts) # mapping method

print(list(res))
结果:

[{'st': 'IL'}, {'st': 'IL'}, {'st': 'IL'}]
[{'a': 5, 'st': 'IL'}, {'a': 7, 'st': 'IL'}, {'a': 8, 'st': 'IL'}]

如果您的词典有其他键,则可以先将原始词典扩展到新词典中,然后用大写版本覆盖要转换的键属性,如下所示:

arr_of_dicts = [{"a": 5, "st" : "il"}, {"a": 7, "st" : "IL"}, {"a": 8, "st" : "Il"}]

upper = lambda k: lambda d: {**d, k: d[k].upper()}
res = map(upper('st'), arr_of_dicts) # mapping method

print(list(res))
结果:

[{'st': 'IL'}, {'st': 'IL'}, {'st': 'IL'}]
[{'a': 5, 'st': 'IL'}, {'a': 7, 'st': 'IL'}, {'a': 8, 'st': 'IL'}]

对循环使用
,并且
d['st']=d['st'].upper()
?可以在一行中完成吗?当我在Python控制台中尝试此操作时,“对于arr_of_dicts:d['st']=d['st']].upper()”没有返回任何内容,只是“…”应该分两行完成。没有什么会回来-这将改变原来的口述。如果你打印后,你应该看到更新的口述。我希望得到一个数组的口述,如输入。“tuple(res)”是否具有相同的dicts类型数组?数组指的是列表吗?您可以使用
list(res)
来创建一个列表(这会给您带来您想要的结果吗?)是的,这是一个很好的调用,我的意思是“list”(对Python来说有点陌生)。问题就在这里。如果“arr_of_dicts”var包含'arr_of_dicts=({“a”:5,st:“il”},{“a”:7,st:“il”},{“a”:8,st:“il”}),则运行上述操作将删除其中键不是“st”的所有键值对。是否有一种方法可以保存所有dict的结构,并且只保留与“st”键相关的大写值?