2个列表之间的列表比较

时间:2021-03-13 09:01:29

标签: python python-3.x list

s=[10,10,20,10,10,20]

t=[20,20,10,20,20,30]
  • s 中 10 的频率为 4,t 中为 1 的频率为 3

  • s 中 20 的频率为 2,t 中为 4 的频率差为 2

  • s 中 30 的频率为 0,t 中为 1 的频率为 1

两个列表中 10,20 和 30 的出现次数相差不超过 3 则返回 True 否则返回 False

如何解决上述问题。我正在使用 set 和 difference 函数,但无法获得预期的 o/p 。有人可以为我提供解决上述问题的提示

7 个答案:

答案 0 :(得分:0)

您可以使用 collections.Counter 获取列表中每个元素的数量:

import collections

s = [10, 10, 20, 10, 10, 20]
t = [20, 20, 10, 20, 20, 30]

collections.Counter(s)
# >> Counter({10: 4, 20: 2})

collections.Counter(t)
# >> Counter({20: 4, 10: 1, 30: 1})

collections.Counter(t)[20]
# >> 4

答案 1 :(得分:0)

使用列表的计数方法可以得到不同的

>>> s=[10,10,20,10,10,20]
>>> 
>>> t=[20,20,10,20,20,30]
>>> 
>>> 
>>> s.count(10)-t.count(10)
3
>>> 

答案 2 :(得分:0)

这确实闻起来像作业:D 但以防万一你通常被卡住,这里是:

s=[10,10,20,10,10,20]
t=[20,20,10,20,20,30]

set_s = set(s)
set_t = set(t)


set_max = set_s if len(set_s) > len(set_t) else set_t

count_dict = {}

for e in set_max:
    if e in s and e in t:
        count_dict[e] = abs(s.count(e) - t.count(e))
    elif e in s:
        count_dict[e] = s.count(e)
    else:
        count_dict[e] = t.count(e)
print(count_dict)

答案 3 :(得分:0)

从合并的列表中获取所有唯一值

st = set(s+t)

现在,您可以找到计数的差异:

diff={}
for val in st:
    diff[val] = abs(s.count(val) - t.count(val))
print(diff)

输出

{10: 3, 20: 2, 30: 1}

现在,您有一本字典,其中包含您可以使用的不同值以及您想要的方式

答案 4 :(得分:0)

a = [10,10,10,20,10,20]
t = [20,20,10,20,20,30]
def difference_calc(a,t):
    for i in set(a).union(set(t)): 
        if abs(a.count(i)-t.count(i)) > 3:
           return False
    return True
print(difference_calc(a,t))

答案 5 :(得分:0)

这是一个可以解决您发布的问题的功能。

#  this is a function that solves the problem

def list_diff(s, t):
    # sorting the lists
    s.sort()
    t.sort()

    # dicts for the numbers in s and t
    xs_in_s = {}
    xs_in_t = {}

    # For loop to populate the dicts with the frequencies of the number in s and t
    for xs in s:
        xs_in_s[xs] = s.count(xs)

        # this is to add numbers that are in 's' but not in 't' into 't'
        if xs not in t: xs_in_t[xs] = 0

    for xs in t:
        xs_in_t[xs] = t.count(xs)

        # this is to add numbers that are in 't' but not in 's' into 's'
        if xs not in s: xs_in_s[xs] = 0

    # After these for loops
    # xs_in_s = {10: 4, 20: 2, 30: 0}
    # xs_in_t = {20: 4, 10: 1, 30: 1}


    # to test for the difference of the datas in the two dicts
    # since the keys in 'xs_in_s' is the same as that of 'xs_in_t'
    # any of 'xs_in_s' or 'xs_in_t' can be used

    for x in xs_in_s:
        xs = xs_in_s[x]
        xt = xs_in_t[x]
        difference = xs - xt
        abs_diff = abs(difference)
        if abs_diff > 3: return False

    # the next code will only execute if the 'abs_diff > 3 == False' for all the keys in the dict
    return True

s=[10,10,20,10,10,20]
s1=[10,10,20,10,10,20, 10, 10]

t=[20,20,10,20,20,30]

true = list_diff(s, t)


print(true)

答案 6 :(得分:0)

<块引用>

我正在使用集合和差分函数

改用 Counter 差异:

from collections import Counter

def check(s, t):
    c = Counter(s)
    c.subtract(t)
    return max((c | -c).values()) <= 3