[英]Python-compare and sort two lists of numbers and return them in ascending order
I have two lists:我有两个清单:
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]
I want to return the list of elements that appear only in the first list but not in the second and the list should be sorted ascending so the result is like this:我想返回仅出现在第一个列表中但不在第二个列表中的元素列表,并且该列表应按升序排序,因此结果如下:
[1, 3, 2, 4]
So far I have this:到目前为止,我有这个:
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)
and it returns this:它返回这个:
[(1,2), (3,2), (2,3), (4,5)]
Could you help me to change the last part of the code so that I get the result I want?您能帮我更改代码的最后一部分,以便得到我想要的结果吗?
You just want to sort by the occurance (counts) of lis1, taking the set-difference from the second list:您只想按 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]
By using sorted(array, key = list_1.count)
to sort by occurrence but this will be slow.通过使用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)
By using Counter
.通过使用Counter
。 This is a faster approach.这是一种更快的方法。 : :
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: Output:
[1, 3, 2, 4]
use set
operations使用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)
All you need is a map
您只需要一个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]
Apply function lambda x:x[0]
over each and every elements using map
使用 map 在每个元素上应用map
lambda x:x[0]
You already have it sorted.你已经把它整理好了。 So you need to extract the first element?所以你需要提取第一个元素?
print([x[0] for x in b])
If you're OK with the default order for tied counts, this is quite succinct:如果您可以接受并列计数的默认顺序,这非常简洁:
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())]
The output is [3, 1, 2, 4]
(note the counts for 1 and 3 are the same). output 为[3, 1, 2, 4]
(注意 1 和 3 的计数相同)。
If you can rely on the ordering of your list_1 to be as it is in your example (all clusters are consecutive) you can do a bit better using itertools.groupby
:如果您可以依赖 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 is [1, 3, 2, 4]
Output 是[1, 3, 2, 4]
My interpretation of the question leads me to this:我对这个问题的解释使我想到了这一点:
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: Output:
[1, 3, 2, 4]
list(dict.fromkeys(list_1))
this is working same as set
but keep order of elements 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.