ee.Dictionary.rename
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Đổi tên các phần tử trong từ điển.
Cách sử dụng | Giá trị trả về |
---|
Dictionary.rename(from, to, overwrite) | Từ điển |
Đối số | Loại | Thông tin chi tiết |
---|
this: dictionary | Từ điển | |
from | Danh sách | Danh sách các khoá cần đổi tên. |
to | Danh sách | Danh sách tên mới cho các khoá có trong thông số "from". Phải có cùng độ dài với danh sách "from". |
overwrite | Boolean, mặc định: false | Cho phép ghi đè các tài sản hiện có có cùng tên. |
Ví dụ
Trình soạn thảo mã (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}));
Thiết lập Python
Hãy xem trang
Môi trường Python để biết thông tin về API Python và cách sử dụng geemap
cho quá trình phát triển tương tác.
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())
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[[["\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```"]]