converting python list of strings to their type

后端 未结 7 631
旧时难觅i
旧时难觅i 2020-12-05 19:57

given a list of python strings, how can I automatically convert them to their correct type? Meaning, if I have:

[\"hello\", \"3\", \"3.64\", \"-1\"]
<         


        
相关标签:
7条回答
  • 2020-12-05 20:21

    I accomplished the same using json.loads method

    def f(l):
        for i in l:
            try:
                yield json.loads(i)
            except:
                yield i
    

    Test:

    In [40]: l
    Out[40]: ['hello', '3', '3.64', '-1']
    
    In [41]: list(f(l))
    Out[41]: ['hello', 3, 3.64, -1]
    
    0 讨论(0)
  • 2020-12-05 20:24

    If the you are truly interested in only strings, floats, and ints, I prefer the more verbose, less-evalful

    def interpret_constant(c):
        try:
            if str(int(c)) == c: return int(c)
        except ValueError:
            pass
        try:
            if str(float(c)) == c: return float(c)
        except ValueError:
            return c
    
    test_list = ["hello", "3", "3.64", "-1"]
    
    typed_list = [interpret_constant(x) for x in test_list]
    print typed_list
    print [type(x) for x in typed_list]
    
    0 讨论(0)
  • 2020-12-05 20:25
    import ast
    
    L = ["hello", "3", "3.64", "-1"]
    
    def tryeval(val):
      try:
        val = ast.literal_eval(val)
      except ValueError:
        pass
      return val
    
    print [tryeval(x) for x in L]
    
    0 讨论(0)
  • 2020-12-05 20:28

    A variant of ryans's nice solution, for numpy users:

    def tonum( x ):
        """ -> int(x) / float(x) / None / x as is """
        if np.isscalar(x):  # np.int8 np.float32 ...
        # if isinstance( x, (int, long, float) ):
            return x
        try:
            return int( x, 0 )  # 0: "0xhex" too
        except ValueError:
            try:
                return float( x )  # strings nan, inf and -inf too
            except ValueError:
                if x == "None":
                    return None
                return x
    
    def numsplit( line, sep=None ):
        """ line -> [nums or strings ...] """
        return map( tonum, line.split( sep ))  # sep None: whitespace
    
    0 讨论(0)
  • 2020-12-05 20:31

    Without using evaluation:

    def convert(val):
        constructors = [int, float, str]
        for c in constructors:
            try:
                return c(val)
            except ValueError:
                pass
    
    0 讨论(0)
  • 2020-12-05 20:40
    def tryEval(s):
      try:
        return eval(s, {}, {})
      except:
        return s
    
    map(tryEval, ["hello", "3", "3.64", "-1"])
    

    Only do this if you trust the input. Also, be aware that it supports more than just literals; arithmetic expressions will be evaluated as well.

    0 讨论(0)
提交回复
热议问题