Skip to main content

Python 元组 ,列表, 集合间的转换

Published 1 min readMarkdown
Python 元组 ,列表, 集合间的转换

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

  • 列表元组集合间隔转换的函数
原始类型目标函数函数举例
列表集合setnew_set = set([1, 2, 3, 4, 5])
列表元组tuplenew_tutple = tuple([1, 2, 3, 4, 5])
元组集合setnew_set = set((1, 2, 3, 4, 5))
元组列表listnew_list = list(1, 2, 3, 4, 5)
集合列表listnew_list = list({1, 2, 3, 4, 5})
集合元组tuplenew_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)))

1 view

No comments yet. Be the first to comment.

More from this blog