比较python中的两个多维数组

时间:2018-02-01 19:00:07

标签: python arrays uid

我的列表看起来像这样(这些数字是UIDS):

sysusers = [['user1'],[7972],
           ['user2'],[2121],
           ['hacker'],[2132]
           ['hacker2'],[1232]]
users = [['user1'],[7972],
        ['user2'],[2121]]

我想区分这两个列表并将其输出到类似于此的列表中:

badusers = [hacker,
           hacker2]

1 个答案:

答案 0 :(得分:1)

您遇到的第一个问题是列表的结构方式。

每个列表之间需要有逗号分隔。

sysusers = [['user1'],[7972],
           ['user2'],[2121],
           ['hacker'],[2132],
           ['hacker2'],[1232]]

users = [['user1'],[7972],
        ['user2'],[2121]]

一旦这是正确的,访问您需要的数据很简单。

goodusers = []

for i in users:
    for j in i:
        for k in sysusers:
            for l in k:
                if l == j:
                    goodusers.append(k)

badusers = [x for x in sysusers if x not in goodusers]
print(badusers)

[['hacker'], [2132], ['hacker2'], [1232]]