可迭代对象

对可迭代对象的操作。

函数

adjacent_n_tuples(objects, n)[来源]

返回循环分割为长度为n的元组的Sequence对象。

另请参阅

adjacent_pairs

别名为 n=2

示例

正常用法:

list(adjacent_n_tuples([1, 2, 3, 4], 2))
# returns [(1, 2), (2, 3), (3, 4), (4, 1)]

list(adjacent_n_tuples([1, 2, 3, 4], 3))
# returns [(1, 2, 3), (2, 3, 4), (3, 4, 1), (4, 1, 2)]
Parameters:
  • 对象 (序列)

  • n (int)

Return type:

zip

adjacent_pairs(objects)[source]

adjacent_n_tuples(objects, 2) 的别名。

另请参阅

adjacent_n_tuples

示例

正常用法:

list(adjacent_pairs([1, 2, 3, 4]))
# returns [(1, 2), (2, 3), (3, 4), (4, 1)]
Parameters:

对象 (序列)

Return type:

zip

all_elements_are_instances(iterable, Class)[来源]

如果可迭代对象的所有元素都是 Class 的实例,则返回 True。否则返回 False。

Parameters:

iterable (可迭代对象)

Return type:

布尔

batch_by_property(items, property_func)[来源]

接收一个序列,并返回一个元组列表,(batch, prop) 使得当放入可调用属性函数 property_func 时,一个批次中的所有项目具有相同的输出,并且将这些批次连接在一起将得到原始序列(即顺序被保留)。

示例

正常用法:

batch_by_property([(1, 2), (3, 4), (5, 6, 7), (8, 9)], len)
# returns [([(1, 2), (3, 4)], 2), ([(5, 6, 7)], 3), ([(8, 9)], 2)]
Parameters:
  • items (序列)

  • property_func (可调用)

Return type:

列表[元组[列表, 任意]]

concatenate_lists(*list_of_lists)[source]

将提供的Iterables参数合并为一个列表。

示例

正常用法:

concatenate_lists([1, 2], [3, 4], [5])
# returns [1, 2, 3, 4, 5]
Parameters:

list_of_lists (可迭代对象)

Return type:

列表

hash_obj(obj)[来源]

确定一个哈希值,即使是可能可变的对象。

Parameters:

obj (对象)

Return type:

整数

list_difference_update(l1, l2)[来源]

返回一个包含所有在l1中但不在l2中的元素的列表。

示例

正常用法:

list_difference_update([1, 2, 3, 4], [2, 4])
# returns [1, 3]
Parameters:
  • l1 (可迭代对象)

  • l2 (可迭代对象)

Return type:

列表

list_update(l1, l2)[来源]
Used instead of set.update() to maintain order,

确保从l1中删除重复项,而不是l2。 移除l1和l2的重叠部分,然后不变地连接l2。

示例

正常用法:

list_update([1, 2, 3], [2, 4, 4])
# returns [1, 3, 2, 4, 4]
Parameters:
  • l1 (可迭代对象)

  • l2 (可迭代对象)

Return type:

列表

listify(obj)[source]

智能地将obj转换为列表。

示例

正常用法:

listify('str')   # ['str']
listify((1, 2))  # [1, 2]
listify(len)     # [<built-in function len>]
Return type:

列表

make_even(iterable_1, iterable_2)[source]
Extends the shorter of the two iterables with duplicate values until its

长度等于较长的可迭代对象(优先考虑较早的元素)。

另请参阅

make_even_by_cycling

循环元素而不是优先选择较早的元素

示例

正常用法:

make_even([1, 2], [3, 4, 5, 6])
([1, 1, 2, 2], [3, 4, 5, 6])

make_even([1, 2], [3, 4, 5, 6, 7])
# ([1, 1, 1, 2, 2], [3, 4, 5, 6, 7])
Parameters:
  • iterable_1 (可迭代对象)

  • iterable_2 (可迭代对象)

Return type:

元组[列表, 列表]

make_even_by_cycling(iterable_1, iterable_2)[source]
Extends the shorter of the two iterables with duplicate values until its

长度等于较长的可迭代对象(循环较短的可迭代对象)。

另请参阅

make_even

倾向于较早的元素而不是循环它们

示例

正常用法:

make_even_by_cycling([1, 2], [3, 4, 5, 6])
([1, 2, 1, 2], [3, 4, 5, 6])

make_even_by_cycling([1, 2], [3, 4, 5, 6, 7])
# ([1, 2, 1, 2, 1], [3, 4, 5, 6, 7])
Parameters:
  • iterable_1 (集合)

  • iterable_2 (集合)

Return type:

元组[列表, 列表]

remove_list_redundancies(lst)[source]

用于代替list(set(l))以保持顺序。保留每个元素的最后一次出现。

Parameters:

lst (可逆的)

Return type:

列表

remove_nones(sequence)[来源]

移除 bool(x) 评估为 False 的元素。

示例

正常用法:

remove_nones(['m', '', 'l', 0, 42, False, True])
# ['m', 'l', 42, True]
Parameters:

序列 (可迭代对象)

Return type:

列表

resize_array(nparray, length)[来源]
Extends/truncates nparray so that len(result) == length.

nparray的元素被循环以达到所需的长度。

另请参阅

resize_preserving_order

倾向于较早的元素而不是循环它们

make_even_by_cycling

用于平衡两个可迭代对象的类似循环行为

示例

正常用法:

>>> points = np.array([[1, 2], [3, 4]])
>>> resize_array(points, 1)
array([[1, 2]])
>>> resize_array(points, 3)
array([[1, 2],
       [3, 4],
       [1, 2]])
>>> resize_array(points, 2)
array([[1, 2],
       [3, 4]])
Parameters:
  • nparray (ndarray)

  • 长度 (整数)

Return type:

ndarray

resize_preserving_order(nparray, length)[source]
Extends/truncates nparray so that len(result) == length.

nparray 的元素被复制以达到所需的长度(优先使用较早的元素)。

如果nparray为空,则构造一个长度为n的零数组。

另请参阅

resize_array

循环元素而不是优先选择较早的元素

make_even

类似的早期偏好的行为,用于平衡两个可迭代对象

示例

正常用法:

resize_preserving_order(np.array([]), 5)
# np.array([0., 0., 0., 0., 0.])

nparray = np.array([[1, 2],
                    [3, 4]])

resize_preserving_order(nparray, 1)
# np.array([[1, 2]])

resize_preserving_order(nparray, 3)
# np.array([[1, 2],
#           [1, 2],
#           [3, 4]])
Parameters:
  • nparray (ndarray)

  • 长度 (整数)

Return type:

ndarray

resize_with_interpolation(nparray, length)[source]
Extends/truncates nparray so that len(result) == length.

新元素被插值以达到所需的长度。

请注意,如果nparray的长度发生变化,其dtype也可能发生变化 (例如,int -> float:参见示例)

另请参阅

resize_array

循环元素而不是插值

resize_preserving_order

倾向于较早的元素而不是插值

示例

正常用法:

nparray = np.array([[1, 2],
                    [3, 4]])

resize_with_interpolation(nparray, 1)
# np.array([[1., 2.]])

resize_with_interpolation(nparray, 4)
# np.array([[1.        , 2.        ],
#           [1.66666667, 2.66666667],
#           [2.33333333, 3.33333333],
#           [3.        , 4.        ]])

nparray = np.array([[[1, 2],[3, 4]]])
resize_with_interpolation(nparray, 3)
# np.array([[[1., 2.], [3., 4.]],
#           [[1., 2.], [3., 4.]],
#           [[1., 2.], [3., 4.]]])

nparray = np.array([[1, 2], [3, 4], [5, 6]])
resize_with_interpolation(nparray, 4)
# np.array([[1.        , 2.        ],
#           [2.33333333, 3.33333333],
#           [3.66666667, 4.66666667],
#           [5.        , 6.        ]])

nparray = np.array([[1, 2], [3, 4], [1, 2]])
resize_with_interpolation(nparray, 4)
# np.array([[1.        , 2.        ],
#           [2.33333333, 3.33333333],
#           [2.33333333, 3.33333333],
#           [1.        , 2.        ]])
Parameters:
  • nparray (ndarray)

  • 长度 (整数)

Return type:

ndarray

stretch_array_to_length(nparray, length)[来源]
Parameters:
  • nparray (ndarray)

  • 长度 (整数)

Return type:

ndarray

tuplify(obj)[来源]

智能地将obj转换为元组。

示例

正常用法:

tuplify('str')   # ('str',)
tuplify([1, 2])  # (1, 2)
tuplify(len)     # (<built-in function len>,)
Return type:

元组

uniq_chain(*args)[source]
Returns a generator that yields all unique elements of the Iterables

按照提供的顺序通过args提供。

示例

正常用法:

uniq_chain([1, 2], [2, 3], [1, 4, 4])
# yields 1, 2, 3, 4
Parameters:

args (可迭代对象)

Return type:

生成器