【问题标题】:How to match duplicates and if match how to remove second one in list in python? [duplicate]如何匹配重复项以及如果匹配如何删除python列表中的第二个? [复制]
【发布时间】:2025-12-29 18:35:12
【问题描述】:

我有 API 列表, 输入 = [WriteConsoleA, WSAStartup, RegCloseKey, RegCloseKey, RegCloseKey, NtTerminateProces, RegCloseKey]

预期输出 = [WriteConsoleA, WSAStartup, RegCloseKey, NtTerminateProces, RegCloseKey]

【问题讨论】:

  • 我看到“RegCloseKey”元素在列表中出现了四次。那么列表中的第二个是什么意思?我没有得到你预期的输出

标签: python python-3.x portable-executable


【解决方案1】:

您可以简单地转换 set(list) 即 set(Input) 以删除所有重复项。

【讨论】:

    【解决方案2】:
    Input = ["WriteConsoleA", "WSAStartup", "RegCloseKey", "RegCloseKey", "RegCloseKey", "NtTerminateProces", "RegCloseKey"]
    Output = []
    api=Input[0]
    for index in range(1,len(Input)):
        if api!=Input[index]:
            Output.append(api)
            api=Input[index]
    Output.append(api)
    print(Output)
    

    试试这个,希望它适用于你的情况。

    【讨论】: