- extend 是 Python 中的一个方法,用于将一个可迭代对象(如列表)的所有元素添加到列表的末尾。它会在原地修改原列表。 以下是一些 extend 的例子:
extend
is a method in Python used to add all elements of an iterable (such as a list) to the end of the list. It modifies the original list in place.
目的是遍历一个对象加入其所有元素到一个list中
- In your code,
camera_targets.extend(batch_camera_targets)
adds all elements frombatch_camera_targets
to thecamera_targets
list.
# 示例 1:将一个列表的元素添加到另一个列表
- In your code, camera_targets.extend(batch_camera_targets) adds all elements from batch_camera_targets to the camera_targets list.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1) # Output: [1, 2, 3, 4, 5, 6]
# 示例 2:将一个元组的元素添加到列表
list1 = [1, 2, 3]
tuple1 = (4, 5, 6)
list1.extend(tuple1)
print(list1) # 输出: [1, 2, 3, 4, 5, 6]
# 示例 3:将一个字符串的字符添加到列表
list1 = [‘a’, ‘b’, ‘c’]
str1 = ‘def’
list1.extend(str1)
print(list1) # 输出: [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
示例 4:将一个集合的元素添加到列表
list1 = [1, 2, 3]
set1 = {4, 5, 6}
list1.extend(set1)
print(list1) # 输出: [1, 2, 3, 4, 5, 6]