[英]RENUMBER ELEMENTS OF 1D ARRAY
I am new here, I want to share with you, a problem that I am facing我是新来的,我想和你分享一个我面临的问题
Basically I have to renumber an array in accordance to the elements of an another array.基本上我必须根据另一个数组的元素重新编号一个数组。
More in details:更多细节:
I have an int array a我有一个 int 数组 a
a=array([ 1, 2, 3, ..., 21041, 21042, 21043])
with和
len(a)=21043
Then, applying a mask to "a" it obtains b然后,将掩码应用于“a”,得到 b
b=a[mask]
b=array[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 28 32 33 34 47 54 55 62....21043]
with和
len(b)=15717
At this point I have sorted easly, this array in such way:在这一点上,我已经轻松地对这个数组进行了排序:
b_renumber=array[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23....15717]
So the number 28, in "b", becames 21 in "b_renumber", 32 becames 22 and so on.因此,“b”中的数字 28 在“b_renumber”中变为 21,32 变为 22,依此类推。
Now the problem is the third array c现在问题是第三个数组 c
since c has a lenght greater then b.因为 c 的长度大于 b。
len(c)=46153
and is given by:并且由下式给出:
c=array[ 3 4 142 633 12 19 564 497 513 54 308 177 254 532 155 3 273 28 ...21037]
So basically c is an array of length 46153 (unsorted) with the elements that go from 1 to 21043, as well as the array b, and like b some numbers doesn't exist (eg 21 22 23...etc) ie the same elements that do not exist in b also in c are not present.所以基本上 c 是一个长度为 46153(未排序)的数组,其中 go 从 1 到 21043 的元素,以及数组 b 和像 b 一样的一些数字不存在(例如 21 22 23... b 中不存在的相同元素也在 c 中不存在。
What I want to do is renumber the elements of c in such that there is a linking between the value of elements of c and the b_renumber.我想要做的是重新编号 c 的元素,这样 c 的元素值和 b_renumber 之间存在链接。 Example in b we have the number 28, that in b_renumber becames 21 (from b to b_renumber, 28 -> 21), and what I want to do is the follow: each time that I meet the value 28 in c, this must becames 21, same for the other numbers. b中的示例我们有数字28,在b_renumber中变成21(从b到b_renumber,28 -> 21),我想做的是:每次我在c中遇到值28时,这必须变成21,其他数字相同。
Just a last stuff: the array are still small, but I have to work with array with greater dimension, so I have to do this in efficient way, maybe exist a function o some package that allow me do this.最后一点:数组仍然很小,但我必须使用更大维度的数组,所以我必须以有效的方式执行此操作,可能存在 function 或一些 package 允许我这样做。 Please I am open to every advises请我愿意接受每一个建议
Sorry for the length of the post I hope someone can and want help me.很抱歉帖子的长度,我希望有人可以帮助我。
Thank you in advance.先感谢您。
Matteo马泰奥
Assuming b_renum just contains the index+1 of each element in b:假设 b_renum 只包含 b 中每个元素的索引+1:
Input:输入:
b = [0,1,2,100]
c = [20,50,60,100]
Note that dict.get(key, default)
returns the key's value if there's a match, or the default otherwise.请注意dict.get(key, default)
如果匹配则返回键的值,否则返回默认值。
b_renum = list(range(1,len(b)+1))
renum_dict = dict(zip(b, b_renum))
# slightly slower
# renum_dict = {elem:ind for ind,elem in enumerate(b)}
c_renum = [renum_dict.get(key,key) for key in c]
>>> print(b_renum)
[1, 2, 3, 4]
>>> print(c_renum)
[20, 50, 60, 3]
Note how 100 gets mapped to 3 in c_renum
.请注意 100 如何在c_renum
中映射到 3。 I think the useful things for you are 1) using zip()
since you know b
and b_renum
are the same length, and 2) using a dictionary to lookup values + the dict.get()
trick.我认为对您有用的是 1) 使用zip()
因为您知道b
和b_renum
的长度相同,以及 2) 使用字典来查找值 + dict.get()
技巧。 The list comprehension is also quite fast.列表理解也很快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.