python五大容器的相互类型转换

在 Python 中,五大容器通常指的是列表(list)、元组(tuple)、字典(dict)、集合(set)和字符串(str)。这些容器之间可以通过内置函数或特定的方法实现相互类型转换。以下是它们之间的转换方式:

1. 列表(list)与其他容器的转换

列表 → 元组

使用 tuple() 函数:

my_list = [1, 2, 3]
my_tuple = tuple(my_list)
print(my_tuple)  # 输出: (1, 2, 3)
列表 → 集合

使用 set() 函数:

my_list = [1, 2, 2, 3]
my_set = set(my_list)
print(my_set)  # 输出: {1, 2, 3}
列表 → 字典

需要手动构造键值对:

my_list = [["a", 1], ["b", 2]]
my_dict = dict(my_list)
print(my_dict)  # 输出: {'a': 1, 'b': 2}
列表 → 字符串

使用 join() 方法:

my_list = ["hello", "world"]
my_str = " ".join(my_list)
print(my_str)  # 输出: "hello world"

2. 元组(tuple)与其他容器的转换

元组 → 列表

使用 list() 函数:

my_tuple = (1, 2, 3)
my_list = list(my_tuple)
print(my_list)  # 输出: [1, 2, 3]
元组 → 集合

使用 set() 函数:

my_tuple = (1, 2, 2, 3)
my_set = set(my_tuple)
print(my_set)  # 输出: {1, 2, 3}
元组 → 字典

需要手动构造键值对:

my_tuple = (("a", 1), ("b", 2))
my_dict = dict(my_tuple)
print(my_dict)  # 输出: {'a': 1, 'b': 2}
元组 → 字符串

使用 join() 方法:

my_tuple = ("hello", "world")
my_str = " ".join(my_tuple)
print(my_str)  # 输出: "hello world"

3. 集合(set)与其他容器的转换

集合 → 列表

使用 list() 函数:

my_set = {1, 2, 3}
my_list = list(my_set)
print(my_list)  # 输出: [1, 2, 3](顺序可能不同)
集合 → 元组

使用 tuple() 函数:

my_set = {1, 2, 3}
my_tuple = tuple(my_set)
print(my_tuple)  # 输出: (1, 2, 3)(顺序可能不同)
集合 → 字典

需要手动构造键值对:

my_set = {"a", "b"}
my_dict = dict(zip(my_set, [1, 2]))
print(my_dict)  # 输出: {'a': 1, 'b': 2}(顺序可能不同)
集合 → 字符串

使用 join() 方法:

my_set = {"hello", "world"}
my_str = " ".join(my_set)
print(my_str)  # 输出: "hello world"(顺序可能不同)

4. 字典(dict)与其他容器的转换

字典 → 列表

可以通过 keys()values()items() 方法转换:

my_dict = {"a": 1, "b": 2}
keys_list = list(my_dict.keys())  # 转换为键的列表
values_list = list(my_dict.values())  # 转换为值的列表
items_list = list(my_dict.items())  # 转换为键值对的列表
print(keys_list)   # 输出: ['a', 'b']
print(values_list) # 输出: [1, 2]
print(items_list)  # 输出: [('a', 1), ('b', 2)]
字典 → 元组

可以通过 keys()values()items() 方法转换:

my_dict = {"a": 1, "b": 2}
keys_tuple = tuple(my_dict.keys())  # 转换为键的元组
values_tuple = tuple(my_dict.values())  # 转换为值的元组
items_tuple = tuple(my_dict.items())  # 转换为键值对的元组
print(keys_tuple)   # 输出: ('a', 'b')
print(values_tuple) # 输出: (1, 2)
print(items_tuple)  # 输出: (('a', 1), ('b', 2))
字典 → 集合

可以通过 keys()values()items() 方法转换:

my_dict = {"a": 1, "b": 2}
keys_set = set(my_dict.keys())  # 转换为键的集合
values_set = set(my_dict.values())  # 转换为值的集合
items_set = set(my_dict.items())  # 转换为键值对的集合
print(keys_set)   # 输出: {'a', 'b'}
print(values_set) # 输出: {1, 2}
print(items_set)  # 输出: {('a', 1), ('b', 2)}
字典 → 字符串

可以通过 str() 函数或自定义格式化:

my_dict = {"a": 1, "b": 2}
my_str = str(my_dict)
print(my_str)  # 输出: "{'a': 1, 'b': 2}"

# 自定义格式化
my_str = " ".join([f"{k}:{v}" for k, v in my_dict.items()])
print(my_str)  # 输出: "a:1 b:2"

5. 字符串(str)与其他容器的转换

字符串 → 列表

使用 list() 函数或 split() 方法:

my_str = "hello"
my_list = list(my_str)  # 转换为字符列表
print(my_list)  # 输出: ['h', 'e', 'l', 'l', 'o']

my_str = "hello world"
my_list = my_str.split()  # 按空格分割成单词列表
print(my_list)  # 输出: ['hello', 'world']
字符串 → 元组

使用 tuple() 函数或 split() 方法:

my_str = "hello"
my_tuple = tuple(my_str)  # 转换为字符元组
print(my_tuple)  # 输出: ('h', 'e', 'l', 'l', 'o')

my_str = "hello world"
my_tuple = tuple(my_str.split())  # 按空格分割成单词元组
print(my_tuple)  # 输出: ('hello', 'world')
字符串 → 集合

使用 set() 函数或 split() 方法:

my_str = "hello"
my_set = set(my_str)  # 转换为字符集合
print(my_set)  # 输出: {'h', 'e', 'l', 'o'}

my_str = "hello world"
my_set = set(my_str.split())  # 按空格分割成单词集合
print(my_set)  # 输出: {'hello', 'world'}
字符串 → 字典

需要手动构造键值对:

my_str = "a:1,b:2"
my_dict = dict(item.split(":") for item in my_str.split(","))
print(my_dict)  # 输出: {'a': '1', 'b': '2'}

通过这些方法,Python 的五大容器可以方便地相互转换,以满足不同的需求。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值