如何从列表中查找常见元素,不常见元素应在列表中

时间:2019-07-12 06:20:18

标签: python-3.x

我有任意数量的列表,假设有4个列表。

[['a = 10', 'b = 20', 'display( "Enter a number: " )', 'num = Any. UI is ON ', 'num < 0', 'display( "Wrong input" )\n'], ['a = 10', 'b = 20', 'display( "Enter a number: " )', 'num = Any. UI is ON ', 'num >= 0', 'display( "Positive number" )', 'num == 0', 'display( "Num is 0" )', 'num < 3', 'display( "Num is less than 3" )\n'], ['a = 10', 'b = 20', 'display( "Enter a number: " )', 'num = Any. UI is ON ', 'num >= 0', 'display( "Positive number" )', 'num == 0', 'display( "Num is 0" )\n'], ['a = 10', 'b = 20', 'display( "Enter a number: " )', 'num = Any. UI is ON ', 'num >= 0', 'display( "Positive number" )']].

首先它应该检查第一个列表中的前两个列表中不常见的元素将放在一个列表中,第二个不常见元素在另一个列表中。我将合并所有这些内容,然后我将检查此更新的列表以及第三个列表是否与第三个列表相同并进一步获得第四名。 所以我想要这种类型的最终清单

['a = 10', 'b = 20','display("Enter the number:")','num = Any. UI is ON',[['num < 0','display( "Wrong input" )'],['num >= 0','display( "Positive number" )',['num == 0', 'display( "Num is 0" )'['num < 3', 'display( "Num is less than 3" )]]]]

pycharm

def processpathwaysList(pathList):
    if len(pathList) == 1:
        baseLine = pathList[0]
        return baseLine
    else:
        baseLine = pathList[0]
        for index in range(1, len(pathList)):
            pathIndex = 0
            pathindexList = pathList[index]
            tempList = []
            merge = []
            while pathIndex < len(pathindexList):
                # comparing items of two lists
                if isinstance(baseLine[pathIndex],str):
                    if baseLine[pathIndex] == pathindexList[pathIndex]:
                        tempList.append(baseLine[pathIndex])
                    else:
                        break
                elif isinstance(baseLine[pathIndex],list):
                    pass
                pathIndex += 1
            if len(pathindexList[pathIndex:]) > 0:
                merge.append(pathindexList[pathIndex:])
            merge.append(baseLine[pathIndex:])
            if merge == [[]]:
                pass
            else:
                tempList.append(merge)
            # common logical list
            baseLine = tempList
        return baseLine

0 个答案:

没有答案