【问题标题】:Sorting a list based off of values inside its elements根据元素内的值对列表进行排序
【发布时间】:2018-10-12 19:04:03
【问题描述】:

我有一个包含元素的列表:

['16:11:40.894 0,Type=IsXover,Count=1,lp-isD=2'
 '16:11:40.894 0,Type=IsXover,Count=54,lp-xsD=1'
 '16:11:40.894 0,Type=IsXover,Count=201,lr-isD=3'
 '16:11:40.894 0,Type=IsXover,Count=3075,lp-gsD=5']

我想根据“Count=”之后的数字对该列表进行排序。 我 不能 使用 .sort(key=lambda x: x[37]) 来做到这一点,正如 here 所说的那样,因为我的数字变成了双、三、...数字。如何在不使用正则表达式的情况下对该列表进行排序?

(请不要列出很长的列表,我写了上面列表的摘要版本)

【问题讨论】:

  • 不允许使用正则表达式?这让事情变得更加困难。
  • sorted(lst, key=lambda x: int(x.split('Count=', 1)[1].split(',', 1)[0]))

标签: python list sorting


【解决方案1】:

这样做:

to_sort = ['16:11:40.894 0,Type=IsXover,Count=201,lr-isD=3',
           '16:11:40.894 0,Type=IsXover,Count=54,lp-xsD=1',
           '16:11:40.894 0,Type=IsXover,Count=1,lp-isD=2',
           '16:11:40.894 0,Type=IsXover,Count=3075,lp-gsD=5']
def key(x:str):
    return int(x.partition("Count=")[2].partition(",")[0])

print(sorted(to_sort, key=key))

【讨论】:

    【解决方案2】:
    lst = ['16:11:40.894 0,Type=IsXover,Count=1,lp-isD=2',
           '16:11:40.894 0,Type=IsXover,Count=54,lp-xsD=1',
           '16:11:40.894 0,Type=IsXover,Count=201,lr-isD=3',
           '16:11:40.894 0,Type=IsXover,Count=3075,lp-gsD=5']
    
    def extract_num(text):
        txt1 = text[text.find("Count=")+6:]
        txt2 = txt1[:txt1.find(",")]
        return int(txt2)
    
    lst.sort(key=extract_num)
    
    print(lst)
    

    【讨论】:

      【解决方案3】:

      如果没有正则表达式,并假设所有字符串的格式相同,您可以这样做:

      mylist.sort(key = lambda x: int(''.join([i for i in x.split(',')[2] if i.isnumeric()])))
      

      列表解析[i for i in x.split(',')[2] if i.isnumeric()] 在逗号处拆分您的字符串,获取索引 2 处的元素(即“Count=___”),并提取所有数字字符串。然后,int(''.join 将它们连接在一起并将其转换为整数。然后,您可以使用它作为您的键来对您的列表进行排序。

      【讨论】:

        【解决方案4】:
        lst = ['16:11:40.894 0,Type=IsXover,Count=1,lp-isD=2',
               '16:11:40.894 0,Type=IsXover,Count=54,lp-xsD=1',
               '16:11:40.894 0,Type=IsXover,Count=201,lr-isD=3',
               '16:11:40.894 0,Type=IsXover,Count=3075,lp-gsD=5']
        
        count_dict = {}
        
        for elem in lst:
            temp_list = elem.split(',')
            count = temp_list[2]
        
            new_count = int(count.split('=')[1])
        
            count_dict[new_count] = elem
        
        new_list = []
        
        for count in sorted(count_dict):
            new_list.append(count_dict[count])
        

        【讨论】:

          【解决方案5】:

          你可以试试Chris_Rands告诉的方法。

          尝试通过拆分整个字符串从字符串中提取Count参数的值。

          sorted(lst, key=lambda x: int(x.split('Count=', 1)[1].split(',', 1)[0]))

          上面的语句首先根据键'Count='分割字符串。因此可以忽略之前的字符串部分,因此我们使用索引 1 获取字符串的第二部分。在这部分中,我们再次将字符串拆分为“,”。然后忽略它之后的部分。所以我们只取','之前的部分,使用索引0。最后,我们把这个值解析为整数类型。

          For ex. take '16:11:40.894 0,Type=IsXover,Count=1,lp-isD=2'
          
          after splitting the string from 'Count=' and taking the value at index 0, we get '1,lp-isD=2'.
          
          now splitting this from ',' and taking the value at index 0, we get '1'.
          
          So after parsing this to function int(), we get the value of Count.
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多