【问题标题】:return list with integers and string from list of strings从字符串列表中返回带有整数和字符串的列表
【发布时间】:2019-08-05 05:58:58
【问题描述】:
['10019', 'Airma25KLOS', 'Juridinis', 'LT', '121979631', 'LT219796314', '2410', '25', '26', '3232', '32131']

在这个列表中,每个项目都是一个字符串,我怎样才能从这个列表中创建具有相同顺序的相同列表,而不是现在作为字符串的整数,例如 10019' '121979631' 等等,作为整数返回。

我的目标是让列表看起来像这样

[10019, 'Airma25KLOS', 'Juridinis', 'LT', 121979631, 'LZ219796314', 2410, 25, 26, 3232, 32131]

如果有字母和数字的混合,它应该保持为像 LZ219796314' 这样的字符串

【问题讨论】:

    标签: python string list integer


    【解决方案1】:

    您可以使用正则表达式来检查字符串是否为整数:

    import re
    
    p = re.compile(r'[+-]?\d+')
    
    [int(i) if p.fullmatch(i) else i for i in l]
    

    【讨论】:

      【解决方案2】:

      您可以将以下列表推导与 .isdigit 一起使用,以便仅转换为 int 数字字符串:

      l = ['10019', 'Airma25KLOS', 'Juridinis', 'LT', '121979631', 
           'LT219796314', '2410', '25', '26', '3232', '32131']
      
      [int(i) if i.lstrip('-').isdigit() else i for i in l]
      # [10019, 'Airma25KLOS', 'Juridinis', 'LT', 121979631, 'LT219796314', 
      #  2410, 25, 26, 3232, 32131]
      

      【讨论】:

      • 不太健壮。如果其中一个整数是负数怎么办?
      • @JohnColeman 是对的。在我看来,更严格的方法是定义一个单独的函数来对字符串是否为整数进行更稳健的检查。
      • @JohnColeman 好点。将添加一个考虑负整数的更新
      • @yatu 这取决于预期的应用程序。对于正整数 base-10 文字(OP 似乎已经想到了),这很好。 OP 当然应该知道isdigit(),但也应该知道它的局限性。
      • 这种方法只适用于整数,以及其他建议的解决方案。对于 + 号,您可以简单地通过 - 和 + 离开,i.lstrip('+-')@MykolaZotko
      【解决方案3】:

      你可以试试这个:

      l=['-10019', 'Airma25KLOS', 'Juridinis', 'LT', '121979631', 'LT219796314', '2410', '25', '26', '3232', '32131']
      a=[]
      for item in l:
          try:
              a.append(int(item))
          except:
              a.append(item)
      

      我不确定这是否是最好的方法,但这适用于所有整数(正数和负数)

      【讨论】:

        【解决方案4】:

        使用此代码。它适用于负整数以及浮点等。

        代码:

        def is_number(s):
            try:
                float(s)
                return True
            except ValueError:
                pass
        
            try:
                import unicodedata
                unicodedata.numeric(s)
                return True
            except (TypeError, ValueError):
                pass
        
            return False
        
        
        l = ['10019', 'Airma25KLOS', 'Juridinis', 'LT', '121979631', 
             'LT219796314', '2410', '-25', '26', '3232', '32131']
        
        print([int(i) if is_number(i) else i for i in l])
        

        输出:

        [10019, 'Airma25KLOS', 'Juridinis', 'LT', 121979631, 'LT219796314', 2410, -25, 26, 3232, 32131]
        

        【讨论】:

        • behtreen hogaya! :)
        • shukriya :) @DirtyBit
        【解决方案5】:

        这应该适用于任何有效表示整数的字符串(无论是正数还是负数)。并不是说 if 不适用于浮点数。

        def to_int(s):
            try:
                return int(s)
            except (TypeError, ValueError) as e:
                return s
        
        result = [to_int(s) for s in your_list]
        

        【讨论】:

        • 是的,这就是我的想法。 +1
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-26
        • 2015-10-03
        • 2011-07-20
        • 2022-01-25
        • 2019-12-17
        相关资源
        最近更新 更多