用python 求出0~100的数字中包含9的所有数字 用python提取文档内全部数字并求和

Python\u8f93\u5165\u82e5\u5e72\u4e2a0-9\u4e4b\u95f4\u7684\u6570\u5b57\uff0c\u5e76\u7edf\u8ba1\u8f93\u51fa0-9\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u6253\u5370\u5728\u4e00\u884c

\u5177\u4f53\u601d\u8def\uff1a \u9996\u5148\u751f\u621030\u4e2a0-9\u7684\u6570\u5b57 int []a=new int[30]; for(i=0,i<30,i++) { a[i]=Math.random()*10; } \u5224\u65ad\u51fa\u73b0\u4e86\u51e0\u6b21 int []b=new int[10]; //\u7528\u6765\u5b58\u653e\u6bcf\u4e00\u4e2a\u6570\u5b57\u51fa\u73b0\u4e86\u51e0\u6b21 for(i=0<i<30<i++) { switch(a[i]) case 0: b[0]++; breake; case 1: b[1]++; breake; . . . case 9: b[9]++ breake; }

\u56e0\u4e3a\u4e0d\u77e5\u9053 "sample.txt" \u7684\u5185\u5bb9\u662f\u4ec0\u4e48\u683c\u5f0f\uff0c\u6211\u731c\u662f\u8fd9\u6837\u7684\u95ee\u9898\uff1a
stuff = re.findall ('[0-9]+',line)\u7684\u610f\u601d\u662f\u5c06\u5b57\u7b26\u4e32 line \u4e2d\u7684\u6240\u6709\u8fde\u7eed\u7684\u6570\u5b57\u627e\u5230\uff0c\u7ec4\u6210\u6570\u7ec4
\u5f53 line \u4e2d\u7684\u5185\u5bb9\u5b58\u5728\u4e0d\u6b62\u4e00\u4e2a\u6570\u5b57\u7684\u65f6\u5019\uff0c\u4f8b\u5982\uff1a
123 234\u65f6\uff0cstuff \u7684\u7ed3\u679c\u4e3a\uff1a
['123', '234']\u8fd9\u6837\u7684\u6570\u7ec4\uff0c\u957f\u5ea6\u4e0d\u4e3a 1
\u8fd9\u65f6\uff0c\u8bed\u53e5
if len(stuff) != 1 :continue\u5c31\u4f1a\u8df3\u8fc7\u8fd9\u4e00\u5faa\u73af\uff0c\u4f7f\u5f97\u8fd9\u884c\u4e2d\u7684\u6570\u5b57\u6ca1\u6709\u88ab\u52a0\u5230\u603b\u548c\u4e2d\u3002

\u5982\u679c\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u4f8b\u5b50\u4ee3\u7801\uff1a
import redigits_reg = re.compile(r'\d+')with open('sample.txt', 'r') as file_handle: print sum(sum(map(int, digits_reg.findall(line))) for line in file_handle)
ps:
\u6211\u731c\u4f60\u7684\u8bed\u53e5
if len(stuff) != 1 :continue\u662f\u60f3\u8981\u8df3\u8fc7\u6ca1\u6709\u6570\u5b57\u7684\u884c\uff0c\u4f46\u662f\u6ca1\u6709\u8003\u8651\u591a\u4e2a\u6570\u5b57\u7684\u884c\uff0c\u6240\u4ee5\u5c11\u52a0\u4e86

# coding: utf-8
# python3.3

if __name__=="__main__":
    
    for i in range(101):
        if '9' in str(i):
            print("i = ", i)

这个可以实现,虽然不一定是最好的。。。



扩展阅读:学python后到底能干什么 ... python代码大全 ... python代码自动生成器 ... python数字0-10转汉字 ... python初学编程必背 ... python 从1加到10 ... python shutil copyfile ... python random ... python网站入口 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网