7、Python数据类型转换

  1. Python数据类型的转换
    1. 1、字符串与数字间的转换
    2. 2、字符串与列表间的转换
    3. 3、字符串与bytes的转换
    4. 4、元组,列表,集合间的转换
    5. 5、练习

Python数据类型的转换

  1. 什么是类型转换,为何做类型转换
    • 将自身数据类型变成新的数据类型,并拥有新的数据类型的所有功能的过程即为类型转换
    • 为方便更好的帮助处理业务,将类型变更为更适合业务场景的类型

1、字符串与数字间的转换

  • 字符串与数字之间转换的要求

    str –> number:数字组成的字符串

    int_str = '123456'
    float_str = '3.14'
    

    number –> str:无要求

  • 字符串与数字之间转换的函数

    原始类型 目标类型 函数 距离
    整型 字符串 str new_str = str(123456)
    浮点型 字符串 str new_str = str(3.14)
    字符串 整型 int new_int = int(‘12’)
    字符串 浮点型 float new_float = float(‘1.2’)

2、字符串与列表间的转换

  • 字符串转列表的函数–split

    • 功能:将字符串以一定规则切割转成列表

    • 用法:string.split(sep=None, maxsplit=-1)

      参数:sep: 切割的规则符号,不填写,默认空格, 如字符串无空格则不分割生成列表

      ​ maxsplit: 根据切割符号切割的次数, 默认-1无限制

      返回值:返回一个列表

    In [1]: info = 'my name is david'
    In [2]: info_list = info.split()
    In [3]: print(info_list)
    Out [1]: ['my', 'name', 'is', 'david']
    
  • 列表转字符串的函数–join

    • 功能:将列表以一定规则转成字符串

    • 用法:’sep’.join(iterable)

      参数:sep: 生成字符串用来分割列表每个元素的符号

      ​ iterable: 非数字类型的列表或元组或集合

    In [1]: test = ['a', 'b', 'c']
    In [2]: new_str = '.'.join(test)
    In [3]: print(new_str)
    Out [1]: 'a.b.c'
    

3、字符串与bytes的转换

  • 什么是比特类型

    • 二进制的数据流—byte

    • 一种特殊的字符串

    • 字符串前 + b 标记

  • 字符串转bytes的函数—encode

    • 功能:u将字符串转成比特(bytes)类型

    • 用法:string. encode(encoding=’utf-8’, errors=’strict’)

    • 参数:encoding: 转换成的编码格式,如ascii,gbk,默认 utf-8

      ​ errors: 出错时的处理方法,默认strict,直接抛错误,也可以选择ignore忽略错误

    • 返回值:返回一个比特(bytes)类型

    In [1]: str_data = 'my name is david'
    In [2]: byte_data = str_data.encode('utf-8')
    In [3]: print(byte_data)
    Out [1]: b'my name is david'
    
  • bytes转字符串的函数–decode

    • 功能:将比特(bytes)类型转成字符串

    • 用法:bytes. decode(encoding=’utf-8’, errors=’strict’)

    • 参数:encoding: 转换成的编码格式,如ascii,gbk,默认 utf-8

      ​ errors: 出错时的处理方法,默认strict,直接抛错误,也可以选择ignore忽略错误

    • 返回值:返回一个字符串类型

    In [1]: byte_data = b'my name is david'
    In [2]: str_data = byte_data.decode('utf-8')
    In [3]: print(str_data)
    Out [1]: 'my name is david'
    

4、元组,列表,集合间的转换

  • 列表元组集合之间的转换函数

    原始类型 目标类型 函数 举例
    列表 集合 set new_set = set([1, 2, 3, 4, 5])
    列表 元组 tuple new_tuple = tuple([1, 2, 3, 4, 5])
    元组 集合 set new_set = set((1, 2, 3, 4, 5))
    元组 列表 list new_list = list((1, 2, 3, 4, 5))
    集合 列表 list new_list = list({1, 2, 3, 4, 5})
    集合 元组 tuple new_tuple = tuple({1, 2, 3, 4, 5})

5、练习

  • 综合之前的学习内容,打印一个九九乘法表(之后学习循环之后会少很多代码)

    # coding:utf-8
    
    '''
    1 * 1 = 1
    2 * 1 = 2 2 * 2 = 4
    3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
    4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
    5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
    6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
    7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
    8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
    9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
    
    '''
    
    work = {}
    # 1
    one_value = (1,)
    one_key = 1
    work[str(one_key)] = one_value
    print(work)
    
    # 2
    two_key = 2
    two_value = []
    two_value.append(1)
    two_value.append(2)
    work[str(two_key)] = two_value
    
    # 3
    three_key = 3
    three_value = []
    three_value.append(1)
    three_value.append(2)
    three_value.append(3)
    work[str(three_key)] = three_value
    
    # 4
    four_key = 4
    four_value = []
    four_value.append(1)
    four_value.append(2)
    four_value.append(3)
    four_value.append(4)
    work[str(four_key)] = four_value
    
    temp_five = (1, 2, 3, 4, 5)
    five_key = 5
    five_value = []
    five_value.extend(temp_five)
    work[str(five_key)] = five_value
    print(work)
    
    # 6
    temp_six = [1, 2, 3, 4, 5, 6]
    six_key = 6
    six_value = []
    six_value.extend(temp_six)
    work[str(six_key)] = six_value
    
    # 7
    temp_seven = {1, 2, 3, 4, 5, 6, 7}
    seven_key = 7
    seven_value = []
    seven_value.extend(temp_seven)
    work[str(seven_key)] = seven_value
    
    # 8
    temp_eight = [1, 2, 3, 4, 5, 6, 7, 8]
    eight_key = 8
    eight_value = []
    eight_value.extend(temp_eight)
    eight_key = str(eight_key)
    work[eight_key] = eight_value
    
    # 9
    temp_nine = (1, 2, 3, 4, 5, 6, 7, 8, 9)
    nine_key = 9
    nine_value = list(temp_nine)
    nine_key = str(nine_key)
    work.update({nine_key: nine_value})
    print(work)
    
    _keys = work.keys()
    keys = list(_keys)
    print(keys)
    
    one = keys[0]
    one_value = work.pop(one)
    print(one, one_value)
    one_key = int(one)
    print('{} * {} = {}'.format(one_key, one_value[0], one_key * one_value[0]))
    
    two = keys[1]
    two_value = work.pop(two)
    print('{} * {} = {}'.format(two, two_value[0], int(two) * two_value[0]), end=' ')
    print('{} * {} = {}'.format(two, two_value[1], int(two) * two_value[1]))
    
    three = keys[2]
    three_value = work.pop(three)
    print('{} * {} = {}'.format(three, three_value[0], int(three) * three_value[0]), end=' ')
    print('{} * {} = {}'.format(three, three_value[1], int(three) * three_value[1]), end=' ')
    print('{} * {} = {}'.format(three, three_value[2], int(three) * three_value[2]))
    
    four = keys[3]
    four_value = work.pop(four)
    print('{} * {} = {}'.format(four, four_value[0], int(four) * four_value[0]), end=' ')
    print('{} * {} = {}'.format(four, four_value[1], int(four) * four_value[1]), end=' ')
    print('{} * {} = {}'.format(four, four_value[2], int(four) * four_value[2]), end=' ')
    print('{} * {} = {}'.format(four, four_value[-1], int(four) * four_value[-1]))
    
    five = keys[4]
    five_value = work.pop(five)
    print('%s * %s = %s' % (five, five_value[0], int(five) * five_value[0]), end=' ')
    print('%s * %s = %s' % (five, five_value[1], int(five) * five_value[1]), end=' ')
    print('%s * %s = %d' % (five, five_value[2], int(five) * five_value[2]), end=' ')
    print('%s * %s = %d' % (five, five_value[3], int(five) * five_value[3]), end=' ')
    print('%s * %s = %d' % (five, five_value[-1], int(five) * five_value[-1]))
    
    six = keys[5]
    six_value = work.pop(six)
    print('%s * %s = %s' % (six, six_value[0], int(six) * six_value[0]), end=' ')
    print('%s * %s = %s' % (six, six_value[1], int(six) * six_value[1]), end=' ')
    print('%s * %s = %s' % (six, six_value[2], int(six) * six_value[2]), end=' ')
    print('{} * {} = {}'.format(six, six_value[3], int(six) * six_value[3]), end=' ')
    print('{} * {} = {}'.format(six, six_value[4], int(six) * six_value[4]), end=' ')
    print('{} * {} = {}'.format(six, six_value[5], int(six) * six_value[5]))
    
    seven = keys[6]
    seven_value = work.pop(seven)
    print('{} * {} = {}'.format(seven, seven_value[0], int(seven) * seven_value[0]), end=' ')
    print('{} * {} = {}'.format(seven, seven_value[1], int(seven) * seven_value[1]), end=' ')
    print('{} * {} = {}'.format(seven, seven_value[2], int(seven) * seven_value[2]), end=' ')
    print('{} * {} = {}'.format(seven, seven_value[3], int(seven) * seven_value[3]), end=' ')
    print('{} * {} = {}'.format(seven, seven_value[4], int(seven) * seven_value[4]), end=' ')
    print('{} * {} = {}'.format(seven, seven_value[5], int(seven) * seven_value[5]), end=' ')
    print('{} * {} = {}'.format(seven, seven_value[6], int(seven) * seven_value[6]))
    
    eight = keys[7]
    eight_value = work.pop(eight)
    print('{} * {} = {}'.format(eight, eight_value[0], int(eight) * eight_value[0]), end=' ')
    print('{} * {} = {}'.format(eight, eight_value[1], int(eight) * eight_value[1]), end=' ')
    print('{} * {} = {}'.format(eight, eight_value[2], int(eight) * eight_value[2]), end=' ')
    print('{} * {} = {}'.format(eight, eight_value[3], int(eight) * eight_value[3]), end=' ')
    print('{} * {} = {}'.format(eight, eight_value[4], int(eight) * eight_value[4]), end=' ')
    print('{} * {} = {}'.format(eight, eight_value[5], int(eight) * eight_value[5]), end=' ')
    print('{} * {} = {}'.format(eight, eight_value[6], int(eight) * eight_value[6]), end=' ')
    print('{} * {} = {}'.format(eight, eight_value[7], int(eight) * eight_value[7]))
    
    nine = keys[-1]
    nine_value = work.get(nine)
    format_str = '{} * {} = {}'
    print(format_str.format(nine, nine_value[0], int(nine) * nine_value[0]), end=' ')
    print(format_str.format(nine, nine_value[1], int(nine) * nine_value[1]), end=' ')
    print(format_str.format(nine, nine_value[2], int(nine) * nine_value[2]), end=' ')
    print(format_str.format(nine, nine_value[3], int(nine) * nine_value[3]), end=' ')
    print(format_str.format(nine, nine_value[4], int(nine) * nine_value[4]), end=' ')
    print(format_str.format(nine, nine_value[5], int(nine) * nine_value[5]), end=' ')
    print(format_str.format(nine, nine_value[6], int(nine) * nine_value[6]), end=' ')
    print(format_str.format(nine, nine_value[7], int(nine) * nine_value[7]), end=' ')
    print(format_str.format(nine, nine_value[-1], int(nine) * nine_value[-1]))
    

转载请注明来源