zoukankan      html  css  js  c++  java
  • python字符类型的相互转换

    str---->list

    str1 = "string"
    list1 = list(str1)
    print(list1)
    
    str2 = "i am ybc"
    list2 = str2.split()
    print(list2)
    
    str3 = "www.google.com"
    list3 = str3.split(".")
    print(list3)
    
    #运行结果
    ['s', 't', 'r', 'i', 'n', 'g']
    ['i', 'am', 'ybc']
    ['www', 'google', 'com']
    

    list---->str

    str4 = "".join(list1)
    print(str4)
    str5 = " ".join(list2)
    print(str5)
    str6 = ".".join(list3)
    print(str6)
    
    #运行结果
    string
    i am ybc
    www.google.com
    

    str---->dict

    内置函数eval

    将字符串str当成有效的表达式来求值并返回计算结果
    无法处理多维字典;字符串里面的字符必须是单引号

    user = "{'name' : 'zhangsan', 'sex' : 'male', 'age': 20}"
    dict1 = eval(user)
    print(dict1)
    
    #运行结果
    {'name': 'zhangsan', 'sex': 'male', 'age': 20}
    

    json包

    字符串里面的字符必须是双引号

    import json
    user = '{"name":"zhangsan","sex":"male","age":"20"}'
    dict2 = json.loads(user)
    print(dict2)
    
    #运行结果
    {'name': 'zhangsan', 'sex': 'male', 'age': '20'}
    

    dict---->str

    内置方法str

    dict = {'name': 'zhangsan', 'sex': 'male', 'age': '20'}
    str = str(dict)
    print(type(str), str)
    
    #运行结果
    <class 'str'> {'name': 'zhangsan', 'sex': 'male', 'age': '20'}
    

    json包

    import json
    dict = {'name': 'zhangsan', 'sex': 'male', 'age': '20'}
    str = json.dumps(dict)
    print(type(str), str)
    
    #运行结果
    <class 'str'> {"name": "zhangsan", "sex": "male", "age": "20"}
    

    dict---->list

    list函数取key,vlaue值

    list函数默认是将字典中的key取出来,返回list

    dict = {'name': 'zhangsan', 'sex': 'male', 'age': '20'}
    #字典中的key转换为列表
    list1 = list(dict.keys())
    #字典中的value转换为列表
    list2 = list(dict.values())
    print(list1, list2)
    
    #运行结果
    ['name', 'sex', 'age'] ['zhangsan', 'male', '20']
    

    for循环

    dict = {'name': 'zhangsan', 'sex': 'male', 'age': '20'}
    list_key = []
    list_value = []
    for key, value in dict.items():
        list_key.append(key)
        list_value.append(value)
    print(list_key, list_value)
    
    #运行结果
    ['name', 'sex', 'age'] ['zhangsan', 'male', '20']
    

    list---->dict

    list = ['zhangsan', '18']
    dict = {}
    for i in range(len(list)):
        dict[i] = list[i]
    print(dict)
    
    #运行结果
    {0: 'zhangsan', 1: '18'}
    

    str---->tuple

    python的元组与列表类似,但元组的元素不能修改

    直接转换

    str转化为tuple,直接进行转换

    str = "zhangsan 18"
    tuple = tuple(str)
    print(tuple)
    
    #运行结果
    ('z', 'h', 'a', 'n', 'g', 's', 'a', 'n', ' ', '1', '8')
    

    借助list

    str = "zhangsan 18"
    tuple = tuple(str.split())
    print(tuple)
    
    #运行结果
    ('zhangsan', '18')
    

    tuple---->str

    tuple转换为str需要借助join()函数来实现

    tuple = ('z', 'h', 'a', 'n', 'g', 's', 'a', 'n', ' ', '1', '8')
    str = "".join(tuple)
    print(str)
    
    #运行结果
    zhangsan 18
    

    list---->tuple

    list = ['zhangsan', '18']
    tuple = tuple(list)
    print(tuple)
    
    #运行结果
    ('zhangsan', '18')
    
  • 相关阅读:
    35 点击全图后发现地图“不见了”
    34 文件地理数据库(GDB)变文件夹了怎么办
    33 ArcToolBox学习系列之数据管理工具箱——投影与变换(Projections and Transformations)未完待续……
    32 ArcToolBox学习系列之数据管理工具箱——属性域(Domains)的两种创建及使用方式
    30 ArcGIS 许可管理器常见问题(持续更新中……)
    算法竞赛入门经典 第三章 简要题解
    SCOI2003 字符串折叠 & NEERC2002 Folding 题解
    Vjios 1617 超级教主
    Codeforces Round #652 题解
    Codeforces Round #655 题解
  • 原文地址:https://www.cnblogs.com/zhaoya2019/p/13085202.html
Copyright © 2011-2022 走看看