公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
ee.Dictionary.rename
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
重命名字典中的元素。
用法 | 返回 |
---|
Dictionary.rename(from, to, overwrite) | 字典 |
参数 | 类型 | 详细信息 |
---|
此:dictionary | 字典 | |
from | 列表 | 要重命名的键的列表。 |
to | 列表 | “from”参数中列出的键的新名称的列表。必须与“from”列表的长度相同。 |
overwrite | 布尔值,默认值:false | 允许覆盖同名的现有属性。 |
示例
代码编辑器 (JavaScript)
// A dictionary (e.g. results of ee.Image.reduceRegion of an S2 image).
var dict = ee.Dictionary({
B1: 182,
B2: 219,
B3: 443
});
// Define from-to key name lists for selected keys.
var from = ['B2', 'B3'];
var to = ['Band_2', 'Band_3'];
print('Renamed keys', dict.rename(from, to));
print('Overwrite existing key names, e.g. B3 becomes B1',
dict.rename({from: ['B3'], to: ['B1'], overwrite: true}));
Python 设置
如需了解 Python API 和如何使用 geemap
进行交互式开发,请参阅
Python 环境页面。
import ee
import geemap.core as geemap
Colab (Python)
# A dictionary (e.g. results of ee.Image.reduceRegion of an S2 image).
dic = ee.Dictionary({
'B1': 182,
'B2': 219,
'B3': 443
})
# Define from-to key name lists for selected keys.
frm = ['B2', 'B3']
to = ['Band_2', 'Band_3']
print('Renamed keys:', dic.rename(frm, to).getInfo())
dic_overwrite = dic.rename(**{'from': ['B3'], 'to': ['B1'], 'overwrite': True})
print('Overwrite existing key names, e.g. B3 becomes B1:',
dic_overwrite.getInfo())
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe \u003ccode\u003erename()\u003c/code\u003e method allows you to rename keys within an Earth Engine Dictionary object.\u003c/p\u003e\n"],["\u003cp\u003eIt takes two required arguments: \u003ccode\u003efrom\u003c/code\u003e (a list of keys to rename) and \u003ccode\u003eto\u003c/code\u003e (a list of new names corresponding to the \u003ccode\u003efrom\u003c/code\u003e list).\u003c/p\u003e\n"],["\u003cp\u003eThe optional \u003ccode\u003eoverwrite\u003c/code\u003e argument, set to \u003ccode\u003efalse\u003c/code\u003e by default, controls whether existing keys can be overwritten during renaming.\u003c/p\u003e\n"]]],["The content describes how to rename keys within a dictionary using the `Dictionary.rename()` method. This method takes two lists: `from` (keys to be renamed) and `to` (new key names), which must have the same length. The optional `overwrite` parameter (default: `false`) allows replacing existing keys. The function returns the modified dictionary. Examples in JavaScript and Python showcase renaming specific keys and overwriting existing ones.\n"],null,["# ee.Dictionary.rename\n\nRename elements in a dictionary.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|----------------------------------------------|------------|\n| Dictionary.rename`(from, to, `*overwrite*`)` | Dictionary |\n\n| Argument | Type | Details |\n|--------------------|-------------------------|--------------------------------------------------------------------------------------------------------------------|\n| this: `dictionary` | Dictionary | |\n| `from` | List | A list of keys to be renamed. |\n| `to` | List | A list of the new names for the keys listed in the 'from' parameter. Must have the same length as the 'from' list. |\n| `overwrite` | Boolean, default: false | Allow overwriting existing properties with the same name. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// A dictionary (e.g. results of ee.Image.reduceRegion of an S2 image).\nvar dict = ee.Dictionary({\n B1: 182,\n B2: 219,\n B3: 443\n});\n\n// Define from-to key name lists for selected keys.\nvar from = ['B2', 'B3'];\nvar to = ['Band_2', 'Band_3'];\nprint('Renamed keys', dict.rename(from, to));\n\nprint('Overwrite existing key names, e.g. B3 becomes B1',\n dict.rename({from: ['B3'], to: ['B1'], overwrite: true}));\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# A dictionary (e.g. results of ee.Image.reduceRegion of an S2 image).\ndic = ee.Dictionary({\n 'B1': 182,\n 'B2': 219,\n 'B3': 443\n})\n\n# Define from-to key name lists for selected keys.\nfrm = ['B2', 'B3']\nto = ['Band_2', 'Band_3']\nprint('Renamed keys:', dic.rename(frm, to).getInfo())\n\ndic_overwrite = dic.rename(**{'from': ['B3'], 'to': ['B1'], 'overwrite': True})\nprint('Overwrite existing key names, e.g. B3 becomes B1:',\n dic_overwrite.getInfo())\n```"]]