我有result列表,其中有两个有序项,例如A,B和B,A组合,或者可以是1,2和2,1等,如果我们发现A,B或1,2或3211有任何反向组合,3222或任何反转的梳子应存储在target_list中,正确排序的梳子应存储在source_list中。另外,如果我们找不到任何反向组合,则将其添加到source_list,但是如果找到,target_list必须仅包含source_list中项目的反向组合。

这是我的代码,我只得到A,B的组合,您能告诉我如何像A一样动态地做吗,可以代替B且可以满足上述条件的任何东西

result = [('A','B', 'IP1','GP1'), ('B', 'C', 'IP2','GP2'),('A', 'C', 'IP3','GP2'),('A','B', 'IP4','GP2'),('D', 'Z', 'IP5','GP2'),('B', 'A', 'IP6','GP2'), ('C','B','IP7','GP2'), ('C', 'A', 'IP8','GP2'),('C','B','IP9','GP2')]
a=[i for i,v in enumerate(result) if v[:2]==('B','A')]
Source_list,target_list=result[:a[0]],result[a[0]:]
print(Source_list)
print(target_list)


输出:


  [('A','B','IP1','GP1'),('B','C','IP2','GP2'),('A','C','IP3', ''GP2'),('A','B','IP4','GP2'),('D','Z','IP5','GP2')]
  [('B','A','IP6','GP2'),('C','B','IP7','GP2'),('C','A','IP8', 'GP2'),('C','B','IP9','GP2')]

最佳答案

我不太确定你到底是什么,但你会告诉我...

def kamoulox(results):
    source_list = []
    target_list = []
    combis = set((result[0], result[1]) for result in results if result[0] <= result[1])
    for combi in combis:
        i = 0
        while i < len(results):
            if results[i][0:2] == combi:
                source_list.append(results.pop(i))
            elif results[i][0:2] == combi[::-1]:
                target_list.append(results.pop(i))
            else:
                i += 1
    return source_list, target_list

results = [
    ('32891', '32822', 'EKRGMD92-vMME-01', '10.88.158.81'),
    ('32822', '32891', 'EKRGMD92-vMME-02', '10.88.159.113'),
    ('32822', '32891', 'HRSNNJAQ-vMME-01', '10.88.162.81'),
    ('32822', '32891', 'HRSNNJAQ-vMME-02', '10.88.163.113'),
    ('32822', '32781', 'EKRGMD92-vMME-02', '10.88.159.113'),
    ('32822', '32781', 'HRSNNJAQ-vMME-01', '10.88.162.81'),
    ('32822', '32781', 'HRSNNJAQ-vMME-02', '10.88.163.113'),
    ('33033', '32891', 'EKRGMD92-vMME-02', '10.88.159.113'),
    ('33033', '32891', 'HRSNNJAQ-vMME-01', '10.88.162.81'),
    ('33033', '32891', 'HRSNNJAQ-vMME-02', '10.88.163.113'),
    ('33033', '32822', 'EKRGMD92-vMME-01', '10.88.158.81'),
    ('33033', '32781', 'EKRGMD92-vMME-02', '10.88.159.113'),
    ('33033', '32781', 'HRSNNJAQ-vMME-01', '10.88.162.81'),
    ('33033', '32781', 'HRSNNJAQ-vMME-02', '10.88.163.113'),
    ('32781', '32891', 'KSCYMOEC-MME-03', '10.148.9.19'),
    ('32781', '32822', 'EKRGMD92-vMME-01', '10.88.158.81'),
    ('32781', '32822', 'KSCYMOEC-MME-03', '10.148.9.19'),
    ('32781', '33033', 'KSCYMOEC-MME-03', '10.148.9.19')
]
source_list, target_list = kamoulox(results)

# source_list contains good ordered items (with or without reversed equivalent)
# target_list contains wrong ordered items with at least one equivalent into source_list
# results now contains wrong ordered items without good ordered equivalent

关于python - 我想在比较列表中的项目时将列表分为两个列表right_order []和reverse_order [],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53333305/

10-14 18:03