元组 ,列表, 集合间的转换
- 列表元组集合间隔转换的函数
| 原始类型 | 目标函数 | 函数 | 举例 |
|---|---|---|---|
| 列表 | 集合 | set | new_set = set([1, 2, 3, 4, 5]) |
| 列表 | 元组 | tuple | new_tutple = 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}) |
代码
| # coding:utf-8 | |
| a = [1, 2, 3] | |
| b = (1, 2, 3) | |
| c = {1, 2, 3} | |
| print(tuple(a), set(a)) | |
| print(type(tuple(a)), type(set(a))) | |
| print(tuple(a) is b) | |
| print(set(a) == c) | |
| print(list(b), set(b)) | |
| print(list(c), tuple(c)) | |
| print(list(a)) | |
| print(str(a), type(str(a))) # '[1, 2, 3]' | |
| print(str(b), type(str(b))) | |
| print(str(c), type(str(c))) | |
| # 转换不可逆... | |
| print(list(str(a))) | |
| print(tuple(str(b))) | |
| print(set(str(c))) | |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 ZkeqのCoding日志!
评论





