有没有办法以特定方式订购此列​​表?

时间:2019-04-06 11:26:49

标签: python sorting

所以我有这个列表:

[7, 31, 31, 61, 61, 79, 29, 79, 29, 103, 37, 103, 37, 47, 47, 53, 53, 89, 5, 89, 5, 13, 13, 83, 83, 101, 53, 101, 17, 53, 11, 17, 11, 59, 17, 59, 17, 41, 41, 79, 79, 97, 3, 97, 3, 41, 19, 41, 19, 53, 53, 67, 29, 67, 29, 73, 23, 73, 23, 43, 43, 71]

我想知道是否有一种方法可以对它们进行排序,例如,一对中的最后一个元素是第二对中的第一个元素,就像这样:

[7, 31, 31, 61, 61, 79, 79, 29, 29, 103, 103, 37 ....]
[pair1] [pair2] [pair3] [pair4] [pair5]  [pair6] ...

有没有办法做到这一点?

我已经尝试过循环,但是它的时间复杂度很高。

这是我的代码:


lis=[7, 31, 31, 61, 61, 79, 29, 79, 29, 103, 37, 103, 37, 47, 47, 53, 53, 89, 5, 89, 5, 13, 13, 83, 83, 101, 53, 101, 17, 53, 11, 17, 11, 59, 17, 59, 17, 41, 41, 79, 79, 97, 3, 97, 3, 41, 19, 41, 19, 53, 53, 67, 29, 67, 29, 73, 23, 73, 23, 43, 43, 71]
for i in range(0, len(lis)-4,4):
    if lis[i]==lis[i+2]:
        p=lis[i]
        lis[i]=lis[i+1]
        lis[i+1]=p
    elif lis[i]==lis[i+3]:
        p = lis[i]
        lis[i] = lis[i + 1]
        lis[i + 1] = p
        p=lis[i+2]
        lis[i+2]=lis[i+3]
        lis[i+3]=p
    elif lis[i+1]==lis[i+3]:
        p=lis[i+2]
        lis[i+2]=lis[i+3]
        lis[i+3]=p
print(lis)

4 个答案:

答案 0 :(得分:2)

如果第一个和第三个元素相等,听起来好像您想转置第二个和第三个元素。你会做类似的事情:

i = 0
while i < len(seq) - 2:
    if seq[i] == seq[i + 2]:
        # Swap the next two elements
        seq[i + 1], seq[i + 2] = seq[i + 2], seq[i + 1]
        i = i + 3
        continue
    i = i + 1

答案 1 :(得分:1)

您可以执行此操作,例如根据原始sort中的index进行list

>>> x
[7, 31, 31, 61, 61, 79, 29, 79, 29, 103, 37, 103, 37, 47, 47, 53, 53, 89, 5, 89, 5, 13, 13, 83, 83, 101, 53, 101, 17, 53, 11, 17, 11, 59, 17, 59, 17, 41, 41, 79, 79, 97, 3, 97, 3, 41, 19, 41, 19, 53, 53, 67, 29, 67, 29, 73, 23, 73, 23, 43, 43, 71]

# the `sorted` function takes a `key` function, that one can use to manipulate how the sort should be based on
# In this case, you could use `index` of the `element` in the `list` `x`,
# So, while the sort goes through each element, it check against the index in the original list, which is `x, thus it aligns each item based on the `index`.
>>> sorted(x, key=x.index) # note that you have more than a pair, so :)
[7, 31, 31, 61, 61, 79, 79, 79, 79, 29, 29, 29, 29, 103, 103, 37, 37, 47, 47, 53, 53, 53, 53, 53, 53, 89, 89, 5, 5, 13, 13, 83, 83, 101, 101, 17, 17, 17, 17, 11, 11, 59, 59, 41, 41, 41, 41, 97, 97, 3, 3, 19, 19, 67, 67, 73, 73, 23, 23, 43, 43, 71]

如果您真的很在意pair

>>> data = []
>>> for k in sorted(x, key=x.index): # `using direct x.index instead of useless lambda(i previously used) in this case, as @Austin suggested
...   if data.count(k) < 2: # add the element until it reaches count 2, which is `pair`, which is what you asked for
...     data.append(k)
... 
>>> data
[7, 31, 31, 61, 61, 79, 79, 29, 29, 103, 103, 37, 37, 47, 47, 53, 53, 89, 89, 5, 5, 13, 13, 83, 83, 101, 101, 17, 17, 11, 11, 59, 59, 41, 41, 97, 97, 3, 3, 19, 19, 67, 67, 73, 73, 23, 23, 43, 43, 71]

原因:

  

list.indexreturn第一个匹配的index。假设您有listx = [1,2,1,3]x.index(1)将始终为return 0。因此,如果使用sorted作为list.index的按键功能,则sort会有所不同

答案 2 :(得分:1)

这是一个很草率的解决方案,但它应该可以完成您的工作。

首先,我们将您的数据分成两对,然后再对这两对进行排序。

data = [7, 31, 31, 61, 61, 79, 29, 79, 29, 103, 37, 103, 37, 47, 47, 53, 53, 89, 5, 89, 5, 13, 13, 83, 83, 101, 53, 101, 17, 53, 11, 17, 11, 59, 17, 59, 17, 41, 41, 79, 79, 97, 3, 97, 3, 41, 19, 41, 19, 53, 53, 67, 29, 67, 29, 73, 23, 73, 23, 43, 43, 71]

def chunks(l, n):
    # For item i in a range that is a length of l,
    for i in range(0, len(l), n):
        # Create an index range for l of n items:
        yield l[i:i+n]

sliced_list = list(chunks(data, 2))

ordered_list = []

for a in sliced_list:
    # Sort the pairs
    ordered_list.append([a[0], a[1]] if a[0] < a[1] else [a[1], a[0]])

print(ordered_list)

答案 3 :(得分:0)

另一个糟糕的解决方案

import numpy as np
alist=np.array([7, 31, 31, 61, 61, 79, 29, 79, 29, 103, 37, 103, 37, 47, 47, 53, 53, 89, 5, 89, 5, 13, 13, 83, 83, 101, 53, 101, 17, 53, 11, 17, 11, 59, 17, 59, 17, 41, 41, 79, 79, 97, 3, 97, 3, 41, 19, 41, 19, 53, 53, 67, 29, 67, 29, 73, 23, 73, 23, 43, 43, 71])
l=alist.reshape(-1,2)

for row in range(1,len(l)):
    if abs(l[row-1,1]-l[row,1])<=abs(l[row-1,1]-l[row,0]):
        l[row]=np.roll(l[row],1)