将整数拆分为数字以计算 ISBN 校验和
我正在编写一个程序来计算 ISBN 号码的校验位。 我必须将用户的输入(ISBN 的九位数字)读入整数变量,然后将最后一位数字乘以 2,倒数第二位数字乘以 3,依此类推。 如何将整数“拆分”为其组成数字来做到这一点? 由于这是一项基本的家庭作业练习,我不应该使用列表。
I'm writing a program which calculates the check digit of an ISBN number. I have to read the user's input (nine digits of an ISBN) into an integer variable, and then multiply the last digit by 2, the second last digit by 3 and so on. How can I "split" the integer into its constituent digits to do this? As this is a basic homework exercise I am not supposed to use a list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
只需从中创建一个字符串即可。
这就够了。 现在您可以迭代它:
或者您可以对其进行切片:
或者更好的是,不要将用户的输入转换为整数(用户键入字符串)
有关更多信息,请阅读 教程。
Just create a string out of it.
That's enough. Now you can iterate over it:
Or you can slice it:
Or better, don't convert the user's input to an integer (the user types a string)
For more information read a tutorial.
在循环的每次迭代中,它都会从 number 中删除最后一位数字,并将其分配给
digit
。是相反的,从最后一位数字开始,到第一个数字结束
On each iteration of the loop, it removes the last digit from number, assigning it to
digit
.It's in reverse, starts from the last digit, finishes with the first
会给你一个有序的整数列表。 当然,考虑到鸭子类型,您也可以使用 str(ISBN)。
编辑:正如评论中提到的,这个列表不是按升序或降序排序的,但它确实有一个定义的顺序(Python中的集合、字典等理论上没有,尽管实际上顺序倾向于相当可靠)。 如果你想对其进行排序:
list_of_ints.sort()
是你的朋友。 请注意,sort() 就地排序(实际上更改了现有列表的顺序)并且不返回新列表。
Will give you a ordered list of ints. Of course, given duck typing, you might as well work with str(ISBN).
Edit: As mentioned in the comments, this list isn't sorted in the sense of being ascending or descending, but it does have a defined order (sets, dictionaries, etc in python in theory don't, although in practice the order tends to be fairly reliable). If you want to sort it:
list_of_ints.sort()
is your friend. Note that sort() sorts in place (as in, actually changes the order of the existing list) and doesn't return a new list.
在旧版本的 Python 上...
在新版本 3k 上
On Older versions of Python...
On New Version 3k
您可以在循环中使用它,其中 number 是完整的数字,x 是循环的每次迭代 (0,1,2,3,...,n),n 是停止点。 x = 0 给出个位,x = 1 给出十位,x = 2 给出百位,依此类推。 请记住,这将给出从右到左的数字值,因此这可能不适用于 ISBN,但它仍然会隔离每个数字。
You can use this in a loop, where number is the full number, x is each iteration of the loop (0,1,2,3,...,n) with n being the stop point. x = 0 gives the ones place, x = 1 gives the tens, x = 2 gives the hundreds, and so on. Keep in mind that this will give the value of the digits from right to left, so this might not be the for an ISBN but it will still isolate each digit.
将其转换为字符串并使用 int() 函数对其进行映射。
Convert it to string and map over it with the int() function.
递归版本:
Recursion version:
转换为
str
肯定比除以 10 慢。map
比列表理解稍慢:这些时间是由我的笔记本电脑上的以下代码返回的:
Converting to
str
is definitely slower then dividing by 10.map
is sligthly slower than list comprehension:These times were returned by the following code on my laptop:
经过自己的努力搜索,我发现了几种解决方案,每种方案都有优点和缺点。 使用最适合您的任务的。
所有示例均在 GNU/Linux Debian 8 操作系统上使用 CPython 3.5 进行测试。
使用递归
代码
演示
使用函数
divmod
代码
演示
< strong>使用构造
tuple(map(int, str(abs(number)))
使用函数
re.findall
使用模块
decimal
After own diligent searches I found several solutions, where each has advantages and disadvantages. Use the most suitable for your task.
All examples tested with the CPython 3.5 on the operation system GNU/Linux Debian 8.
Using a recursion
Code
Demo
Using the function
divmod
Code
Demo
Using a construction
tuple(map(int, str(abs(number)))
Using the function
re.findall
Using the module
decimal
使用此循环的主体对数字执行任何您想要的操作
Use the body of this loop to do whatever you want to with the digits
我编写了这个程序,这是在我的程序中实际计算校验位的代码
,它是一长行代码,但它将数字分开,将数字乘以适当的数量,将它们加在一起并除以它们11、一行代码。 .split() 函数只是创建一个列表(按小数点拆分),因此您可以获取列表中的第二项,然后从 11 中获取校验位。 通过更改这两行也可以提高效率:
为此:
希望这有帮助:)
I have made this program and here is the bit of code that actually calculates the check digit in my program
Its a long line of code, but it splits the number up, multiplies the digits by the appropriate amount, adds them together and divides them by 11, in one line of code. The .split() function just creates a list (being split at the decimal) so you can take the 2nd item in the list and take that from 11 to find the check digit. This could also be made even more efficient by changing these two lines:
To this:
Hope this helps :)
类似于 this 答案,但更“Pythonic”的方式迭代数字将是:
Similar to this answer but more a more "pythonic" way to iterate over the digis would be:
一行数字列表怎么样?
How about a one-liner list of digits...
答案: 165
方法:暴力破解! 这里有一小段 Python(2.7 版)代码来统计所有数据。
Answer: 165
Method: brute-force! Here is a tiny bit of Python (version 2.7) code to count'em all.
假设您想从整数 x 中获取第 i 个最低有效数字,您可以尝试:
我希望它有所帮助。
Just assuming you want to get the i-th least significant digit from an integer number x, you can try:
I hope it helps.