Python Type Conversion

前端 未结 3 989
Happy的楠姐
Happy的楠姐 2021-01-11 18:23

Whats the best way to convert int\'s, long\'s, double\'s to strings and vice versa in python.

I am looping through a list and passing longs to a dict that should be

3条回答
  •  感情败类
    2021-01-11 18:42

    You could do it like this in Python 2.x:

    >>> l = ((1,2),(3,4))
    >>> dict(map(lambda n: (n[0], unicode(n[1])), l))
    {1: u'2', 3: u'4'}
    

    or in Python 3.x:

    >>> l = ((1,2),(3,4))
    >>> {n[0] : str(n[1]) for n in l}
    {1: '2', 3: '4'}
    

    Note that strings in Python 3 are the same as unicode strings in Python 2.

提交回复
热议问题