首页 > 其他分享 >7. str|list|dict|tupe类型转换

7. str|list|dict|tupe类型转换

时间:2022-12-26 18:13:02浏览次数:48  
标签:tupe set tuple -- list dict str print

 类型的转换
# 常用的字符串转换
# int、str、bool、list、tuple、dict、None
# 1.int和str
v1 = int('123')
v2 = str(123)

# 2.list和tuple
v3 = tuple([1, 2, 3, 4])
v4 = list((11, 22, 33))

# 3.其他类型转为bool
# 所有类型转换为布尔值时,只有:0、空字典、空列表、空元组和None为False,其他都为true

v5 = "aaa"
v5.encode("utf-8")
#list --> set
l1 =[1,2,3,4,5]
s1 = set(l1)
print(s1)
#tuple -->set
t2 = (1,2,3,4)
s2 = set(t2)
print(s2)

#set--> list
s3 = {1,2,3}
l3 =  list(s3)
print(l3)
#set-->tuple

s4 = {1,2,3,4}
t4 = tuple(s4)
print(t4)

#list 去重:转换为set去重
l4 = [1,2,3,4,4,5,5,6]
l4 = list(set(l4))
print(l4)

 

标签:tupe,set,tuple,--,list,dict,str,print
From: https://www.cnblogs.com/zmc60/p/17006373.html

相关文章