如何创建一个长正整数

发布于 2025-01-11 09:24:53 字数 431 浏览 3 评论 0原文

如果这个问题对于那些比我更专业的人来说可能听起来有点微不足道,我很抱歉。如果我必须创建一个长正整数并找到零的数量(使用以下指令作为提示:将数字更改为字符串),你知道我应该做什么吗?实际上,这些是我为了解决这个问题而关注的一些概念和例子。

输入图片这里的描述

我不知道蓝色突出显示的线是否适合该作业。没有指定的其他信息(如果您想知道,我已经下载了 python 3.10,虽然在命令提示符面板上输入命令 python,但弹出正在使用的 python 版本是 3.6.6)。

我对这种问题感到非常抱歉,但我刚刚开始向 Python 迈出第一步。

谢谢

I am sorry if this question may sound a bit trivial for those who are more expert than me. If I have to create a long positive integer as well as find the number of zeros (with the following instruction as a hint: change the number to a string), do you know what I am supposed to do? Actually, these are some of the notions and examples that I'm focusing on to solve this problem.

enter image description here

I do not know whether the blue highlighted lines may be suitable for the assignment. There is no specified other info (if you are wondering, I have downloaded python 3.10, although typing the command python onto the panel of commands prompt, it pops up that the python's version in use is 3.6.6).

I am very sorry for this kind of question but I have just started moving my first step towards Python.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ花落谁相伴 2025-01-18 09:24:53

无需借助其他模块即可查找整数中零的数量:

n = 1001001 # for example

print(str(n).count('0'))

输出:

4

To find the number of zeroes in an integer without the aid of additional modules:

n = 1001001 # for example

print(str(n).count('0'))

Output:

4
纵性 2025-01-18 09:24:53

您可以使用 str()int 转换为 str,然后计算零的数量,
这可以使用 re 中的 findall() 函数来完成

import re 
len(re.findall('0', str(number)))

findall() 返回与第一个中的正则表达式匹配的所有出现的列表参数,这里 '0' 只会匹配零,第二个参数是要检查的字符串,这里我们使用 str() 将数字转换为字符串。由于 findall() 返回出现的列表,我们可以将该函数作为参数传递给 len() 来获取出现的次数,

您可以阅读更多此处

you can convert an int to a str using str() and then count the number of zeros,
this can be done using the findall() function from re

import re 
len(re.findall('0', str(number)))

findall() returns a list of all the occurrences that match the regex in the first argument, here '0' will only match a zero, the second argument is the string to check, here we pass the number cast as a string using str(). as findall() returns a list of occurrences we can pass the function as an argument to len() to get the number of occurrences

you can read more here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文