newString = ''.join([i for i in originString if i in digitalList])
print newString
复制代码
作者: xuefu 时间: 2013-12-17 23:57
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
int main(int argc, const char *argv[])
{
char *s;
int len;
s = "aAsmr3idd4bgs7Dlsf9eAF";
len = strlen(s);
int i;
for (i = 0; i < len; i++)
{
if (isdigit(s[i]))
printf("%c", s[i]);
}
printf("\n");
return 0;
}
复制代码
作者: byron 时间: 2013-12-18 10:00
text = "aAsmr3idd4bgs7Dlsf9eAF"
#只保留数字
list(filter(str.isdigit, text))
#只保留字母
list(filter(str.isalpha, text))
复制代码
作者: 歪脑特 时间: 2013-12-18 10:07
初学者,用笨方法实现的
text = "aAsmr3idd4bgs7Dlsf9eAF"
num = ''
for i in range(len(text)):
if text[i].isnumeric():
num += text[i]
复制代码
作者: h01m3s 时间: 2013-12-18 13:17
# -*- coding: utf-8 -*-
'''
今天的坑:
有一个字符串
text = "aAsmr3idd4bgs7Dlsf9eAF"
请将text字符串中的数字取出,并输出成一个新的字符串。
'''
import re
def numonly(text):
num = re.findall(r'\d+', text)
print num
print ''.join(num)
text = "aAsmr3idd4bgs7Dlsf9eAF"
numonly(text)
复制代码
作者: TongShan 时间: 2013-12-18 14:19
import re
p = re.compile(r'\d')
text = "aAsmr3idd4bgs7Dlsf9eAF"
new_text = ''.join(p.findall(text))
复制代码
作者: zeq 时间: 2013-12-18 16:53
text = "aAsmr3idd4bgs7Dlsf9eAF"
new_text = filter(lambda x: x.isdigit(), text)
复制代码
作者: aurora 时间: 2013-12-18 17:50
def filter_digit_from_string(text):
return ''.join([i for i in text if i.isdigit()])作者: aurora 时间: 2013-12-18 17:58
def filter_digit_from_string(text):
return ''.join([i for i in text if i.isdigit()])作者: fl0w 时间: 2013-12-18 22:24
参考zeq同学,输出字母版
text = 'aAsmr3idd4bgs7Dlsf9eAF'
new_text = filter(lambda x: x.isalpha(), text)
复制代码
作者: liucy 时间: 2013-12-19 01:07
import re
import string
text = "aAsmr3idd4bgs7Dlsf9eAF"
list1 = re.findall( "\d", text)
print ''.join(list1)
复制代码
作者: Ares 时间: 2013-12-19 12:31
text = 'aAsmr3idd4bgs7Dlsf9eAF'
for i in text :
if '0' <= i <= '9':
print i, 作者: vose 时间: 2013-12-19 15:16
#!\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()
没编过程序,所以,哪怕有什么不好的编程习惯,编程思路,都请大神帮我指出来,谢谢
作者: fangweiren 时间: 2016-2-6 14:26
用正则
#coding:utf-8
import re
text = "aAsmr3idd4bgs7Dlsf9eAF"
result = re.findall(r'\d',text)
print result
复制代码
本办法
#coding:utf-8
text = "aAsmr3idd4bgs7Dlsf9eAF"
li = '0123456789'
list1 = []
for i in text:
if i in li:
list1.append(i)
print list1
复制代码
作者: 不许逗 时间: 2016-7-10 19:03
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
感觉我的方法应该还是比较奇葩的………………作者: 不许逗 时间: 2016-7-10 19:07
fangweiren 发表于 2016-2-6 14:26
用正则本办法
0123456789………………
服气作者: bingdashen 时间: 2016-8-8 20:18
新手试试水,楼主看看这个怎么样?
text="aAsmr3idd4bgs7DIsf9eAF"
a=list(text)
b=[]
for i in a :
if i.isdecimal():
b.append(i)
print([int(i)for i in b])作者: crossin先生 时间: 2016-8-9 17:01
text = "aAsmr3idd4bgs7Dlsf9eAF"
result = "".join([s for s in list(text) if s.isdigit()])
print(result) 作者: jssngz 时间: 2017-3-12 21:07
python 新手,正则还不会用 先用笨方法写出两种
text = "aAsmr3idd4bgs7Dlsf9eAF"
number = ''
for i in range(len(text)):
char =text[i]
if char.isdigit():
# python的内建函数,判断是否是数字
number += char
print(number)
# 第二种解决方法
text = "aAsmr3idd4bgs7Dlsf9eAF"
number = ''
for i in range(len(text)):
char =text[i]
try:
int(char)
number += char
except ValueError:
pass
print(number)
复制代码
作者: zhaozihang 时间: 2017-5-22 09:46
import re
text='aAsmr3idd4bgs7Dlsf9eAF'
print str(re.findall(r'\d+',text)) 作者: qingri 时间: 2017-6-7 23:22
# 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作者: crossin先生 时间: 2017-6-8 09:58
qingri 发表于 2017-6-7 23:22
# 3
# find all the numbers and combine them to a new string
import re
作者: zaizaicaocao 时间: 2017-7-27 21:32
text = "aAsmr3idd4bgs7Dlsf9eAF"
import re
num=re.findall('[0-9]+',text)
print(num)作者: zaizaicaocao 时间: 2017-7-27 21:33
import re
text = "aAsmr3idd4bgs7Dlsf9eAF"
num=re.findall('[0-9]+',text)
print(num)作者: wolfog 时间: 2017-8-18 11:20
def findDigit():
for i in text:
if i.isdigit():
listStr.append(i)
text = "aAsmr3idd4bgs7Dlsf9eAF?*"
listStr = []
findDigit()
作者: yy58710623 时间: 2017-9-26 18:22
text = "aAsmr3idd4bgs7Dlsf9eAF"
l=[]
for i in text:
if '0'<=i<='9':
l.append(i)
print(l)
#这个比较的原理是什么 作者: crossin先生 时间: 2017-9-26 20:12
yy58710623 发表于 2017-9-26 18:22
text = "aAsmr3idd4bgs7Dlsf9eAF"
l=[]
for i in text:
if a in ['0','1','2','3','4','5','6','7','8','9']:
b += a
else:
c += str(a)
print(b,c)
复制代码
作者: ZZZZZ 时间: 2021-5-2 09:46
text = input()
new_text = []
for i in text:
if i in list(range(0, 9)):
new_text.append(i)
for i in new_text:
print(i) 作者: chinaso1111 时间: 2021-7-6 14:57
text = "aAsmr3idd4bgs7Dlsf9eAF"
num = ''
for i in range(len(text)):
if text[i].isdigit():
print("'{}'".format(text[i]),end="")作者: chinaso1111 时间: 2021-7-6 21:21
for i in str1:
if i.isdigit() == True:
print(i,end="")
复制代码
if i.isdigit() == True:
print(i)作者: Danieldepython 时间: 2022-9-15 14:00
import re
text = "aAsmr3idd4bgs7Dlsf9eAF"
text_new = re.findall("\d",text)
复制代码
作者: xiaolx29 时间: 2023-12-23 21:53
from functools import reduce
text = "aAsmr3idd4bgs7Dlsf9eAF"
text2 = reduce(lambda a, b: ''.join((a, b)), (map(lambda i: i if i.isdigit() else '', text)))