Skip to content

数据类型转换

1 数据类型转换概述(掌握)

数据类型转换指的是将一个数据类型转换为另一个数据类型。例如,将整数转换为浮点数,将字符串转换为列表等。Python 提供了多种内置函数来实现不同的数据类型转换。


2 常见的数据类型转换方法(掌握)

Python 提供的基本数据类型转换函数如下:

函数描述
int(x)x 转换为整数
float(x)x 转换为浮点数
str(x)x 转换为字符串
list(x)x 转换为列表
tuple(x)x 转换为元组
set(x)x 转换为集合
dict(x)x 转换为字典(需满足特定结构)

3 数字类型转换(掌握)

Python 中支持整数和浮点数的转换,可以通过 int()float() 函数实现。

3.1 整数和浮点数转换

  • 示例

    python
    # 将浮点数转换为整数
    num1 = 3.14
    int_num1 = int(num1)  # 输出: 3
    
    # 将整数转换为浮点数
    num2 = 5
    float_num2 = float(num2)  # 输出: 5.0

3.2 字符串和数字的转换

  • 字符串转整数

    python
    str_num = "123"
    int_num = int(str_num)  # 输出: 123
  • 字符串转浮点数

    python
    str_float = "3.14"
    float_num = float(str_float)  # 输出: 3.14
  • 数字转字符串

    python
    num = 456
    str_num = str(num)  # 输出: "456"

4 字符串与列表/元组的转换(掌握)

字符串、列表和元组之间的转换方法主要依靠 list()tuple()join() 函数。

4.1 字符串转列表和元组

  • 字符串转列表:每个字符将成为列表的一个元素。

    python
    text = "hello"
    text_list = list(text)  # 输出: ['h', 'e', 'l', 'l', 'o']
  • 字符串转元组:使用 tuple() 将字符串转换为元组。

    python
    text_tuple = tuple(text)  # 输出: ('h', 'e', 'l', 'l', 'o')

4.2 列表/元组转字符串

  • 列表转字符串:使用 join() 方法将列表中的元素合并为字符串。

    python
    words = ['Python', 'is', 'fun']
    sentence = ' '.join(words)  # 输出: "Python is fun"
  • 元组转字符串:同样使用 join() 方法。

    python
    chars = ('P', 'y', 't', 'h', 'o', 'n')
    word = ''.join(chars)  # 输出: "Python"

5 列表、元组、集合的互相转换(掌握)

可以使用 list()tuple()set() 函数在列表、元组和集合之间进行转换。

5.1 列表、元组、集合的转换

  • 列表转元组

    python
    fruits = ['apple', 'banana', 'cherry']
    fruits_tuple = tuple(fruits)  # 输出: ('apple', 'banana', 'cherry')
  • 列表转集合

    python
    fruits_set = set(fruits)  # 输出: {'apple', 'banana', 'cherry'}
  • 元组转列表

    python
    fruits_list = list(fruits_tuple)  # 输出: ['apple', 'banana', 'cherry']
  • 集合转列表

    python
    set_to_list = list(fruits_set)  # 输出: ['apple', 'banana', 'cherry']

6 字典的转换(掌握)

字典的键和值可以分别转换为列表、元组或集合,字典本身可以通过满足一定结构的列表或元组进行转换。

6.1 字典转列表/元组/集合

  • 字典键转换为列表

    python
    person = {'name': 'Alice', 'age': 25}
    keys_list = list(person.keys())  # 输出: ['name', 'age']
  • 字典值转换为列表

    python
    values_list = list(person.values())  # 输出: ['Alice', 25]
  • 字典键值对转换为元组列表

    python
    items_list = list(person.items())  # 输出: [('name', 'Alice'), ('age', 25)]

6.2 列表/元组转字典

  • 列表转字典:列表中的元素需为二元组。

    python
    items = [('name', 'Bob'), ('age', 30)]
    person_dict = dict(items)  # 输出: {'name': 'Bob', 'age': 30}
  • 元组转字典:与列表转字典相同,要求元组内元素为二元组。

    python
    items_tuple = (('name', 'Charlie'), ('age', 35))
    person_dict = dict(items_tuple)  # 输出: {'name': 'Charlie', 'age': 35}

7 常见数据类型转换示例(掌握)

示例 1:混合数据类型列表转字符串

将包含多个不同数据类型的列表元素合并成字符串。

python
data = [1, 'apple', 3.14]
string_data = ' | '.join(map(str, data))
print(string_data)  # 输出: "1 | apple | 3.14"

示例 2:字符串列表转整数列表

将一个包含数字字符串的列表转换为整数列表。

python
str_nums = ["10", "20", "30"]
int_nums = list(map(int, str_nums))
print(int_nums)  # 输出: [10, 20, 30]

示例 3:将嵌套列表转换为字典

将嵌套列表(每个子列表包含键值对)转换为字典。

python
data = [["name", "Alice"], ["age", 28], ["city", "New York"]]
data_dict = dict(data)
print(data_dict)  # 输出: {'name': 'Alice', 'age': 28, 'city': 'New York'}

8 数据类型转换的注意事项(掌握)

  1. 转换条件:并非所有数据类型都可以互相转换,如将非数值字符串转换为数字会报错。

    python
    int("hello")  # ValueError: invalid literal for int()
  2. 精度损失:浮点数转换为整数时会丢失小数部分。

    python
    int(3.9)  # 输出: 3
  3. 可变与不可变:列表、字典和集合是可变类型,元组和字符串是不可变类型。数据转换时要注意特性变化。

  4. 数据丢失:集合的元素是唯一的,将包含重复元素的列表转换为集合会自动去重。

    python
    list_to_set = set([1, 2, 2, 3])  # 输出: {1, 2, 3}