python如何去数字 python:数组里有数字和单词,如何将数字去掉
python\u4e2d\u5982\u4f55\u53bb\u6389\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b571.strip()\uff1a\u628a\u5934\u548c\u5c3e\u7684\u7a7a\u683c\u53bb\u6389
2.lstrip()\uff1a\u628a\u5de6\u8fb9\u7684\u7a7a\u683c\u53bb\u6389
3.rstrip()\uff1a\u628a\u53f3\u8fb9\u7684\u7a7a\u683c\u53bb\u6389
4.replace('c1','c2')\uff1a\u628a\u5b57\u7b26\u4e32\u91cc\u7684c1\u66ff\u6362\u6210c2\u3002\u6545\u53ef\u4ee5\u7528replace('
','')\u6765\u53bb\u6389\u5b57\u7b26\u4e32\u91cc\u7684\u6240\u6709\u7a7a\u683c
5.split()\uff1a\u901a\u8fc7\u6307\u5b9a\u5206\u9694\u7b26\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5207\u7247\uff0c\u5982\u679c\u53c2\u6570num
\u6709\u6307\u5b9a\u503c\uff0c\u5219\u4ec5\u5206\u9694
num
\u4e2a\u5b50\u5b57\u7b26\u4e32
\u65b9\u6cd5\u4e00\u3001\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f
\u7528\u6cd5\uff1a
## \u603b\u7ed3## ^ \u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u3002## $ \u5339\u914d\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u3002## \b \u5339\u914d\u4e00\u4e2a\u5355\u8bcd\u7684\u8fb9\u754c\u3002## \d \u5339\u914d\u4efb\u610f\u6570\u5b57\u3002## \D \u5339\u914d\u4efb\u610f\u975e\u6570\u5b57\u5b57\u7b26\u3002## x? \u5339\u914d\u4e00\u4e2a\u53ef\u9009\u7684 x \u5b57\u7b26 (\u6362\u8a00\u4e4b\uff0c\u5b83\u5339\u914d 1 \u6b21\u6216\u8005 0 \u6b21 x \u5b57\u7b26)\u3002## x* \u5339\u914d0\u6b21\u6216\u8005\u591a\u6b21 x \u5b57\u7b26\u3002## x+ \u5339\u914d1\u6b21\u6216\u8005\u591a\u6b21 x \u5b57\u7b26\u3002## x{n,m} \u5339\u914d x \u5b57\u7b26\uff0c\u81f3\u5c11 n \u6b21\uff0c\u81f3\u591a m \u6b21\u3002## (a|b|c) \u8981\u4e48\u5339\u914d a\uff0c\u8981\u4e48\u5339\u914d b\uff0c\u8981\u4e48\u5339\u914d c\u3002## (x) \u4e00\u822c\u60c5\u51b5\u4e0b\u8868\u793a\u4e00\u4e2a\u8bb0\u5fc6\u7ec4 (remembered group)\u3002\u4f60\u53ef\u4ee5\u5229\u7528 re.search \u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u7684 groups() \u51fd\u6570\u83b7\u53d6\u5b83\u7684\u503c\u3002## \u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u70b9\u53f7\u901a\u5e38\u610f\u5473\u7740 \u201c\u5339\u914d\u4efb\u610f\u5355\u5b57\u7b26\u201d
[plain] view plain copy
import re
string = u'127\u7c73'
print re.findall(r"\d+\.?\d*", string)
\u65b9\u6cd5\u4e8c\u3001\u5229\u7528filter(str.isdigit, iterable)
[plain] view plain copy
string = u'127\u7c73'
print (filter(str.isdigit, string))
bug\uff1aTypeError: descriptor 'isdigit' requires a 'str' object but received a 'unicode'
\u539f\u56e0\uff1astring\u4e0d\u662fstr\u7c7b\u578b
\u4fee\u6539\u4e3a\uff1a
[plain] view plain copy
string = u'127\u7c73'
string2 = string.encode('gbk')
print (type(str))
print (filter(str.isdigit, string2))
\u7ed3\u679c\uff1a
127
\u6ce8\u610f\uff1a\u8981\u63d0\u53d6\u7684\u5b57\u7b26\u4e32\u4e0d\u80fd\u547d\u540d\u4e3astr\uff0c\u5426\u5219\u4f1a\u51fa\u73b0TypeError: isdigit() takes no arguments (1 given)
\u56e0\u4e3astr\u548cfilter\u91cc\u7684str\u91cd\u540d\u4e86\u3002
import re
s = "我的手机号码是15381575555,你可以随机打给我"
new_s = re.sub("\d","",s)
print(new_s)
其中最主要的是re.sub(要查找的字符,替换成的字符,字符串变量)
运行后的结果是:我的手机号码是,你可以随机打给我
如果代码更改为:new_s = re.sub("\d","x",s)
运行后的结果是:我的手机号码是xxxxxxxxxxx,你可以随机打给我
绛旓細1銆乻trip()锛氭妸澶村拰灏剧殑绌烘牸鍘绘帀lstrip()锛氭妸宸﹁竟鐨勭┖鏍煎幓鎺塺strip()锛氭妸鍙宠竟鐨勭┖鏍煎幓鎺塺eplace(c1锛宑2)锛氭妸瀛楃涓查噷鐨刢1鏇挎崲鎴恈2銆2銆佽緭鍑猴細abcdefg鍦Python涓浣跨敤string.translate()鏂规硶浠庡瓧绗︿覆涓垹闄鏁板瓧Python2涓殑string.translate(map)鏂规硶灏嗘槧灏勮〃鎴栧瓧鍏镐綔涓鸿緭鍏ワ紝骞跺湪灏嗘寚瀹氱殑瀛楃鏇挎崲涓鸿緭鍏ユ槧灏...
绛旓細鍦╬ython涓彲浠ヤ娇鐢╮e杩欎釜妯″潡锛岃繖鏄鍒欒〃杈惧紡妯″潡锛屼互涓嬫槸涓涓ず渚嬶細import re s = "鎴戠殑鎵嬫満鍙风爜鏄15381575555锛屼綘鍙互闅忔満鎵撶粰鎴"new_s = re.sub("\d","",s)print(new_s)鍏朵腑鏈涓昏鐨勬槸re.sub(瑕佹煡鎵剧殑瀛楃,鏇挎崲鎴愮殑瀛楃,瀛楃涓插彉閲)杩愯鍚庣殑缁撴灉鏄細鎴戠殑鎵嬫満鍙风爜鏄紝浣犲彲浠ラ殢鏈烘墦缁欐垜 ...
绛旓細1.strip()锛氭妸澶村拰灏剧殑绌烘牸鍘绘帀 2.lstrip()锛氭妸宸﹁竟鐨勭┖鏍煎幓鎺 3.rstrip()锛氭妸鍙宠竟鐨勭┖鏍煎幓鎺 4.replace('c1','c2')锛氭妸瀛楃涓查噷鐨刢1鏇挎崲鎴恈2銆傛晠鍙互鐢╮eplace('','')鏉ュ幓鎺夊瓧绗︿覆閲岀殑鎵鏈夌┖鏍 5.split()锛氶氳繃鎸囧畾鍒嗛殧绗﹀瀛楃涓茶繘琛屽垏鐗囷紝濡傛灉鍙傛暟num 鏈夋寚瀹氬硷紝鍒欎粎鍒嗛殧 num 涓瓙瀛楃...
绛旓細濡傛灉瑕佸垹闄ゅ瓧绗︿覆涓殑鏌愪釜瀛楃锛屽彲浠ヤ娇鐢╮eplace()鏂规硶锛屽皢瑕佸垹闄ょ殑瀛楃鏇挎崲涓虹┖瀛楃涓銆備緥濡傦細鍒犻櫎瀛楃涓蹭腑鐨刟瀛楃 s = "banana"s = s.replace("a", "")print(s) # 杈撳嚭bnn 濡傛灉瑕佸垹闄ゅ瓧绗︿覆涓殑鏌愪釜瀛愪覆锛屼篃鍙互浣跨敤replace()鏂规硶锛屽皢瑕佸垹闄ょ殑瀛愪覆鏇挎崲涓虹┖瀛楃涓层備緥濡傦細鍒犻櫎瀛楃涓蹭腑鐨刟n瀛...
绛旓細str = 'a1b2c3-)'print filter(lambda x:x not in '0123456789',str)
绛旓細鍙互鍙傝冧笅闈㈢殑璧勬枡锛岃繃for 鐒跺悗浠嶢鐨勬暟缁勫彇鍑哄埌a锛岀劧鍚庡彇鍑 import re a = [鍒楄〃]c = []for x in a:c.append(re.findall(r'\d+',x))
绛旓細re.sub(r'$\d+','',inputtext)
绛旓細import redef fun(s): return re.sub(r'([\d]+)','',s).lower()print fun('Abc123De4f')
绛旓細浣犵湅鐪嬩笅闈㈢殑浠g爜琛屼笉琛岋紝浣犵殑鎷彿鏄叏瑙掔殑锛屾墍浠ユ垜灏辨寜鍏ㄨ鐨勫啓浜 import res = '锛-90锛/锛0锛/45/-45/锛-30锛/60'rt = re.findall('锛-*\d+锛', s)nl = [n[1:-1] for n in rt]print(nl)# 杈撳嚭 ['-90', '0', '-30']# 浠ヤ笂鏄嫭鍙风殑鍏堢瓟涓鍗 鏄庡ぉ鍐嶇瓟鍙︿竴鍗 ...
绛旓細浠ヤ笅鏄Python璇█鐨勪唬鐮佸疄鐜帮細python澶嶅埗浠g爜 m, n = map(int, input("璇疯緭鍏ヤ袱涓暣鏁癿鍜宯锛").split())if m < n:print(" m灏忎簬n")print(m, n)else:print(" m绛変簬n")print(m, n)棣栧厛锛屼娇鐢╩ap()鍑芥暟灏嗚緭鍏ョ殑瀛楃涓茶浆鎹负鏁存暟绫诲瀷锛岀劧鍚庝娇鐢╥nput()鍑芥暟鑾峰彇鐢ㄦ埛杈撳叆鐨勪袱涓暣鏁癿鍜宯銆