【问题标题】:I want to access element in list of arrays of int64我想访问 int64 数组列表中的元素
【发布时间】:2020-11-27 23:07:49
【问题描述】:

我有这些数据:

l = ['10 20 10 36 30 33 400 400 -1 -1', 
    '100 50 50 30 60 27 70 24 -2 -2 700 700', 
    '300 1000 80 21 90 18 100 15 110 12 120 9 900 900 -3 -3',
    '30 90 130 6 140 3 -4 -4 1000 1000']
                                                                                            
l = [e.split() for e in l]

我创建了以下变量,它是 int64 数组的列表:

time = [np.array(time[2::2], dtype=int) for time in l]

我希望对我的时间数组值进行排序,并且只包含不在 0-50 范围内的值。 所以我做了以下事情:

for array in range(len(time)):
    for row in time[array]:
        for element in row:
          if element < 0 or element > 50:
          element = False
          print("Error: Index {:.2f} in time is out of range".format(element))

我得到了这个输出:

 'numpy.int64' object is not iterable

我想要这个输出:

Array of int64 [10 30 False False]
Array of int64 [50 60 70 False False]
Array of int64 [80 90 100 110 120 False  False]
Array of int64 [130 140 False False]

【问题讨论】:

  • 我没有得到你的输出,因为你说你想要 0-50 之间的值
  • 循环中应该有if element &gt;= 50:吗?
  • time[array] 是一行,而不是二维数组。
  • 我想你想要for row in time:,你不需要array 变量。
  • 好的,我在 for 循环中忘记了一些东西——所以我已经编辑了我的问题,现在我应该拥有一切;我的意思是我希望我的数组/行只包含不包含 0-50 的值,否则 value/int = False

标签: python arrays python-3.x arraylist numpy-ndarray


【解决方案1】:

先提几个小指点:

  1. 请不要使用 1 个字母的变量名,除非它们用于索引(i、j、k 等),而是正确的全名。
  2. 在 python 中,您可以直接迭代元素,因此不需要 range 函数和索引。

问题是你试图迭代一个数字

for array in range(len(time)):
    # time[array] = [10 30 400 1] on first turn, so row gets value 10
    # Since we are directly iterating over the elements.
    for row in time[array]:  
        for element in row:  # <--- Row is here an integer value (10) on the first turn.
            if element < 0 or element > 50:
                element = False
                print("Error: Index {:.2f} in time is out of range".format(element))

现在我稍微改写了您的开头,使用了一些更具描述性的名称(请随意忽略它)。但这是我的代码开始。

import numpy as np

data = [
    '10 20 10 36 30 33 400 400 -1 -1',
    '100 50 50 30 60 27 70 24 -2 -2 700 700',
    '300 1000 80 21 90 18 100 15 110 12 120 9 900 900 -3 -3',
    '30 90 130 6 140 3 -4 -4 1000 1000'
]

data = [row.split() for row in data]
time = [np.array(time[2::2], dtype=np.int) for time in data]

然后是下面的代码:

for row in time:
    elements = []
    nr_false = 0
    for element in row:
        if 0 <= element <= 50:
            nr_false += 1
        else:
            elements.append(element)

    elements.sort()
    elements.extend([False] * nr_false)
    print("Array of int64", elements)

产生输出:

Array of int64 [-1, 400, False, False]
Array of int64 [-2, 60, 70, 700, False]
Array of int64 [-3, 80, 90, 100, 110, 120, 900]
Array of int64 [-4, 130, 140, 1000]

说明

我们将添加整数或布尔值 (False) 的新数据。如果我们尝试使用np.int 类型来执行此操作,则布尔值将表示为0。因此,我们创建了一个中间列表,它将维护所有有效值并计算无效元素的数量。

由于在您的示例中,False 元素在最后进行了排序,因此我选择忽略 False 添加直到最后一点并首先对元素进行排序。

【讨论】:

  • 谢谢你,这正是我正在寻找的输出,因为我希望变量资源管理器中的变量也具有相同的输出,而不仅仅是在控制台中打印。但这太接近了。如果您能进一步帮助我,我将不胜感激 - 下面是我迄今为止编写的代码,无论哪种方式,我都感谢您的时间和帮助!
  • 如果您想存储输出变量,您可以创建另一个列表,类似于 elements 但在循环之外并附加最终答案。那是你要找的吗?另请注意,无法使用 False 值创建 int64 数组。由于False不是整数,所以在转换为np.int64时会用0表示。
【解决方案2】:

我对您的解释和示例输出感到困惑。

正如 Thymen 在他的回答中指出的那样,您不能在 int 类型的 numpy 数组中包含字符串或布尔类型值。

由于您希望超出要求范围的值与范围内的值不同,因此可以将要求范围外的值替换为异常值。

因此,不在 0-50 之间的值将被替换为 9999999999(因为我们不能在 int 类型的 numpy 数组中包含 False 或“False”)。

例如,-1 将被替换为 9999999999 或 70 将被替换为 9999999999,因为它们不是

我已经考虑了以下代码的示例:

l = ['10 20 10 36 30 33 400 400 -1 -1', 
    '100 50 50 30 60 27 70 24 -2 -2 700 700', 
    '300 1000 80 21 90 18 100 15 110 12 120 9 900 900 -3 -3',
    '30 90 130 6 140 3 -4 -4 1000 1000']
                                                                                            
l = [e.split() for e in l]

time = [np.array(time[2::2], dtype=np.int64) for time in l]

for row in range(len(time)):
    for element in range(len(time[row])):
        
        # I didn't understand your output requirement. You said you 
        # wanted to have values that are not withing 0-50 but the 
        # example output shows 10, 30 and also 60 and 140.
        
        # So, edit the below condition according to your requirement
        if time[row][element] >=0 and time[row][element] <= 50: 
            time[row][element] = 9999999999

    time[row].sort()
    
time

这给出了:

[array([        -1,        400, 9999999999, 9999999999], dtype=int64),
 array([        -2,         60,         70,        700, 9999999999],
       dtype=int64),
 array([ -3,  80,  90, 100, 110, 120, 900], dtype=int64),
 array([  -4,  130,  140, 1000], dtype=int64)]

它改变了原始数组,在变量资源管理器中如下所示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 2022-01-13
    相关资源
    最近更新 更多