将带有元组的列表与列表和 return 唯一元组进行比较

Compare List with tuples against list and return unique tuples

好的,所以我已经做了一段时间了,我仍然在使用 python 进行排骨,这似乎暂时超出了我的范围。我有一个元组列表和普通的旧列表(确切的术语可能是错误的,如果是这样请告诉我如何正确陈述它。我喜欢学习)。我正在尝试将每个元组中的值与列表中的值和 return 所有唯一元组进行比较。所以这是我的代码(修改为其中一些是内部类型的东西):

from suds.client import Client
from suds.transport.https import WindowsHttpAuthenticated
import requests, base64


soapurl = 'soapurl'
username = base64.b64decode('base64codehere').split(':')[0]
password = base64.b64decode('base64codehere').split(':')[1]
apiuser = base64.b64decode('differentbase64codehere').split(':')[0]
apipass = base64.b64decode('differentbase64codehere').split(':')[1]
ntlm = WindowsHttpAuthenticated(username=username, password=password)
client = Client(soapurl, transport=ntlm)
result = client.service.method(serverName='a1bcefg')
soapfiltered = []
for item in result[0]:
    soapfiltered.extend(list(item[i] for i in [0, 1, 6, 7]))
soapfiltered = zip(*[iter(soapfiltered)]*4)
apifiltered = []
for i in range(0, 11000, 1000):
    apiUrl = 'apiurl/?sort=id&limit=0&offset=' + str(i) +'&order=desc'
    apiData = requests.get(apiUrl, verify=False, auth=(apiuser, apipass)).json()
    for response in apiData['data']:
        apifiltered.append(str(response['hostname']))

这基本上给了我这个:

apifiltered(限制输出,因为 apifiltered 中几乎有 3.8k 个响应)

['A1BCEFG001', 'A1BCEFG339', 'A1BCEFGDED444', 'A1BCEFG343', 'A1BCEFG2700', 'A1BCEFG1133', 'A1BCEFGDED321', 'A1BCEFG347', 'A1BCEFG371', 'A1BCEFG373']

soapfiltered(限制输出,因为在 soapfiltered 中有大约 4k 响应)

[(63258, A1BCEFG001, A5001195, Live), (63259, A1BCEFGDED444, A5001196, Live), (63260, A1BCEFGDED321, A5001194, Live), (63261, A1BCEFGDED2743, A5001199, Live), (63262, A1BCEFG339, A5001193, Live), (63263, A1BCEFGDED2745, A5001197, Live), (63264, A1BCEFGDED2746, A5001206, Live), (63265, A1BCEFGDED2747, A5001202, Live), (63266, A1BCEFGDED2748, A5001205, Live), (63387, A1BCEFG1193, A5004312, Live)]

所以,如果可能的话,我想要完成的是这样的:

[(63261, A1BCEFGDED2743, A5001199, Live), (63263, A1BCEFGDED2745, A5001197, Live), (63264, A1BCEFGDED2746, A5001206, Live), (63265, A1BCEFGDED2747, A5001202, Live), (63266, A1BCEFGDED2748, A5001205, Live), (63387, A1BCEFG1193, A5004312, Live)]

我已经查看了其他解决方案,但似乎无法获得所需的结果。任何帮助将不胜感激,因为我会从头开始继续努力。如果需要任何其他信息,请询问,我会尽我所能提供。谢谢。

编辑澄清(TLDR;): 我有一个列表:

apifiltered = ['A1BCEFG001', 'A1BCEFG339', 'A1BCEFGDED444', 'A1BCEFG343', 'A1BCEFG2700', 'A1BCEFG1133', 'A1BCEFGDED321', 'A1BCEFG347', 'A1BCEFG371', 'A1BCEFG373']

和一个元组列表:

soapfiltered = [(63258, A1BCEFG001, A5001195, Live), (63259, A1BCEFGDED444, A5001196, Live), (63260, A1BCEFGDED321, A5001194, Live), (63261, A1BCEFGDED2743, A5001199, Live), (63262, A1BCEFG339, A5001193, Live), (63263, A1BCEFGDED2745, A5001197, Live), (63264, A1BCEFGDED2746, A5001206, Live), (63265, A1BCEFGDED2747, A5001202, Live), (63266, A1BCEFGDED2748, A5001205, Live), (63387, A1BCEFG1193, A5004312, Live)]

我想将 soapfiltered 与 apifiltered 进行比较,并从 soapfiltered 中删除任何元组,其中 soapfiltered 中每个元组的 [1] 值与在 apifiltered 中找到的值相匹配。

预期结果:

expected = [(63261, A1BCEFGDED2743, A5001199, Live), (63263, A1BCEFGDED2745, A5001197, Live), (63264, A1BCEFGDED2746, A5001206, Live), (63265, A1BCEFGDED2747, A5001202, Live), (63266, A1BCEFGDED2748, A5001205, Live), (63387, A1BCEFG1193, A5004312, Live)]

又一次编辑:

@szabad 给出了很好的答案:

expected = [tup for tup in smdbfiltered if tup[1] not in apifiltered]

所以这很好用,但是有一些元组 tup[3] 不是 'Live',如下所示:

[(56536, A1BCEFG744-r, Retired, Prod 1CP)]

我也需要从预期结果中删除这些。有人可以提供包含第一个问题和这个问题的解决方案吗?

 filtered = [tup for tup in soapfiltered if tup[3] == 'Live' and tup[1] not in apifiltered]