【问题标题】:How to map a function on a list and append the new values onto the same list multiple times?如何在列表上映射函数并将新值多次附加到同一个列表中?
【发布时间】:2020-11-05 00:42:18
【问题描述】:

我有以下数组:

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

我正在尝试在此列表上添加一个列表,但新列表已将每个元素添加了 10。我需要完成 30 次。

例如,输出应该是

array([1, 1, 1, 3, 3, 4, 4, 6, 6, 6, 2, 2, 5, 5, 3, 11, 11, 11, 13, 13, 14, 14, 16, 16, 16, 12, 12, 15, 15, 13, 21, 21, 21, 23, 23, 24, 24, 26, 26, 26, 22, 22, 25, 25, 23, ...])

到目前为止,我有以下代码。不知道怎么迭代30次。

np.append(np.array([1, 1, 1, 3, 3, 4, 4, 6, 6, 6, 2, 2, 5, 5, 3]),np.array([1, 1, 1, 3, 3, 4, 4, 6, 6, 6, 2, 2, 5, 5, 3])+10)

【问题讨论】:

    标签: python arrays python-3.x numpy


    【解决方案1】:

    广播加法可以解决问题:

    In [42]: arr+np.arange(0,4)[:,None]*10
    Out[42]: 
    array([[ 1,  1,  1,  3,  3,  4,  4,  6,  6,  6,  2,  2,  5,  5,  3],
           [11, 11, 11, 13, 13, 14, 14, 16, 16, 16, 12, 12, 15, 15, 13],
           [21, 21, 21, 23, 23, 24, 24, 26, 26, 26, 22, 22, 25, 25, 23],
           [31, 31, 31, 33, 33, 34, 34, 36, 36, 36, 32, 32, 35, 35, 33]])
    

    既然你想要一个 1d 版本:

    In [43]: (arr+np.arange(0,4)[:,None]*10).ravel()
    Out[43]: 
    array([ 1,  1,  1,  3,  3,  4,  4,  6,  6,  6,  2,  2,  5,  5,  3, 11, 11,
           11, 13, 13, 14, 14, 16, 16, 16, 12, 12, 15, 15, 13, 21, 21, 21, 23,
           23, 24, 24, 26, 26, 26, 22, 22, 25, 25, 23, 31, 31, 31, 33, 33, 34,
           34, 36, 36, 36, 32, 32, 35, 35, 33])
    

    我们为每个 row 添加的内容:

    In [44]: np.arange(0,4)[:,None]*10
    Out[44]: 
    array([[ 0],
           [10],
           [20],
           [30]])
    

    【讨论】:

      【解决方案2】:

      只需使用运行 30 次的循环即可。

      template = np.array([1, 1, 1, 3, 3, 4, 4, 6, 6, 6, 2, 2, 5, 5, 3])
      arr = np.array([])
      for i in range(30):
          arr = np.append(arr, template + i * 10)
      

      【讨论】:

        【解决方案3】:

        使用这个:

        new_a = np.concatenate([a+(i*10) for i in range(0,31)])
        

        a 是您的原始数组。

        【讨论】:

          【解决方案4】:

          你可以试试这个:

          import numpy as np 
          x=np.array([1, 1, 1, 3, 3, 4, 4, 6, 6, 6, 2, 2, 5, 5, 3])
          print(np.concatenate([x,x+10,x+20]))
          

          30 次重复的更好方法:

          import numpy as np
          template = np.array([1, 1, 1, 3, 3, 4, 4, 6, 6, 6, 2, 2, 5, 5, 3])
          print((template+np.arange(0,4)[:,None]*10).flatten())
          

          【讨论】:

          • 这没有30次重复。
          • 如果他自己可以创建一个 for 循环,他就不会发布这个问题了。
          【解决方案5】:

          我认为您正在尝试连接数组而不是附加它们以获取数组列表。

          import numpy as np
          
          a = np.array([1, 1, 1, 3, 3, 4, 4, 6, 6, 6, 2, 2, 5, 5, 3])
          
          result = np.concatenate([a + 10*i for i in range(30)])
          

          请注意,列表推导从 i = 0 开始,表示您的原始数组,并在 i = 29 结束,将 290 添加到原始数组的每个元素。

          如果您确实需要数组列表,列表推导式[a + 10*i for i in range(30)] 将提供该列表。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-06-07
            • 1970-01-01
            • 1970-01-01
            • 2021-09-21
            • 2018-06-09
            • 1970-01-01
            • 1970-01-01
            • 2014-09-06
            相关资源
            最近更新 更多