比较python中的两个列表,并在某些条件下检查它们是否相等

问题描述 投票:2回答:2

我是python和pandas的新手。在这里,我有以下具有两个列表的数据框。

Test             Test1
[1,1,1]          [1,2,2]
[1,2,2]          [1,0,1]
[1,0,0]          [1,1,1]
[2,2,2]          [0,0,2]

在此datframe中,我正在尝试对两个列表进行补全。有些条件只能返回true。因此,

如果任一端具有2个0和1个正值,而另一端具有相同的正值,则应返回True,否则返回False。

所以在这种情况下

[1,0,0]          [1,1,1]    
[2,2,2]          [0,0,2]

这两个都将返回true。

现在,我尝试过的是这样的

def check_two_zeros_onEither_side(tup1,tup2):
    count_on_previous = tup1.count(0)
    count_on_next = tup1.count(0)
    rem1 = [x for x in tup1 if x != 0]
    rem2 = [x for x in tup2 if x != 0]
    if count_on_previous == 2:
        if all([rem1[0] == rem2[0], rem1[0] == rem2[1]]):

但是在这里,我无法处理某些异常情况,例如,索引超出范围。。有什么可以帮助我的吗?谢谢。以及我该如何实现呢?

def check_two_zeros_onEither_side(tup1,tup2,ins):
    count_on_previous = tup1.count(0)
    count_on_next = tup2.count(0)
    print(count_on_previous,count_on_next)
    rem1 = [x for x in tup1 if x != 0]
    rem2 = [x for x in tup2 if x != 0]
    print(rem1,rem2,len(tup1),len(tup2))
    if count_on_previous == 2 and len(rem1) == 1 and len(rem2) == 3:
        if all( [rem1[0] == rem2[0], rem1[0] == rem2[1], rem1[0] == rem2[2]]):
            print("GOin insde one",ins)
            return True
    elif count_on_next == 2 and len(rem2) == 1 and len(rem1) == 3:
        if all([rem2[0] == rem1[0], rem2[0] == rem1[1], rem2[0] == rem1[2]]):
            print("GOin insde two",ins)
            return True
        else:
            return False
    else:
        return False

这是我尝试过的。正在运行,但是还有其他方法可以这样做吗?

python python-3.x pandas numpy
2个回答
0
投票

以下内容

def compare(a, b):
    # Ensure there are two zeros on either side
    if a.count(0) == 2 or b.count(0) == 2:
        # Compute the intersection and ensure it's not zero
        return len(set(a).intersection(b)) is not 0
    return False

这里是使用Pytest进行的测试。

import pytest

class TestDataframes:

    def test_frame_a_contains_two_zeros(self):
        a = [0, 0 ,1]
        b = [1, 1, 1]

        assert compare(a, b) is True


    def test_frame_b_contains_two_zeros(self):
            a = [2, 2, 2]
            b = [0, 0, 2]

            assert compare(a, b) is True

    def test_both_frames_contains_two_zeros(self):
            a = [0, 0, 2]
            b = [0, 0, 2]

            assert compare(a, b) is True

    def test_neither_contain_two_zeros(self):
            a = [0, 1, 2]
            b = [0, 1, 2]

            assert compare(a, b) is False

    def test_positive_number_doesnt_match(self):
            a = [2, 2, 2]
            b = [0, 0, 1]

            assert compare(a, b) is False

compare函数将返回以下内容。

[0, 0 ,1] [1, 1, 1] #=> True
[2, 2, 2] [0, 0, 2] #=> True
[0, 0, 2] [0, 0, 2] #=> True
[0, 1, 2] [0, 1, 2] #=> False
[0, 2, 2] [0, 0, 1] #=> False

0
投票

您可以尝试:

import itertools
#this assumes the dataframe has 2 columns of lists
c1=df[['Test','Test1']].applymap(lambda x: sum([i==0 for i in x])).eq(2).any(1)
c2=(df[['Test','Test1']].apply(lambda x: 
  len(set(filter((0).__ne__,itertools.chain.from_iterable(x)))),axis=1).eq(1))
df[c1&c2]

        Test      Test1
2  [1, 0, 0]  [1, 1, 1]
3  [2, 2, 2]  [0, 0, 2]
© www.soinside.com 2019 - 2024. All rights reserved.