• <bdo id='FRjY2'></bdo><ul id='FRjY2'></ul>
    <tfoot id='FRjY2'></tfoot>

    <small id='FRjY2'></small><noframes id='FRjY2'>

  • <i id='FRjY2'><tr id='FRjY2'><dt id='FRjY2'><q id='FRjY2'><span id='FRjY2'><b id='FRjY2'><form id='FRjY2'><ins id='FRjY2'></ins><ul id='FRjY2'></ul><sub id='FRjY2'></sub></form><legend id='FRjY2'></legend><bdo id='FRjY2'><pre id='FRjY2'><center id='FRjY2'></center></pre></bdo></b><th id='FRjY2'></th></span></q></dt></tr></i><div id='FRjY2'><tfoot id='FRjY2'></tfoot><dl id='FRjY2'><fieldset id='FRjY2'></fieldset></dl></div>

      <legend id='FRjY2'><style id='FRjY2'><dir id='FRjY2'><q id='FRjY2'></q></dir></style></legend>

      1. 格式化多维数组 Python

        时间:2023-10-09
          1. <legend id='8XHIT'><style id='8XHIT'><dir id='8XHIT'><q id='8XHIT'></q></dir></style></legend>
              <bdo id='8XHIT'></bdo><ul id='8XHIT'></ul>
              • <i id='8XHIT'><tr id='8XHIT'><dt id='8XHIT'><q id='8XHIT'><span id='8XHIT'><b id='8XHIT'><form id='8XHIT'><ins id='8XHIT'></ins><ul id='8XHIT'></ul><sub id='8XHIT'></sub></form><legend id='8XHIT'></legend><bdo id='8XHIT'><pre id='8XHIT'><center id='8XHIT'></center></pre></bdo></b><th id='8XHIT'></th></span></q></dt></tr></i><div id='8XHIT'><tfoot id='8XHIT'></tfoot><dl id='8XHIT'><fieldset id='8XHIT'></fieldset></dl></div>

                <tfoot id='8XHIT'></tfoot>
                  <tbody id='8XHIT'></tbody>

                1. <small id='8XHIT'></small><noframes id='8XHIT'>

                2. 本文介绍了格式化多维数组 Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何编写代码,在出现负值后立即将多维数组 a 中每个单独数组的值更改为零.因此,a 中的第二个数组的负值 [12,34,5,6,88,-10,30,75] 为 -10,它会将所有该值和紧随其后的值为零.将数组转换为 [12,34,5,6,88,0,0,0].我怎样才能获得预期的输出?

                  How can I write a code that changes the values of each individual arrays within the multidimensional array a to zeroes right after there was a negative value. So the second array within a has a negative value [12,34,5,6,88,-10,30,75] of -10 that would turn all the values of that and the values right after it to zeroes. Turning the array into [12,34,5,6,88,0,0,0]. How would I be able to get my Expected Output?

                  import numpy as np 
                  
                  a = np.array([[12,45,50,60,30],
                               [12,34,5,6,88,-10,30,75],
                               [3,45,332,45,-12,-4,-64,12],
                               [12,45,3,22,323]])
                  

                  预期输出:

                  [[12,45,50,60,30],
                   [12,34,5,6,88,0,0,0],
                   [3,45,332,45,0,0,0,0],
                   [12,45,3,22,323]]
                  

                  推荐答案

                  试试这个:

                  import numpy as np 
                  
                  a = np.array([[12,45,50,60,30],
                               [12,34,5,6,88,-10,30,75],
                               [3,45,332,45,-12,-4,-64,12],
                               [12,45,3,22,323]], dtype='object')
                  
                  
                  for l in a:
                      for i in l:
                          if i<0:
                              l[l.index(i):] = [0] * len(l[l.index(i):])
                              
                  a
                  

                  输出:

                  array([list([12, 45, 50, 60, 30]), list([12, 34, 5, 6, 88, 0, 0, 0]),
                         list([3, 45, 332, 45, 0, 0, 0, 0]), list([12, 45, 3, 22, 323])],
                        dtype=object)
                  

                  第二种解决方案:

                  import numpy as np 
                  
                  def neg_to_zero(l):
                      for i in l:
                          if i<0:
                              l[l.index(i):] = [0] * len(l[l.index(i):])
                  
                  a = np.array([[12,45,50,60,30],
                               [12,34,5,6,88,-10,30,75],
                               [3,45,332,45,-12,-4,-64,12],
                               [12,45,3,22,323]], dtype='object')
                  
                  list(map(neg_to_zero, a))
                  
                  a
                  

                  这篇关于格式化多维数组 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何为新的 python 格式选项设置可变精度 下一篇:如何仅将列表中每个字符串的标题大写?

                  相关文章

                  最新文章

                3. <legend id='zh0mb'><style id='zh0mb'><dir id='zh0mb'><q id='zh0mb'></q></dir></style></legend>
                4. <tfoot id='zh0mb'></tfoot>

                    <bdo id='zh0mb'></bdo><ul id='zh0mb'></ul>

                    <i id='zh0mb'><tr id='zh0mb'><dt id='zh0mb'><q id='zh0mb'><span id='zh0mb'><b id='zh0mb'><form id='zh0mb'><ins id='zh0mb'></ins><ul id='zh0mb'></ul><sub id='zh0mb'></sub></form><legend id='zh0mb'></legend><bdo id='zh0mb'><pre id='zh0mb'><center id='zh0mb'></center></pre></bdo></b><th id='zh0mb'></th></span></q></dt></tr></i><div id='zh0mb'><tfoot id='zh0mb'></tfoot><dl id='zh0mb'><fieldset id='zh0mb'></fieldset></dl></div>
                    1. <small id='zh0mb'></small><noframes id='zh0mb'>