python for 数据类型转换
Posted q240756200
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python for 数据类型转换相关的知识,希望对你有一定的参考价值。
1.set() --- list()
s = {12,4,3,4}
print(list(s)) # [3, 12, 4]
lst = [3, 12, 4]
print(set(lst)) # {3, 12, 4}
tu = (1,2,3,4)
print(list(tu)) #[1, 2, 3, 4]
print(set(tu)) #{1, 2, 3, 4}
s = {1,2,3}
print(tuple(s)) #(1, 2, 3)
tu = (1,2,3,4)
print(str(tu))
lst = [1,2,3,4,[5,6,7,[8,9,10]]]
print(str(lst))
print #面向用户的
s = str(lst)
print(repr(s))
na = ‘alex‘
lst = list(na)
print(lst)
dic = {‘name‘:‘黑哥‘,‘age‘:18}
lst = list(dic) # 字典的键
print(lst)
s = set(dic)
print(s)
str
int
bool
list
tuple
dict
set
print(bool(‘ ‘)) True
print(bool(‘‘)) False
print(bool(0)) False
print(bool(56)) True
print(bool(True)) True
print(bool(False)) False
print(bool([1,2,3])) True
print(bool([])) # False
print(bool((1,2,3))) #True
print(bool(())) #False
print(bool())
0 ‘‘ None [ ] ( ) { } { } False
print(bool(None))
for i in ‘alex‘:
if i:
print(44)
以上是关于python for 数据类型转换的主要内容,如果未能解决你的问题,请参考以下文章
从浮点字符串到 Int 的 Python 类型转换在创建列表时无法在 for 循环中工作