Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想重新定义和列出int64的数组,这样数组中的值就会被排序,并且只包含非False的值_Python_Arrays_Python 3.x_Sorting - Fatal编程技术网

Python 我想重新定义和列出int64的数组,这样数组中的值就会被排序,并且只包含非False的值

Python 我想重新定义和列出int64的数组,这样数组中的值就会被排序,并且只包含非False的值,python,arrays,python-3.x,sorting,Python,Arrays,Python 3.x,Sorting,我有以下数据: 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 = [e.split() for e in l] concentration = [n

我有以下数据:

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 = [e.split() for e in l]
concentration = [np.array(concentration[3::2], dtype=np.int) for concentration in data]
我希望将变量(浓度)中不在区间(0-50)内的值设置为False/0。因此,我对代码执行了以下操作:

for row in range(len(concentration)):
    for element in range(len(concentration[row])):
        if 0 > concentration[row][element] or concentration[row][element] > 50:
        concentration[row][element] = False
        print("Error: Index {:} in time is out of range".format(element))
我得到以下输出,我的浓度变量如下所示:

Array of int64 [36 33 0 0]
Array of int64 [30 27 24 0 0]
Array of int64 [21 18 15 12 0 0]
Array of int64 [6 3 0 0]
Array of int64 [33 36]
Array of int64 [24 27 30]
Array of int64 [12 15 18 21]
Array of int64 [3 6]
现在我想重新定义我的变量(浓度),其中的值被排序,并且只包含True/1值(不是False/0的值)。我希望我的新浓度变量如下所示:

Array of int64 [36 33 0 0]
Array of int64 [30 27 24 0 0]
Array of int64 [21 18 15 12 0 0]
Array of int64 [6 3 0 0]
Array of int64 [33 36]
Array of int64 [24 27 30]
Array of int64 [12 15 18 21]
Array of int64 [3 6]

谢谢你迄今为止的帮助

您可以通过以下方式解决问题:

initial_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']

result = [sorted(filter(lambda x: 0 < x < 50, 
                        list(map(int, elem.split()))[3::2])) for elem in initial_data]

print(result)
# [[33, 36], [24, 27, 30], [9, 12, 15, 18, 21], [3, 6]]

这是一个很好的答案!但我仍然希望在for循环之后仅使用True/0值重新定义变量(浓度),但我使用了“sort”方法对变量进行排序。谢谢你@金迪,有新的更新部分在回答。请看一下。谢谢你的帮助,爱德华!我还试图按降序对另一个变量排序,所以我尝试了list(map(lambda x:np.sort(x[x>0],reverse=True),concentration)),但我一直得到一个错误?@Kimdey,你可以使用[::-1]结构,f.e.np.sort(x[x>0])[::-1]谢谢你的快速回答,我实际上做了以下几件事;列表(map(lambda x:-np.sort(-x[x>0]),concentration)),这也导致我按降序排列