python整数转换字符串_Python | 将字符串转换为整数列表

python整数转换字符串

Given a string with digits and we have to convert the string to its equivalent list of the integers in Python.

给定一个带有数字的字符串,我们必须将该字符串转换为Python中与之等效的整数列表。

Example:

例:

    Input:
str1 = "12345"
Output:
int_list = [1, 2, 3, 4, 5]
Input:
str1 = "12345ABCD"
Output:
ValueError

Note: The string must contain only digits between 0 to 9, if there is any character except digits, the program will through a ValueError.

注意:该字符串只能包含0到9之间的数字,如果除数字之外还有任何其他字符,则程序将通过ValueError进行操作

如何将字符(仅数字)转换为整数? (How to convert character (only digit) to integer?)

To convert a character (that is digit only like: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9') to integer, we use int() function - which is a library function in Python. int() returns integer value equivalent to given character i.e. digit in character form.

转换字符(仅是数字,例如: “ 0”,“ 1”,“ 2”,“ 3”,“ 4”,“ 5”,“ 6”,“ 7”,“ 8”,“ 9” )转换为整数,我们使用int()函数-这是Python中的库函数。 int()返回等于给定字符的整数值,即字符形式的数字。

print (int('0'))
print (int('1'))
print (int('2'))
print (int('3'))
print (int('4'))
print (int('5'))
print (int('6'))
print (int('7'))
print (int('8'))
print (int('9'))

Output

输出量

0
1
2
3
4
5
6
7
8
9

Python程序将字符串转换为整数列表 (Python program to convert a string to integers list)

Here, we have a string "12345" and we are converting it to integers list [1, 2, 3, 4, 5].

在这里,我们有一个字符串“ 12345” ,并将其转换为整数列表[1、2、3、4、5]

# program to convert string to integer list
# language: python3
# declare a list
str1 = "12345"
# list variable to integeres
int_list =[]
# converting characters to integers
for ch in str1:
int_list.append(int(ch))
# printing the str_list and int_list    
print ("str1: ", str1)
print ("int_list: ", int_list)

Output

输出量

str1:  12345
int_list:  [1, 2, 3, 4, 5]

ValueError (ValueError)

If there is any character except the digits, there will be an error "ValueError: invalid literal for int() with base 10".

如果除数字外没有其他字符,将出现错误“ ValueError:int()的基数为10的无效文字”

Program with Error:

出现错误的程序:

# program to convert string to integer list
# language: python3
# declare a list
str1 = "12345ABCD"
# list variable to integeres
int_list =[]
# converting characters to integers
for ch in str1:
int_list.append(int(ch))
# printing the str_list and int_list    
print ("str1: ", str1)
print ("int_list: ", int_list)

Output

输出量

Traceback (most recent call last):
File "/home/main.py", line 12, in int_list.append(int(ch))
ValueError: invalid literal for int() with base 10: 'A'

翻译自: https://www.includehelp.com/python/convert-a-string-to-integers-list.aspx

python整数转换字符串

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544579.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

什么是可中断锁?有什么用?怎么实现?

作者 | 王磊来源 | Java中文社群(ID:javacn666)转载请联系授权(微信ID:GG_Stone在 Java 中有两种锁,一种是内置锁 synchronized,一种是显示锁 Lock,其中 Lock 锁是可中断锁&#xff…

android开发,设置listview的高度无效

一般是在item的layout中设置高度 android:layout_height"100dp" 但是发现这样后无效,因此找到解决办法,如下: android:minHeight"100dp"

10个经典又容易被人疏忽的JVM面试题

前言整理了10个经典又容易被疏忽的JVM面试题,谢谢阅读,大家加油哈.1. 对象一定分配在堆中吗?有没有了解逃逸分析技术?「对象一定分配在堆中吗?」 不一定的,JVM通过「逃逸分析」,那些逃不出方法的…

duration java_Java Duration类| ofDays()方法与示例

duration java持续时间Class ofDays()方法 (Duration Class ofDays() method) ofDays() method is available in java.time package. ofDays()方法在java.time包中可用。 ofDays() method is used to represent the given number of days in this Duration. ofDays()方法用于表…

面试官:怎么解决MySQL中的死锁问题?

咱们使用 MySQL 大概率上都会遇到死锁问题,这实在是个令人非常头痛的问题。本文将会对死锁进行相应介绍,对常见的死锁案例进行相关分析与探讨,以及如何去尽可能避免死锁给出一些建议。话不多说,开整!什么是死锁死锁是并…

java的equals方法_Java Duration类| 带示例的equals()方法

java的equals方法持续时间类equals()方法 (Duration Class equals() method) equals() method is available in java.time package. equals()方法在java.time包中可用。 equals() method is used to identifies whether this Duration and the given object are equal or not. …

ubuntu双系统导致进windows花屏

2019独角兽企业重金招聘Python工程师标准>>> 5600U的集成显卡,装了ubuntu的双系统,居然导致进win7的时候花屏,度娘狗哥都不得求解 网上很多解决方法都说在启动时加上nomodeset,发现对ubuntu15没用,且失去了…

stl resize函数_vector :: resize()函数以及C ++ STL中的示例

stl resize函数C vector :: resize()函数 (C vector::resize() function) vector::resize() is a library function of "vector" header, it is used to resize the vector, it accepts the updated number of elements and a default value (optional) and resizes…

工作总结:日志打印的15个建议

前言 日志是快速定位问题的好帮手,是撕逼和甩锅的利器!打印好日志非常重要。今天我们来聊聊日志打印的15个好建议~1. 选择恰当的日志级别 常见的日志级别有5种,分别是error、warn、info、debug、trace。日常开发中,我们需要选择恰…

MFC属性页对话框

属性页对话框 分类 分页和引导 类 CPropertyPage-父亲CDialog类别,所谓的属性页或网页对话框。 CPropertySheet-父类是CWnd,称为属性表单。 一个完整的属性页对话框由一个属性表单多个属性页组成。属性页嵌套在属性表单内。 标签式属性页的创建步骤&…

vector cbegin_vector :: cbegin()函数以及C ++ STL中的示例

vector cbeginC vector :: cbegin()函数 (C vector::cbegin() function) vector::cbegin() is a library function of "vector" header, it is used to get the const iterator pointing to the first element of the vector. vector :: cbegin()是“ vector”头文件…

面试官:ConcurrentHashMap为什么放弃了分段锁?

今天我们来讨论一下一个比较经典的面试题就是 ConcurrentHashMap 为什么放弃使用了分段锁,这个面试题阿粉相信很多人肯定觉得有点头疼,因为很少有人在开发中去研究这块的内容,今天阿粉就来给大家讲一下这个 ConcurrentHashMap 为什么在 JDK8 …

C语言函数指针的应用——自制谐波分析软件

文章目录函数指针简介格式介绍颜色头文件计算机仿真使用说明完整代码部分效果图函数指针简介 如果在一个大型C语言程序中要反复调用函数,而调用的函数又不明确时,函数指针就是一个非常有用的东西。如果你的函数体内可以传递不同的函数,那就非…

PHP5.5四种序列化性能对比

2019独角兽企业重金招聘Python工程师标准>>> 结论: 1、小数组用msgpack,无论空间和性能都最好 2、大数组,考虑空间用igbinary,考虑性能用msgpack json_encode,serialize,igbinary,msgpack四种序列化方式&am…

多线程循环输出abcc++_C ++循环| 查找输出程序| 套装2

多线程循环输出abccProgram 1: 程序1&#xff1a; #include<iostream>using namespace std;int main(){ for(;;){cout<<"Hello ";}return 0;}Output: 输出&#xff1a; Hello Hello .... Infinite loopExplanation: 说明&#xff1a; In the above co…

MyBatis Plus 批量数据插入功能,yyds!

作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone最近 Review 小伙伴代码的时候&#xff0c;发现了一个小小的问题&#xff0c;小伙伴竟然在 for 循环中进行了 insert &#xff08;插入&a…

C语言打印彩色字符——以(枚举法+字符串查找)为例展示

文章目录C语言颜色头文件——自制非常简单的调用函数实战演练——一个基础的枚举变量小程序牛刀小试——查找字符小程序C语言颜色头文件——自制非常简单的调用函数 显然&#xff0c;C语言是不会提供打印彩色字符的标准函数&#xff0c;而我们有时候为了强调C语言打印的部分字…

人工智能ai 学习_学习代理| 人工智能

人工智能ai 学习Learning is an important part of human behavior. It is the first step in the development phase of any human. When the concept of Artificial Intelligence was proposed, the main approach of the developers was to build a system which could reac…

sql server中同时执行select和update语句死锁问题

原始出处 http://oecpby.blog.51cto.com/2203338/457054 最近在项目中使用SqlServer的时候发现在高并发情况下&#xff0c;频繁更新和频繁查询引发死锁。通常我们知道如果两个事务同时对一个表进行插入或修改数据&#xff0c;会发生在请求对表的X锁时&#xff0c;已经被对方持有…

再见 Spring Task,这个定时任务框架真香!

最近有朋友问到定时任务相关的问题。于是&#xff0c;我简单写了一篇文章总结一下定时任务的一些概念以及一些常见的定时任务技术选型。希望能对小伙伴们有帮助&#xff01;个人能力有限。如果文章有任何需要补充/完善/修改的地方&#xff0c;欢迎在评论区指出&#xff0c;共同…