【问题标题】:how to merge list of arrays and each element in one array into list of tuples in numpy如何将数组列表和一个数组中的每个元素合并到numpy中的元组列表中
【发布时间】:2021-11-28 01:29:29
【问题描述】:

所以在 python 中,我有一个看起来像的数组列表

    [array([628,  688,  924, 1598], dtype=int32), 
    array([ 957, 1983, 2031, 2429], dtype=int32),
    array([1243, 1598, 2872], dtype=int32)]

还有一个看起来像的数组

    array([1, 2, 3], dtype = int32])

我想将 2 合并到元组列表中,或者类似的东西。

    [(array([628,  688,  924, 1598], dtype=int32), 1), 
    (array([ 957, 1983, 2031, 2429], dtype=int32), 2),
    (array([1243, 1598, 2872], dtype=int32), 3)]

有人可以帮我编写这个代码吗?

谢谢!

【问题讨论】:

  • list(zip(list1, arr1)) 应该这样做。
  • 谢谢!我考虑过使用 zip,但没有考虑使用 list(zip())!

标签: python arrays numpy merge tuples


【解决方案1】:

You can use zip():

a = [array([628,  688,  924, 1598], dtype=int32), 
    array([ 957, 1983, 2031, 2429], dtype=int32),
    array([1243, 1598, 2872], dtype=int32)]
b = array([1, 2, 3], dtype = int32)          # there was an extra ] here
c = list(zip(a, b))

结果:

[(array([ 628,  688,  924, 1598], dtype=int32), 1),
 (array([ 957, 1983, 2031, 2429], dtype=int32), 2),
 (array([1243, 1598, 2872], dtype=int32), 3)]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 2011-08-27
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多