按特定顺序组合,比较和排序列表

时间:2016-01-27 06:33:38

标签: python list

list1 = [-1, 2, -3]
list2 = [4, -6, 3]
print(get_negatives_at_front(list1, list2))
print()
list1 = [1, 2, -3, 4, 7]
list2 = [4, -6, 3, -1]
print(get_negatives_at_front(list1, list2))

打印

[-1, -6, -3, 3, 2, 4]
[-6, -3, -1, 7, 4, 3, 2, 4, 1]

我不确定如何处理这个问题。我的第一次尝试是让我制作一个新的清单并且只是订购它:

list3 = list1 + [i for i in list2 if i not in list1]
list3 = sorted(list3)
return list3

但这不是问题的预期。我想我们需要使用:

for number in list1:
    if number < 0:

但我无法弄清楚该怎么做。

5 个答案:

答案 0 :(得分:1)

希望这是有益的。感谢。

def get_negatives_at_front(list1, list2):
   a = max(len(list2), len(list1))
   ### Make the length of lists equal by adding NONE item
   if len(list1) > len(list2):
      for d in range(0, len(list1) - len(list2)):
         list2.append(None)
   elif len(list2) > len(list1):
      for d in range(0, len(list2) - len(list1)):
         list1.append(None)
   ### add negative & positive items in two seperate lists while iterating
   neg = []
   pos = []
   for l in range(0, a):
     print l
     if list1[l] != None: ###Ignore none items which was added to make length equal to the other list
        if list1[l] < 0:
           neg.append(list1[l])
        else:
           pos.append(list1[l])
     if list2[l] != None:
        if list2[l] < 0:
           neg.append(list2[l])
        else:
           pos.append(list2[l])
   print neg+pos   ### join two lists

list1 = [1, 2, -3, 4, 7]
list2 = [4, -6, 3, -1]
print(get_negatives_at_front(list1, list2))

答案 1 :(得分:1)

如果允许您使用Python库,则可以使用zip_longest函数按顺序从两个列表中读取元素:

from itertools import zip_longest

def get_negatives_at_front(list1, list2):
    pos = []
    neg = []

    for v1, v2 in zip_longest(list1, list2):
        if v1 is not None:
            neg.append(v1) if v1 < 0 else pos.append(v1)
        if v2 is not None:
            neg.append(v2) if v2 < 0 else pos.append(v2)

    return neg + pos

list1 = [-1, 2, -3, 0]
list2 = [4, -6, 3]

print(get_negatives_at_front(list1, list2))

这将显示以下输出:

[-1, -6, -3, 4, 2, 3]

答案 2 :(得分:0)

根据您的场景轻松实现

def get_negatives_at_front(list1, list2):
    lp, ln = [], []
    for i in range(len(list1) if len(list1) >= len(list2) else len(list2)):
        if i < len(list1): ln.append(list1[i]) if list1[i] < 0 else lp.insert(0, list1[i])
        if i < len(list2): ln.append(list2[i]) if list2[i] < 0 else lp.insert(0, list2[i])
    return ln + lp

get_negatives_at_front([-1, 2, -3], [4, -6, 3])
> [-1, -6, -3, 3, 2, 4]

get_negatives_at_front([1, 2, -3, 4, 7], [4, -6, 3, -1])
> [-6, -3, -1, 7, 4, 3, 2, 4, 1]

答案 3 :(得分:0)

基本上,您希望使用zip从两个列表中创建一对元素。由于列表的长度可以是任意的并且可能不匹配,因此最好使用来自itertools的izip_longest,尽管默认情况下会使用None“填充”缺少的元素。然后我们只执行检查并使用两个辅助列表(正数,负数),并根据我们迭代的值,我们将其附加到否定列表的末尾,或者在正数的前面推送,然后连接两个输出之前。在代码中:

from itertools import izip_longest

def get_negatives_at_front(l1, l2):
    negatives = []
    positives = []
    # This will alternate on elements from list1,list2:
    # In case of padding needed, this will put "None"
    for x,y in izip_longest(l1, l2):
        # check if not None
        if x is not None:
            negatives.append(x) if x < 0 else positives.append(x)
        if y is not None:
            negatives.append(y) if y < 0 else positives.append(y)

    # concat the two lists after reversing the positives list
    return negatives+list(reversed(positives))

list1 = [-1, 2, -3]
list2 = [4, -6, 3]
print get_negatives_at_front(list1, list2)

这将为您提供以下结果:

[-1, -6, -3, 3, 2, 4]

另见以下内容: https://eval.in/509593

答案 4 :(得分:0)

使用chaindeque

from collections import deque 
from itertools import chain

def get_negatives_at_front(l1, l2):
    res = deque()
    for i in chain(l1, l2):
        if i < 0:
            res.appendleft(i)
        else:
            res.append(i)
    return list(res)

比较其他答案的时间安排:

list1 = [random.randint(-100, 100) for _ in range(100000)]
list2 = [random.randint(-100, 100) for _ in range(100000)]

%timeit get_negatives_at_front(list1, list2) 
# 10 loops, best of 3: 42 ms per loop

%timeit get_negatives_at_front_Rick(list1, list2)
#1 loops, best of 3: 5.85 s per loop

%timeit get_negatives_at_front_Trying2Learn(list1, list2)
#10 loops, best of 3: 89.8 ms per loop

%timeit get_negatives_at_front_Sal(list1, list2)
#1 loops, best of 3: 5.72 s per loop

%timeit get_negatives_at_front_martin_evans(list1, list2)
#10 loops, best of 3: 52.9 ms per loop

#%timeit get_negatives_at_front_Minitoto(list1, list2)
#Does not work in python 3

修改
最初OP的问题包含函数返回值的描述

  

该函数返回一个包含两者中所有元素的新列表   参数列出了前面的所有负数   列表和列表后面的所有正数。

我从中得出正面和负面的内部排序并不重要。所以这只能确保负面在积极面前。

因此,对于示例输入,它提供:[-6, -3, -1, 2, 4, 3]而不是[-1, -6, -3, 3, 2, 4]