2

在我的小项目中,我按降序对列表进行了排序,但是,我的目标是按照这种自定义模式对其进行排序。(最大 -> 最小 -> 次大 -> 次小 ->)等。

在java中,我可以这样做:

public static void wackySort(int[] nums) {
    //first, this simply sorts the array by ascending order.
    int sign = 0;
    int temp = 0;
    int temp2 = 0;
    for (int i = 0; i < nums.length; i++) {
        for (int j = 0; j < nums.length -1; j++){
            if (nums[j] > nums[j+1]) {
               temp = nums[j];
               nums[j] = nums[j+1];
               nums[j+1] = temp;
            }
        }
    }

    //prepare for new array to actually do the wacky sort.
    System.out.println();
    int firstPointer = 0;
    int secondPointer = nums.length -1;
    int[] newarray = new int[nums.length];
    int size = nums.length;

    //increment by two taking second slot replacing the last (n-1) term
    for (int i = 0; i < nums.length -1; i+=2) {
        newarray[i] = nums[firstPointer++];
        newarray[i+1] = nums[secondPointer--];
    }

    //store those values back in the nums array    
    for (int i = 0; i < nums.length; i++) {
        nums[i] = newarray[i];
    }
}

我的目标是在 python 中做同样的事情,除了向后。关于如何将 wackysort 的最后一个 for 循环转换为 python 并使其倒退的任何想法?

4

5 回答 5

6
nums = [1, 2, 3, 4]
newarray = sum(zip(reversed(nums), nums), ())[:len(nums)]

>>> print(newarray)
(4, 1, 3, 2)

它的作用,一步一步。首先,reversed()

>>> list(reversed(nums))
[4, 3, 2, 1]

然后zip()

>>> list(zip([4, 3, 2, 1], [1, 2, 3, 4]))
[(4, 1), (3, 2), (2, 3), (1, 4)]

你可以看到我们几乎有我们想要的列表,我们有一个问题:这些是元组。我们想把它们弄平。

>>> (4, 1) + (3, 2) + (2, 3) + (1, 4)
(4, 1, 3, 2, 2, 3, 1, 4)

哦。那很好。但是如何在列表中做到这一点?很简单: use sum(),它正是这样做的——将许多东西加在一起。只有我们需要给它一些开始 - 一个空元组()

>>> sum([(4, 1), (3, 2), (2, 3), (1, 4)], ())
(4, 1, 3, 2, 2, 3, 1, 4)

但是我们不想要下半场,所以让我们删除它。我们知道他的名单太长了两倍,是吗?

>>> (4, 1, 3, 2, 2, 3, 1, 4)[:len(nums)]
(4, 1, 3, 2)

而已。


另外的选择:

from itertools import chain, islice
a = list(islice(chain.from_iterable(zip(nums, reversed(nums))), len(nums)))
于 2013-07-02T22:42:46.770 回答
5

I would suggest sorting it normally first, then doing your shuffle:

inlist=[3,5,7,6,9,8,2,1]
inlist.sort()
outlist=[]
while len(inlist)>0:
  if (len(outlist)%2==0):
      outlist.append(inlist.pop())
  else:
      outlist.append(inlist.pop(0))
于 2013-07-02T22:36:28.127 回答
0

That last for loop:

for (int i = 0; i < nums.length; i++){
    nums[i] = newarray[i];

… is a one-liner in Python. The exact equivalent is:

nums[:] = newarray[:len(nums)]

However, most likely, all you really need is:

nums = newarray

If you really want to write it in "Java style" for some reason, it would be this:

i = 0
while i < len(nums):
    nums[i] = newarray[i]
    i += 1

But that does exactly the same thing as the first version, except a slower more slowly and less readably.


Meanwhile, for the previous loop:

for (int i = 0; i < nums.length -1; i+=2){
    newarray[i] = nums[firstPointer++];
                newarray[i+1] = nums[secondPointer--];

Again, you can translate this more-or-less directly to Python:

i = 0
while i < len(nums)-1:
    newarray[i] = nums[firstPointer]
    firstPointer += 1
    newarray[i+1] = nums[secondPointer]
    secondPointer -= 1
    i += 2

But as with the last version of the last loop, this is horribly unreadable, and you'll be a lot happier if you try to describe the algorithm, then write that in Python.

于 2013-07-02T22:35:40.410 回答
0

正如我在对您之前的问题的评论中所说,做(我认为)您想要的最简单的方法是首先编写一个可以将已经排序的列表交换为您想要的顺序的函数,然后您可以链接您的排序函数和新功能一起。

def wacky_sort(seq):
    # code you already have

def alternate_ends(seq):
    new_seq = []
    while seq:
        new_seq.append(seq.pop(0))
        if seq:
            new_seq.append(seq.pop())
    return new_seq

def wacky_sort_with_alternating_ends(seq):
    wacky_sort(seq)
    return alternate_ends(seq)
于 2013-07-02T22:41:43.417 回答
0

如果你想在 O(1) 额外空间和 O(n) 时间内解决这个问题,那么你可以像下面的代码一样解决。

def 重新排列(arr,n):

k,j=1,1
a=[0]*n
for i in range(0,n):

    if i%2==0:
        a[i]=arr[n-k]
        k=k+1

    else:
        a[i]=arr[i-j]
        j=j+1
for i in range(n):
    arr[i]=a[i]
于 2020-05-11T05:08:17.370 回答