从txt文件中读取和排序向量

时间:2021-03-13 16:37:37

Is a beginner question, but i don't succeed to go out from the error. I have a text file with value written like: [ 2 3 4 5 1] and another vector where i write the order in which i want reorder the first vector.
So if the order vector is [ 5 3 2 1 4 ] the first vector will become [ 1 4 3 2 5 ].
The idea was import the file.txt with np.loadtxt and than:

是一个初学者的问题,但我没有成功地从错误中走出来。我有一个文本文件,其值写为:[2 3 4 5 1]和另一个向量,我写下我想重新排序第一个向量的顺序。因此,如果顺序向量是[5 3 2 1 4],则第一个向量将变为[1 4 3 2 5]。这个想法是使用np.loadtxt导入file.txt而不是:

for i in range (5):
    a=int('order[i]')
    b = a
    c = suono[b]
    suono2.write[i] = c
    suono2.save(c)
    emp =  "" 
    suono2.save(emp)
    suono2.close

3 个解决方案

#1


2  

If you wish to use numpy, which is not a bad idea if you are working exclusively with numeric arrays, then you can use simple indexing.

如果你想使用numpy,如果你只使用数字数组,这不是一个坏主意,那么你可以使用简单的索引。

You just need to be careful to subtract one since indices begin at 0. This appears to be the cause of your error.

您只需要小心减去一个,因为索引从0开始。这似乎是您的错误的原因。

import numpy as np
lst= np.loadtxt("vector.txt")
lst = lst.astype(int)
order = np.loadtxt("orderSet.txt")
order = order.astype(int)
# alternative is A = np.array([ 2, 3, 4, 5, 1])
#order = np.array([ 5, 3, 2, 1, 4 ])

res = A[order-1]

print(res)

[1 4 3 2 5]

#2


1  

I'm not sure if you have to use numpy at all.

我不确定你是否必须使用numpy。

With bare python: just rebuild your list using a list comprehension, iterating on the "order" list (minus 1 because python lists start at 0):

使用裸python:只需使用列表推导重建列表,迭代“订单”列表(减1,因为python列表从0开始):

lst = [ 2, 3, 4, 5, 1]
order = [ 5, 3, 2, 1, 4 ]

result = [lst[i-1] for i in order]

print(result)

result:

[1, 4, 3, 2, 5]

#3


0  

You can use getitem

你可以使用getitem

 mylist = range(10)
 indexes= [1,3,5]
 print(list(map(l.__getitem__, indexes)))

In Python 3

在Python 3中

>>> mylist = range(10)
>>> indexes= [1,3,5]
>>> print(list(map(l.__getitem__, indexes))) 
[1, 3, 5]

#1


2  

If you wish to use numpy, which is not a bad idea if you are working exclusively with numeric arrays, then you can use simple indexing.

如果你想使用numpy,如果你只使用数字数组,这不是一个坏主意,那么你可以使用简单的索引。

You just need to be careful to subtract one since indices begin at 0. This appears to be the cause of your error.

您只需要小心减去一个,因为索引从0开始。这似乎是您的错误的原因。

import numpy as np
lst= np.loadtxt("vector.txt")
lst = lst.astype(int)
order = np.loadtxt("orderSet.txt")
order = order.astype(int)
# alternative is A = np.array([ 2, 3, 4, 5, 1])
#order = np.array([ 5, 3, 2, 1, 4 ])

res = A[order-1]

print(res)

[1 4 3 2 5]

#2


1  

I'm not sure if you have to use numpy at all.

我不确定你是否必须使用numpy。

With bare python: just rebuild your list using a list comprehension, iterating on the "order" list (minus 1 because python lists start at 0):

使用裸python:只需使用列表推导重建列表,迭代“订单”列表(减1,因为python列表从0开始):

lst = [ 2, 3, 4, 5, 1]
order = [ 5, 3, 2, 1, 4 ]

result = [lst[i-1] for i in order]

print(result)

result:

[1, 4, 3, 2, 5]

#3


0  

You can use getitem

你可以使用getitem

 mylist = range(10)
 indexes= [1,3,5]
 print(list(map(l.__getitem__, indexes)))

In Python 3

在Python 3中

>>> mylist = range(10)
>>> indexes= [1,3,5]
>>> print(list(map(l.__getitem__, indexes))) 
[1, 3, 5]