美文网首页测试开发
python_dict/tuple/list/string 互相

python_dict/tuple/list/string 互相

作者: 古佛青灯度流年 | 来源:发表于2016-10-28 11:22 被阅读20次
    • 字典(dict)

    dict = {‘name’: ‘Zara’, ‘age’: 7, ‘class’: ‘First’}
    1.1 字典——字符串
    返回:
    print type(str(dict)), str(dict)
    1.2 字典——元组
    返回:(‘age’, ‘name’, ‘class’)
    print tuple(dict)
    1.3 字典——元组
    返回:(7, ‘Zara’, ‘First’)
    print tuple(dict.values())
    1.4 字典——列表
    返回:[‘age’, ‘name’, ‘class’]
    print list(dict)
    1.5 字典——列表
    print dict.values

    • 元组

    tup=(1, 2, 3, 4, 5)
    2.1 元组——字符串
    返回:(1, 2, 3, 4, 5)
    print tup.__str__()
    2.2 元组——列表
    返回:[1, 2, 3, 4, 5]
    print list(tup)
    2.3 元组不可以转为字典

    • 列表

    nums=[1, 3, 5, 7, 8, 13, 20];
    3.1 列表——字符串
    返回:[1, 3, 5, 7, 8, 13, 20]
    print str(nums)
    3.2 列表——元组
    返回:(1, 3, 5, 7, 8, 13, 20)
    print tuple(nums)
    3.3 列表不可以转为字典

    • 字符串

    4.1 字符串——元组
    返回:(1, 2, 3)
    print tuple(eval("(1,2,3)"))
    4.2 字符串——列表
    返回:[1, 2, 3]
    print list(eval("(1,2,3)"))
    4.3 字符串——字典
    返回:
    print type(eval("{'name':'ljq', 'age':24}"))

    @ 晴-2016年10月28日11:22:19

    相关文章

      网友评论

        本文标题:python_dict/tuple/list/string 互相

        本文链接:https://www.haomeiwen.com/subject/zkxeuttx.html