【问题标题】:Python fill array with 0's without numpy [duplicate]Python用0填充数组而没有numpy [重复]
【发布时间】:2023-03-29 05:30:01
【问题描述】:

我知道在 python 中你可以使用numpy.zeroes(size) 填充一个数组,但作为学习的一部分,我被要求不要使用numpy,所以我的问题是,有没有办法在没有numpy.zeroes 的情况下填充一个数组功能?

具体来说,如何在以下函数中用 0 填充以下列表?

def myHistWithRescale(listOfNums, maxInt):
    """Givne a list of real numbers in any range, first scale the numbers to
    inters between 0 and maxInt (inclusive), then return the number of occurrences
    of each integer in a list
    """
    rescaledData = rescaleToInt(listOfNums, maxInt)  
    return counts

【问题讨论】:

  • 标题似乎与方法中的注释不符。
  • 该功能与问题有何关系?

标签: python


【解决方案1】:

要创建一个大小为全零的n 列表,只需编写[0] * n

【讨论】:

    【解决方案2】:

    对于函数,你可以这样做:arr = [0] * maxInt。其中maxInt 是列表的大小。
    同样,如果你想用 0 以外的东西填充数组;您可以将 0 替换为您要填写的任何数字

    【讨论】:

      猜你喜欢
      • 2021-10-04
      • 2019-03-22
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      • 2016-06-15
      • 2022-01-09
      相关资源
      最近更新 更多