请选择 进入手机版 | 继续访问电脑版
设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生

【每日一坑 3】 找数字

[复制链接]

0

主题

0

好友

42

积分

新手上路

Rank: 1

发表于 2015-1-5 11:06:27 |显示全部楼层
遍历对比 很笨
回复

使用道具 举报

0

主题

0

好友

42

积分

新手上路

Rank: 1

发表于 2015-1-5 11:06:45 |显示全部楼层
  1. text = "aAsmr3idd4bgs7Dlsf9eAF"
  2. y = list(text)
  3. n = ['0','1', '2', '3', '4', '5', '6', '7', '8', '9',]
  4. l = []
  5. for i in range(len(y)-1):
  6.     if y[i] in n:
  7.         l.append(y[i])
  8. print ''.join(l)
复制代码
回复

使用道具 举报

0

主题

0

好友

8

积分

新手上路

Rank: 1

发表于 2015-9-1 03:43:14 |显示全部楼层
  1. import re

  2. text = "aAsmr3idd4bgs7Dlsf9eAF"

  3. a = re.findall('\d',text)
  4. print ''.join(a)
复制代码
不知道为什么,总觉得对于正则表达式的理解不够到位。
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2015-9-1 17:43:20 |显示全部楼层
十二君 发表于 2015-9-1 03:43
不知道为什么,总觉得对于正则表达式的理解不够到位。

正则本身就是个巨大的坑,我每次用都还要试一番
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

1

主题

0

好友

207

积分

中级会员

Rank: 3Rank: 3

发表于 2016-1-16 11:44:56 |显示全部楼层

  1. import re
  2. str = "aAsmr3idd4bgs7Dlsf9eAF"
  3. print(''.join(re.findall("\d",str)))
复制代码
回复

使用道具 举报

0

主题

1

好友

61

积分

注册会员

Rank: 2

发表于 2016-2-3 09:27:30 |显示全部楼层
本帖最后由 xqqxjnt1988 于 2016-2-3 09:31 编辑

交作业了,大神们请多提宝贵意见,我刚刚写,斗胆交上来请帮我看看

#!\usr\bin\python
#coding=utf-8
#author=xuqq
#这个程序是在一个字符串中找到数字,并把这些数字重新组成一个新的字符串
def num_find(strB):
    total_len=len(strB)
    str_new=[]
    if total_len>0:
        print"Begin to search:"
        for i in range (0,total_len):
            if (strB).isdigit()==True:
                str_new.append(strB)
            else:
                continue
        print "The final new str composed by number is that:\t" , str_new
    else:
        print"Nothing could be found!"
        
def main():
    text = "aAsmr3idd4bgs7Dlsf9eAF"
    num_find(text)
   
if __name__=='__main__':
    main()

没编过程序,所以,哪怕有什么不好的编程习惯,编程思路,都请大神帮我指出来,谢谢
   
回复

使用道具 举报

2

主题

0

好友

59

积分

注册会员

Rank: 2

发表于 2016-2-6 14:26:05 |显示全部楼层
用正则
  1. #coding:utf-8
  2. import re
  3. text = "aAsmr3idd4bgs7Dlsf9eAF"
  4. result = re.findall(r'\d',text)
  5. print result
复制代码
本办法
  1. #coding:utf-8
  2. text = "aAsmr3idd4bgs7Dlsf9eAF"
  3. li = '0123456789'
  4. list1 = []
  5. for i in text:
  6.         if i in li:
  7.                 list1.append(i)
  8. print list1       
复制代码
回复

使用道具 举报

1

主题

0

好友

33

积分

新手上路

Rank: 1

发表于 2016-7-10 19:03:32 |显示全部楼层
a = raw_input('input text\n')
b = list(a)
list_1 = []
for i in b:
    try:
        i = int(i) + 0
        list_1.append(i)
    except:
        continue
d = "".join([str(v) for v in list_1])
print d


感觉我的方法应该还是比较奇葩的………………
回复

使用道具 举报

1

主题

0

好友

33

积分

新手上路

Rank: 1

发表于 2016-7-10 19:07:54 |显示全部楼层
fangweiren 发表于 2016-2-6 14:26
用正则本办法

0123456789………………
服气
回复

使用道具 举报

0

主题

0

好友

12

积分

新手上路

Rank: 1

发表于 2016-8-8 20:18:38 |显示全部楼层
新手试试水,楼主看看这个怎么样?
text="aAsmr3idd4bgs7DIsf9eAF"
a=list(text)
b=[]
for i in a :
    if i.isdecimal():
       b.append(i)
print([int(i)for i in b])
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-3-29 19:20 , Processed in 0.018375 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部