Python Type Conversion

前端 未结 3 976
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:46

    You can do it this way

    for n in l:
        {'my_key':unicode(n[0]),'my_other_key':unicode(n[1])}
    

    Perhaps this is clearer if there are only 2 or 3 keys/values

    for my_value, my_other_value in l:
        {'my_key':unicode(my_value),'my_other_key':unicode(my_other_value)}
    

    I think this would be better if there are more than 3 keys/values

    for n in l:
        dict(zip(('my_key','myother_key'),map(unicode,n)))
    

提交回复
热议问题