好的,所以我一直在这方面做了一点,我仍然用python得到我的印章,这似乎暂时超出了我。我有一个元组列表和普通旧列表(可能是错误的确切术语,如果是这样,请让我知道如何正确陈述它。我喜欢学习)。我试图将每个元组中的值与列表中的值进行比较,并返回所有唯一元组。所以这是我的代码(修改为一些内部类型的东西):
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(限制输出,因为在过滤时几乎有3.8k响应)
['A1BCEFG001', 'A1BCEFG339', 'A1BCEFGDED444', 'A1BCEFG343', 'A1BCEFG2700', 'A1BCEFG1133', 'A1BCEFGDED321', 'A1BCEFG347', 'A1BCEFG371', 'A1BCEFG373']
和
[(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)]
我也需要将这些从预期结果中删除。有人能提供包含第一个问题和这个问题的解决方案吗?
答案 0 :(得分:1)
filtered = [tup for tup in soapfiltered if tup[3] == 'Live' and tup[1] not in apifiltered]