Python 类型转换

标签 python types int

在 Python 中将 int、long、double 转换为字符串的最佳方法是什么,反之亦然。

我正在遍历一个列表并将 long 传递给一个应该转换为 unicode 字符串的字典。

我愿意

for n in l:  
    {'my_key':n[0],'my_other_key':n[1]}

为什么一些最明显的事情如此复杂?

最佳答案

将数字类型转换为字符串:

str(100)

将字符串转换为整数:

int("100")

将字符串转换为 float :

float("100")

关于Python 类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2375334/

相关文章:

python - Python 2.7 的 gae-session 等价物

python - 在 scikit-learn 中获取 DecisionTreeRegressor 的叶节点处的值分布

python - 在 SQL Server 存储过程中导入 Python 脚本

Scala Function.tupled 和 Function.untupled 等效于变量 arity,或者,使用元组调用变量 arity 函数

typescript ,将键限制为数组元素

java - 应用程序崩溃 java.lang.NumberFormatException : Invalid int:

python - ABC ----> ABC、CAB、BCA 仅顺序重要但不是真正的 itertools 排列

C# : Type. GetType 在动态加载程序集的类上为 null

c - 尝试在函数中使用 txt 文件时获取 "invalid type argument of ' ->' (have ' int')"

不使用 atoi() 或 stoi() 的 C++ 字符串到 int