繁体   English   中英

Python-比较和排序两个数字列表并按升序返回它们

[英]Python-compare and sort two lists of numbers and return them in ascending order

我有两个清单:

list_1 = [1,1, 2,2,2, 3,3, 4, 4, 4 ,4, 4, 5,5,5,5]
list_2 = [5, 5, 5, 6, 6, 7]

我想返回仅出现在第一个列表中但不在第二个列表中的元素列表,并且该列表应按升序排序,因此结果如下:

[1, 3, 2, 4]

到目前为止,我有这个:

def sorted_nums(list_1,list2_2):
    c = (set(list_1) - set(list_2))
    d = dict.fromkeys(c, 0)
    for index in list_1:
        if index in c:
            d[index]+=1
    return d
a = sorted_nums(list_1,list_2)
b = sorted(a.items(), key = lambda x: x[1])
print(b)

它返回这个:

[(1,2), (3,2), (2,3), (4,5)]

您能帮我更改代码的最后一部分,以便得到我想要的结果吗?

您只想按 lis1 的出现(计数)排序,从第二个列表中获取集合差异:

>>> list1 = [1,1, 2,2,2, 3,3, 4, 4, 4 ,4, 4, 5,5,5,5]
>>> list2 = [5, 5, 5, 6, 6, 7]
>>> from collections import Counter
>>> counts1 = Counter(list1)
>>> sorted(counts1.keys() - set(list2), key=counts1.get)
[1, 3, 2, 4]

方法一:

通过使用sorted(array, key = list_1.count)按出现排序,但这会很慢。

list_1 =  [1,1, 2,2,2, 3,3, 4, 4, 4 ,4, 4, 5,5,5,5]
list_2 = [5, 5, 5, 6, 6, 7]

def sorted_nums(list_1,list2_2):
    return sorted(list(set(list_1) - set(list_2)), key = list_1.count)

sorted_nums(list_1, list_2)

方法2 (学分)

通过使用Counter 这是一种更快的方法。

from collections import Counter

list_1 =  [1,1, 2,2,2, 3,3, 4, 4, 4 ,4, 4, 5,5,5,5]
list_2 = [5, 5, 5, 6, 6, 7]
counter_1 = Counter(list1)
def sorted_nums(list_1,list2_2):
    return sorted(counter_1.keys() - set(list2), key=counter_1.get)

sorted_nums(list_1, list_2)

Output:

[1, 3, 2, 4]

使用set操作

a =  [1,1, 2,2,2, 3,3, 4, 4, 4 ,4, 4, 5,5,5,5]
b =  [5, 5, 5, 6, 6, 7]

c = sorted(list(set(a) - set(b)))
print(c)

您只需要一个map

def sorted_nums(list_1,list2_2):
    c = (set(list_1) - set(list_2))
    d = dict.fromkeys(c, 0)
    for index in list_1:
        if index in c:
            d[index]+=1
    return d

list_1 = [1,1, 2,2,2, 3,3, 4, 4, 4 ,4, 4, 5,5,5,5] 
list_2 = [5, 5, 5, 6, 6, 7]
a = sorted_nums(list_1,list_2)
b = list(map(lambda x:x[0],sorted(a.items(), key = lambda x: x[1]))) ## changed here
print(b)
[1, 3, 2, 4]

使用 map 在每个元素上应用map lambda x:x[0]

你已经把它整理好了。 所以你需要提取第一个元素?

print([x[0] for x in b])

如果您可以接受并列计数的默认顺序,这非常简洁:


from collections import Counter

list_1 = [1,1, 2,2,2, 3,3, 4, 4, 4 ,4, 4, 5,5,5,5]
list_2 = [5, 5, 5, 6, 6, 7]

blacklist = set(list_2)

c = Counter(i for i in list_1 if i not in blacklist)

[n for n,_ in reversed(c.most_common())]

output 为[3, 1, 2, 4] (注意 1 和 3 的计数相同)。

如果您可以依赖 list_1 的顺序与示例中的顺序相同(所有集群都是连续的),则可以使用itertools.groupby做得更好:

import itertools.groupby

c = sorted((len(list(g)), i) for i, g in itertools.groupby(list_1) if i not in blacklist)

[i for _, i in c]

Output 是[1, 3, 2, 4]

我对这个问题的解释使我想到了这一点:

list_1 = [1,1, 2,2,2, 3,3, 4, 4, 4 ,4, 4, 5,5,5,5]
list_2 = [5, 5, 5, 6, 6, 7]

d = dict()
list_2_s = set(list_2)
for e in list_1:
    if e not in list_2_s:
        d[e] = d.setdefault(e, 0) + 1
lst = [t[1] for t in sorted((v, k) for k, v in d.items())]
print(lst)

Output:

[1, 3, 2, 4]

list(dict.fromkeys(list_1))这与set相同,但保持元素的顺序

def sorted_nums(list_1,list_2):
return tuple(filter(lambda x: x not in set(list_2), list(dict.fromkeys(list_1))))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM