python将数组拆分为等效排名的子数组

时间:2021-01-18 21:41:09

EDITED:

I have an array of n sorted values.

我有一个n个排序值的数组。

I want to create m sub-arrays so that my best element goes into my first sub-array, my second element goes into my second sub array, etc, and my n+1-th best element goes into my first sub array.

我想创建m个子数组,以便我的最佳元素进入我的第一个子数组,我的第二个元素进入我的第二个子数组等,并且我的第n + 1个最佳元素进入我的第一个子数组。

If I have just two arrays its easy but if I want more than two sub-arrays I don't know how to do it.

如果我只有两个数组很容易,但如果我想要两个以上的子数组,我不知道该怎么做。

for example if I have an initial array:

例如,如果我有一个初始数组:

a = [50, 45, 40, 35, 30, 25, 20, 10, 9, 8]

And I want 3 sub-arrays I should get:

我想要3个子阵列:

x1: [50, 35, 20, 8]
x2: [45, 30, 10]
x3: [40, 25, 9]

What's the most efficient/pythonic way of doing this?

这样做最有效/ pythonic的方法是什么?

4 个解决方案

#1


1  

FOR X SUBLISTS :

对于X SUBLISTS:

One possibility would be to do :

一种可能性是:

def get_sublists(original_list, number_of_sub_list_wanted):
    sublists = list()
    for sub_list_count in range(number_of_sub_list_wanted): 
        sublists.append(original_list[sub_list_count::number_of_sub_list_wanted])
    return sublists

You can then unpack the sub-lists stored in sublist.

然后,您可以解压缩存储在子列表中的子列表。

For example :

例如 :

a = [5,4,3,2,1,0]
x1, x2 = get_sublists(a, 2)

will grant you the expected output.

会给你预期的输出。

This is the trivial solution. Their is probably something more pythonic in itertools or an other lib.

这是一个简单的解决方案。它们可能是itertools或其他lib中的pythonic。

If you don't understand how this code works , take a look at the documentation of a slice.

如果您不理解此代码的工作原理,请查看切片的文档。

#2


2  

For 2 arrays

x1, x2 = map(list, zip(*zip(*[iter(a)] * 2)))

print(x1, x2, sep='\n')

[5, 3, 1]
[4, 2, 0]

For 3 arrays

x1, x2, x3 = map(list, zip(*zip(*[iter(a)] * 3)))

print(x1, x2, x3, sep='\n')

[5, 2]
[4, 1]
[3, 0]

Why this works

  1. iter(a) creates an iterator on the list a. When you iterate through it, the elements get used up and eventually the iterator becomes exhausted.
  2. iter(a)在列表a上创建一个迭代器。当你遍历它时,元素会用完,最终迭代器会耗尽。

  3. [iter(a)] * 2 creates a list that looks like this i = iter(a); [i, i]. Notice that the same iterator shows up twice. That means that when I take an element from the first i, I also take it from the second i because they point to the same iterator.
  4. [iter(a)] * 2创建一个看起来像这样的列表i = iter(a); [我,我]。请注意,相同的迭代器显示两次。这意味着当我从第一个i中获取一个元素时,我也从第二个i中获取它,因为它们指向同一个迭代器。

  5. So! when I use zip on the unpacked list of the same iterator zip(*[iter(a)] * 2), As I pair things up, I'm pulling from the same iterator and therefore naturally exhausting them in the order we want.
  6. 所以!当我在同一个迭代器zip(* [iter(a)] * 2)的解压缩列表上使用zip时,当我配对时,我从同一个迭代器中拉出来,因此按照我们想要的顺序自然地耗尽它们。

  7. I then use another zip to transpose the results and then map with list to make them lists instead of tuples.
  8. 然后我使用另一个zip转置结果,然后使用列表映射使它们成为列表而不是元组。

#3


0  

a = [5, 4, 3 ,2, 1, 0]

def get_original_val(original, x):
    return [original[index] for index in x]

def get_index(a):
    index_x1, index_x2 = [], []
    local_list = a if not len(a) % 2 else a + [0]
    for idx, x in enumerate(local_list):
        target = index_x2 if idx % 2 else index_x1
        target.append(idx)
    get_x1 = get_original_val(a, index_x1)
    get_x2 = get_original_val(a,  index_x2)
    return get_x1, get_x2

>>>x1, x2 = get_index(a)
>>>x1
[5, 3, 1]
>>>x2
[4, 2, 0]

#4


0  

Below will give the desired answer,

下面将给出所需的答案,

a = [5, 4, 3 ,2, 1, 0]
a.sort(reverse=True)
x1 = []
x2 = []
for i in range(len(a)):
   x1.append(a[i]) if (i%2==0) else x2.append(a[i])
print x1, x2

#1


1  

FOR X SUBLISTS :

对于X SUBLISTS:

One possibility would be to do :

一种可能性是:

def get_sublists(original_list, number_of_sub_list_wanted):
    sublists = list()
    for sub_list_count in range(number_of_sub_list_wanted): 
        sublists.append(original_list[sub_list_count::number_of_sub_list_wanted])
    return sublists

You can then unpack the sub-lists stored in sublist.

然后,您可以解压缩存储在子列表中的子列表。

For example :

例如 :

a = [5,4,3,2,1,0]
x1, x2 = get_sublists(a, 2)

will grant you the expected output.

会给你预期的输出。

This is the trivial solution. Their is probably something more pythonic in itertools or an other lib.

这是一个简单的解决方案。它们可能是itertools或其他lib中的pythonic。

If you don't understand how this code works , take a look at the documentation of a slice.

如果您不理解此代码的工作原理,请查看切片的文档。

#2


2  

For 2 arrays

x1, x2 = map(list, zip(*zip(*[iter(a)] * 2)))

print(x1, x2, sep='\n')

[5, 3, 1]
[4, 2, 0]

For 3 arrays

x1, x2, x3 = map(list, zip(*zip(*[iter(a)] * 3)))

print(x1, x2, x3, sep='\n')

[5, 2]
[4, 1]
[3, 0]

Why this works

  1. iter(a) creates an iterator on the list a. When you iterate through it, the elements get used up and eventually the iterator becomes exhausted.
  2. iter(a)在列表a上创建一个迭代器。当你遍历它时,元素会用完,最终迭代器会耗尽。

  3. [iter(a)] * 2 creates a list that looks like this i = iter(a); [i, i]. Notice that the same iterator shows up twice. That means that when I take an element from the first i, I also take it from the second i because they point to the same iterator.
  4. [iter(a)] * 2创建一个看起来像这样的列表i = iter(a); [我,我]。请注意,相同的迭代器显示两次。这意味着当我从第一个i中获取一个元素时,我也从第二个i中获取它,因为它们指向同一个迭代器。

  5. So! when I use zip on the unpacked list of the same iterator zip(*[iter(a)] * 2), As I pair things up, I'm pulling from the same iterator and therefore naturally exhausting them in the order we want.
  6. 所以!当我在同一个迭代器zip(* [iter(a)] * 2)的解压缩列表上使用zip时,当我配对时,我从同一个迭代器中拉出来,因此按照我们想要的顺序自然地耗尽它们。

  7. I then use another zip to transpose the results and then map with list to make them lists instead of tuples.
  8. 然后我使用另一个zip转置结果,然后使用列表映射使它们成为列表而不是元组。

#3


0  

a = [5, 4, 3 ,2, 1, 0]

def get_original_val(original, x):
    return [original[index] for index in x]

def get_index(a):
    index_x1, index_x2 = [], []
    local_list = a if not len(a) % 2 else a + [0]
    for idx, x in enumerate(local_list):
        target = index_x2 if idx % 2 else index_x1
        target.append(idx)
    get_x1 = get_original_val(a, index_x1)
    get_x2 = get_original_val(a,  index_x2)
    return get_x1, get_x2

>>>x1, x2 = get_index(a)
>>>x1
[5, 3, 1]
>>>x2
[4, 2, 0]

#4


0  

Below will give the desired answer,

下面将给出所需的答案,

a = [5, 4, 3 ,2, 1, 0]
a.sort(reverse=True)
x1 = []
x2 = []
for i in range(len(a)):
   x1.append(a[i]) if (i%2==0) else x2.append(a[i])
print x1, x2