【问题标题】:selecting array elements of certain range and define a new array选择一定范围的数组元素并定义一个新数组
【发布时间】:2016-01-13 13:57:14
【问题描述】:

我不太习惯 pyhton,我想编写一个函数,该函数将数组 x 作为输入并返回一个数组 (select),该数组仅由输入数组中满足特定属性的条目组成,例如处于一定范围内。应该执行此操作的函数如下:

def select(x):
    count = 0
    select = []                #0
    for i in range(0,len(x[0])):
      if ( (int(x[4][i])+0.5) > x[4][i] > (int(x[4][i])-0.5)  ):
        select[0][count]=x[0][i] #1
        select[1][count]=x[1][i] #2
        select[2][count]=x[4][i] #3
        count = count + 1
    return select

但是,如果我调用该函数,我会收到以下错误消息:

IndexError: list index out of range

导致它的行是“#1”(我认为以下 2 行也很麻烦)。我想我必须以某种方式定义数组大小。在这种情况下,我该如何在 python 中做到这一点?正如我所见,select=[] 还不够。

亲切的问候

【问题讨论】:

  • 问题(我认为您意识到)是 select 没有 [0] 元素。你能说明你期望输入x 和输出select 在某些情况下是什么吗?

标签: python arrays list python-3.x size


【解决方案1】:

select 最初是一个空列表。您正在尝试为其当前不存在的元素分配值。

可能你需要select = [[], [], []]

当您尝试在#1、#2 和#3 中分配select 的内部元素时,它们的元素也不会存在

也许这就是你想要的:

def select(x):
    select = [[] for i in range(3)]    #0 : [[], [], []]
    for i in range(0,len(x[0])):
      if ( (int(x[4][i])+0.5) > x[4][i] > (int(x[4][i])-0.5)  ):
        select[0].append(x[0][i]) #1
        select[1].append(x[1][i]) #2
        select[2].append(x[4][i]) #3
    return select

【讨论】:

  • 也许,但是用空白填充它似乎是一个奇怪的选择。我认为select = range(3) 是更 Pythonic 的方式。
  • 他想在select 中嵌套一些东西,所以需要一个字典或另一个列表,而不是一个范围。
  • 完全明白,呵呵!
  • @jatinderjit 非常感谢!
【解决方案2】:

作为您自己的过滤方法的替代方法(以实现对范围进行数组过滤),您可以使用以下方法

myRange = [8, 11]
myArr = [6, 7, 8, 9, 10, 11, 12]
myArrFiltered = [x for x in myArr if myRange[0] <= x <= myRange[1]]
print(myArrFiltered)
# [8, 9, 10, 11]

【讨论】:

    【解决方案3】:

    您的代码不起作用,因为select 列表在索引0 处没有项目。

    如果我要实现类似的功能,我会使用 filter 和一个决定应该选择哪些元素的函数:

    ages = [5, 15, 3, 7, 18, 34, 9, 10]
    
    def check_age(age):
        # we could implement all sorts of logic here
        # if we return True, the element is selected
        return age > 10
    
    children_over_ten = filter(check_age, ages)
    
    print(children_over_ten)
    

    如果选择标准足够简单,也可以简单地使用列表推导:

    ages = [5, 15, 3, 7, 18, 34, 9, 10]
    children_over_ten = [x for x in ages if x > 10]
    print(children_over_ten)
    

    【讨论】:

      【解决方案4】:

      您或许应该尝试使用可用于过滤数据的列表推导。

      例如使用交互式 python 会话,

      >>> data = [i*0.25 for i in range(0, 21)]
      >>> data
      [0.0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5, 3.75, 4.0, 4.25, 4.5, 4.75, 5.0]
      >>> [x for x in data if abs(2.5 - x) <= 0.5]
      [2.0, 2.25, 2.5, 2.75, 3.0]
      

      选择 [2.0, 3.0] 范围内的数据。

      通常可以不使用索引来完成对数组的迭代,例如

      for x in array:
          print(x)
      

      【讨论】:

        【解决方案5】:

        或者声明select = [],这样操作:

        def select(x):
            count = 0
            select = [] #0
            for i in range(0,len(x[0])):
              if ( (int(x[4][i])+0.5) > x[4][i] > (int(x[4][i])-0.5)  ):
                row = [x[r*r][i] for r in range(3)]
                select.append(row)
                count = count + 1
            return select
        

        【讨论】:

          【解决方案6】:

          您需要相应地初始化选择

          def select(x):
              count = 0
              select = [[],[],[]]                #0
              for i in range(0,len(x[0])):
                if ( (int(x[4][i])+0.5) > x[4][i] > (int(x[4][i])-0.5)  ):
                  select[0]+=[x[0][i]] #1
                  select[1]+=[x[1][i]] #2
                  select[2]+=[x[4][i]] #3
                  count = count + 1
              return select
          

          【讨论】:

          • 感谢您的宝贵时间,我收到TypeError: You must first set_array for mappable,有什么想法吗?老实说,我不知道+= 做了什么。
          • a += ba = a+b 的简写。对于列表,+ 进行连接。
          • @DonkeyKong,我刚刚编辑了我没有在右侧列出的问题。我刚刚在右上方添加了[]。您也可以使用append 代替串联。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-01
          • 2020-03-20
          • 1970-01-01
          • 1970-01-01
          • 2017-04-27
          • 1970-01-01
          相关资源
          最近更新 更多