代码之家  ›  专栏  ›  技术社区  ›  Kyu96

对字符串列表进行数字排序并筛选重复项?

  •  1
  • Kyu96  · 技术社区  · 4 年前

    提供以下格式的字符串列表:

    [
        "464782,-100,4,3,1,100,0,0"
        "465042,-166.666666666667,4,3,1,100,0,0",
        "465825,-250.000000000001,4,3,1,100,0,0",
        "466868,-166.666666666667,4,3,1,100,0,0",
        "467390,-200.000000000001,4,3,1,100,0,0",
        "469999,-100,4,3,1,100,0,0",
        "470260,-166.666666666667,4,3,1,100,0,0",
        "474173,-100,4,3,1,100,0,0",
        "474434,-166.666666666667,4,3,1,100,0,0",
        "481477,-100,4,3,1,100,0,1",
        "531564,259.011439671919,4,3,1,60,1,0",
        "24369,-333.333333333335,4,3,1,100,0,0",
        "21082,410.958904109589,4,3,1,60,1,0",
        "21082,-250,4,3,1,100,0,0",
        "22725,-142.857142857143,4,3,1,100,0,0",
        "23547,-166.666666666667,4,3,1,100,0,0",
        "24369,-333.333333333335,4,3,1,100,0,0",
        "27657,-200.000000000001,4,3,1,100,0,0",
        "29301,-142.857142857143,4,3,1,100,0,0",
        "30123,-166.666666666667,4,3,1,100,0,0",
        "30945,-250,4,3,1,100,0,0",
        "32588,-166.666666666667,4,3,1,100,0,0",
        "34232,-250,4,3,1,100,0,0",
        "35876,-142.857142857143,4,3,1,100,0,0",
        "36698,-166.666666666667,4,3,1,100,0,0",
        "37520,-250,4,3,1,100,0,0",
        "42451,-142.857142857143,4,3,1,100,0,0",
        "43273,-166.666666666667,4,3,1,100,0,0",
    ]
    

    如何使用python根据每行中的第一个数字对列表进行排序? 然后,一旦排序,删除所有重复,如果有的话?

    列表的排序条件是每行中第一个逗号之前的数字,该数字始终是整数。

    我尝试使用list .SoTo(),但是,这是按词序排序的,而不是数值的。

    4 回复  |  直到 4 年前
        1
  •  3
  •   jignatius    4 年前

    你可以用字典来做这个。关键字是第一个逗号前的数字,值是整个字符串。将消除重复项,但只存储特定数字字符串的最后一个匹配项。

    l = ['464782,-100,4,3,1,100,0,0',
    '465042,-166.666666666667,4,3,1,100,0,0',
    '465825,-250.000000000001,4,3,1,100,0,0',
    '466868,-166.666666666667,4,3,1,100,0,0',
    '467390,-200.000000000001,4,3,1,100,0,0',
    ...]
    
    d = {int(s.split(',')[0]) : s for s in l}
    result = [d[key] for key in sorted(d.keys())]
    
        2
  •  1
  •   OakenDuck    4 年前

    def sort_list(lis):
        nums = [int(num) if isdigit(num) else float(num) for num in lis]
    
        nums = list(set(nums))
        nums.sort()
    
        return [str(i) for i in nums]  # I assumed you wanted them to be strings.
    

    第一个会引起 TypeError 如果所有项目都在 lis ints , floats

    def sort_list(lis):
        ints = [int(num) for num in lis if num.isdigit()]
        floats = [float(num) for num in lis if not num.isdigit()]
    
        nums = ints.copy()
        nums.extend(floats)
        nums = list(set(nums))
        nums.sort()
    
        return [str(i) for i in nums]  # I assumed you wanted them to be strings.
    

    希望这有帮助。

        3
  •  1
  •   Errol    4 年前

    你可以试试这个。

    首先,我们需要使用set()删除列表中的重复项

    removed_duplicates_list = list(set(listr))
    

    然后我们将字符串列表转换为元组列表

    list_of_tuples = [tuple(i.split(",")) for i in removed_duplicates_list]
    

    然后我们使用sort()对它进行排序

    list_of_tuples.sort()
    

    完整的代码示例如下:

    listr = [
        "464782,-100,4,3,1,100,0,0"
        "465042,-166.666666666667,4,3,1,100,0,0",
        "465825,-250.000000000001,4,3,1,100,0,0",
        "466868,-166.666666666667,4,3,1,100,0,0",
        "467390,-200.000000000001,4,3,1,100,0,0",
        "469999,-100,4,3,1,100,0,0",
        "470260,-166.666666666667,4,3,1,100,0,0",
        "474173,-100,4,3,1,100,0,0",
        "474434,-166.666666666667,4,3,1,100,0,0",
        "481477,-100,4,3,1,100,0,1",
        "531564,259.011439671919,4,3,1,60,1,0",
        "24369,-333.333333333335,4,3,1,100,0,0",
        "21082,410.958904109589,4,3,1,60,1,0",
        "21082,-250,4,3,1,100,0,0",
        "22725,-142.857142857143,4,3,1,100,0,0",
        "23547,-166.666666666667,4,3,1,100,0,0",
        "24369,-333.333333333335,4,3,1,100,0,0",
        "27657,-200.000000000001,4,3,1,100,0,0",
        "29301,-142.857142857143,4,3,1,100,0,0",
        "30123,-166.666666666667,4,3,1,100,0,0",
        "30945,-250,4,3,1,100,0,0",
        "32588,-166.666666666667,4,3,1,100,0,0",
        "34232,-250,4,3,1,100,0,0",
        "35876,-142.857142857143,4,3,1,100,0,0",
        "36698,-166.666666666667,4,3,1,100,0,0",
        "37520,-250,4,3,1,100,0,0",
        "42451,-142.857142857143,4,3,1,100,0,0",
        "43273,-166.666666666667,4,3,1,100,0,0",
    ]
    
    removed_duplicates_list = list(set(listr))
    list_of_tuples = [tuple(i.split(",")) for i in removed_duplicates_list]
    list_of_tuples.sort()
    print(list_of_tuples) # the output is a list of tuples
    

    输出:

        [('21082', '-250', '4', '3', '1', '100', '0', '0'),
        ('21082', '410.958904109589', '4', '3', '1', '60', '1', '0'),
        ('22725', '-142.857142857143', '4', '3', '1', '100', '0', '0'),
        ('23547', '-166.666666666667', '4', '3', '1', '100', '0', '0'),
        ('24369', '-333.333333333335', '4', '3', '1', '100', '0', '0'),
        ('27657', '-200.000000000001', '4', '3', '1', '100', '0', '0'),
        ('29301', '-142.857142857143', '4', '3', '1', '100', '0', '0'),
        ('30123', '-166.666666666667', '4', '3', '1', '100', '0', '0'),
        ('30945', '-250', '4', '3', '1', '100', '0', '0'),
        ('32588', '-166.666666666667', '4', '3', '1', '100', '0', '0'),
        ('34232', '-250', '4', '3', '1', '100', '0', '0'),
        ('35876', '-142.857142857143', '4', '3', '1', '100', '0', '0'),
        ('36698', '-166.666666666667', '4', '3', '1', '100', '0', '0'),
        ('37520', '-250', '4', '3', '1', '100', '0', '0'),
        ('42451', '-142.857142857143', '4', '3', '1', '100', '0', '0'),
        ('43273', '-166.666666666667', '4', '3', '1', '100', '0', '0'),  
        ('464782','-100','4','3','1','100','0'),
        ('465042','-166.666666666667','4','3','1','100','0','0'),
        ('465825', '-250.000000000001', '4', '3', '1', '100', '0', '0'),
        ('466868', '-166.666666666667', '4', '3', '1', '100', '0', '0'),
        ('467390', '-200.000000000001', '4', '3', '1', '100', '0', '0'),
        ('469999', '-100', '4', '3', '1', '100', '0', '0'),
        ('470260', '-166.666666666667', '4', '3', '1', '100', '0', '0'),
        ('474173', '-100', '4', '3', '1', '100', '0', '0'),
        ('474434', '-166.666666666667', '4', '3', '1', '100', '0', '0'),
        ('481477', '-100', '4', '3', '1', '100', '0', '1'),
        ('531564', '259.011439671919', '4', '3', '1', '60', '1', '0')]
    
        4
  •  0
  •   ASI    4 年前

    我希望这会有帮助。 列表a.txt 在本例中,我将从文件中获取您的列表。。。我喜欢更有条理,并且有单独的文件,你也可以在python上做,但我的想法是你需要从列表中一个接一个地得到所有的元素( 虽然 功能或 函数,并通过检查新项是否已经存在,如果存在,则可以将它们添加到临时列表中,然后可以使用示例。 .sort() 因为会用数字和技巧。

    # Global variables
    file = "lista.txt"
    tempList = []
    
    # Logic get items from file
    def GetListFromFile(fileName):
        # Local variables
        showDoneMsg = True
    
        # Try to run this code
        try:
            # Open file and try to read it
            with open(fileName, mode="r") as f:
                # Define line
                line = f.readline()
                # For every line in file
                while line:
                    # Get out all end white space (\n, \r)
                    item = line.rstrip()
    
                    # Check if this item is not allready in the list
                    if item not in tempList:
                        # Append item to a temporar list
                        tempList.append(item)
                    # Show me if a itmes allready exist
                    else:
                        print("Dublicate >>", item)
    
                    # Go to new line
                    line = f.readline()
            # This is optional because is callet automatical
            # but I like to be shore
            f.close()
    
        # Execptions
        except FileNotFoundError:
            print("ERROR >> File do not exist!")
            showDoneMsg = False
    
        # Sort the list
        tempList.sort()
        # Show me when is done if file exist
        if showDoneMsg == True:
            print("\n>>> DONE <<<\n")
    
    # Logic show list items
    def ShowListItems(thisList):
        if len(thisList) == 0:
            print("Temporary list is empty...")
        else:
            print("This is new items list:")
            for i in thisList:
                print(i)
    
    # Execute function
    GetListFromFile(file)
    # Testing if items was sorted
    ShowListItems(tempList)
    

    ========================= RESTART: D:\Python\StackOverflow\help.py =========================
    Dublicate >> 43273,-166.666666666667,4,3,1,100,0,0
    
    >>> DONE <<<
    
    21082,-250,4,3,1,100,0,0
    21082,410.958904109589,4,3,1,60,1,0
    22725,-142.857142857143,4,3,1,100,0,0
    ...
    474434,-166.666666666667,4,3,1,100,0,0
    481477,-100,4,3,1,100,0,1
    531564,259.011439671919,4,3,1,60,1,0
    >>>