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

Crossin的编程教室

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

【每日一坑 3】 找数字

[复制链接]

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2016-8-9 17:01:35 |显示全部楼层
bingdashen 发表于 2016-8-8 20:18
新手试试水,楼主看看这个怎么样?
text="aAsmr3idd4bgs7DIsf9eAF"
a=list(text)

可以,你这是python3吧
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

12

积分

新手上路

Rank: 1

发表于 2016-8-10 08:44:03 |显示全部楼层
crossin先生 发表于 2016-8-9 17:01
可以,你这是python3吧

对。
回复

使用道具 举报

0

主题

0

好友

26

积分

新手上路

Rank: 1

发表于 2017-1-20 10:36:58 |显示全部楼层
本帖最后由 morpheus2222 于 2017-1-20 10:39 编辑

text = "aAsmr3idd4bgs7Dlsf9eAF"
result = "".join([s for s in list(text) if s.isdigit()])
print(result)
回复

使用道具 举报

0

主题

0

好友

12

积分

新手上路

Rank: 1

发表于 2017-3-12 21:07:22 |显示全部楼层
python 新手,正则还不会用 先用笨方法写出两种
  1. text = "aAsmr3idd4bgs7Dlsf9eAF"
  2. number = ''
  3. for i in range(len(text)):
  4.     char =text[i]
  5.     if char.isdigit():
  6.         # python的内建函数,判断是否是数字
  7.         number += char
  8. print(number)
  9. # 第二种解决方法
  10. text = "aAsmr3idd4bgs7Dlsf9eAF"
  11. number = ''
  12. for i in range(len(text)):
  13.     char =text[i]
  14.     try:
  15.         int(char)
  16.         number += char
  17.     except ValueError:
  18.         pass
  19. print(number)
复制代码
回复

使用道具 举报

1

主题

0

好友

33

积分

新手上路

Rank: 1

发表于 2017-5-22 09:46:55 |显示全部楼层
import re
text='aAsmr3idd4bgs7Dlsf9eAF'

print str(re.findall(r'\d+',text))
回复

使用道具 举报

0

主题

0

好友

14

积分

新手上路

Rank: 1

发表于 2017-6-7 23:22:19 |显示全部楼层
# 3
# find all the numbers and combine them to a new string
import re
text = "aAsmr3idd42bgs72Dlsf23eAF"
# regular expression find all the numbers which length greater equal than 1
nums = re.findall("[0-9]+",text)
# check the output
print nums
# join them together
new_str = ''.join(nums)
# check the type of new string
print type(new_str)
# print new string
print new_str
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2017-6-8 09:58:04 |显示全部楼层
qingri 发表于 2017-6-7 23:22
# 3
# find all the numbers and combine them to a new string
import re

#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

26

积分

新手上路

Rank: 1

发表于 2017-7-27 21:32:16 |显示全部楼层
text = "aAsmr3idd4bgs7Dlsf9eAF"
import re
num=re.findall('[0-9]+',text)
print(num)
回复

使用道具 举报

0

主题

0

好友

26

积分

新手上路

Rank: 1

发表于 2017-7-27 21:33:56 |显示全部楼层
import re
text = "aAsmr3idd4bgs7Dlsf9eAF"

num=re.findall('[0-9]+',text)
print(num)
回复

使用道具 举报

1

主题

0

好友

39

积分

新手上路

Rank: 1

发表于 2017-8-18 11:20:36 |显示全部楼层
def findDigit():
    for i in text:
        if i.isdigit():
            listStr.append(i)
text = "aAsmr3idd4bgs7Dlsf9eAF?*"
listStr = []
findDigit()

print "".join(listStr)
回复

使用道具 举报

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

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

GMT+8, 2024-3-28 22:03 , Processed in 0.026380 second(s), 20 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部