正则表达式
1、findall的用法 HJ33
import re def change_ipaddr(ip): ip = ip.split(".") res = '' for i in ip: i = bin(int(i))[2:].rjust(8,'0') res +=i return int(res,2) def change_number(number): ip_list = [] data = bin(int(number))[2:].rjust(32,'0') data = re.findall(r'.{8}',data) for i in data: ip_list.append(str(int(i,2))) return '.'.join(ip_list) while True: try: num = change_ipaddr(input()) ip = change_number(input()) print(num) print(ip) except: break
1 import re 2 3 s = input() 4 # A Famous Saying: Much Ado About Nothing (2012/8). 5 print(s) 6 7 ss=[] 8 for i in s: 9 if i.isalpha() == True: 10 ss+=i.upper() 11 print(ss) 12 ss=sorted(set(ss)) 13 print(ss) 14 s_new=[] 15 for i in ss: 16 s_new+=re.findall(i,s,flags=re.I) #列表的运算 17 print(s_new) 18 19 for j in range(len(s)): 20 if s[j].isalpha() == False: 21 s_new.insert(j,s[j]) 22 23 print(s_new) 24 print(''.join(s_new)) ME
import re 2 while 1: 3 try: 4 s=input().strip() 5 #把字母取出来,然后去重,排序,得到了字母序列 6 sl=sorted(set([i.lower() for i in s if i.isalpha()])) 7 #字母序列 8 ll=[] 9 for i in sl: 10 ll+=re.findall(i,s,re.I) #从字母表SL中取i,(同时忽略大小写)与原字符串匹配,找到了,就加入到新的字符串中, 11 #字符串出现的每种字母按原序列添加到列表中 12 for j in range(len(s)): 13 if not s[j].isalpha(): 14 ll.insert(j,s[j]) #将原来的非字母,插入到新字符串的在原字符串中的位置 15 print(''.join(ll)) 16 #将非字母出现的位置设为原来的字符 17 except: 18 break
2、search的用法 HJ29
import re while True: try: s = input() ss = '' for i in s: if re.search('[A-Z]', i): if 'A' <= i < 'Z': ss += chr(ord(i) - 1).lower() elif i == 'Z': ss += 'a' elif re.search('[a-z]', i): if 'a' <= i < 'z': ss += chr(ord(i) + 1).upper() elif i == 'z': ss += 'A' elif re.search('[0-9]', i): if '0' <= i < '9': ss += str(int(i) + 1) elif i == '9': ss += '0' elif re.search('[^0-9a-zA-z]', i): ss += i print(ss) except: break
1 #我的 2 import re 3 s = input() 4 ss =s.split(";") 5 print(ss) 6 L1=[] 7 x=0 8 y=0 9 powid=(x,y) 10 11 for i in ss: 12 if re.search('^[ASWD]{1}\d{1,2}$',i): 13 L1.append(i) 14 print(L1) 15 16 17 18 for j in L1: 19 if re.split('\d+',i)[0]=='A': 20 x =x-int(re.split('^A')[1]) 21 22 elif re.split('\d+',i)[0]=='S': 23 y=y-int(re.split('^S')[1]) 24 25 elif re.split('\d+',i)[0]=='D': 26 x=x+int(re.split('^D')[1]) 27 28 elif re.split('\d+',i)[0]=='W': 29 y=y+int(re.split('^W')[1]) 30 31 print(x,y) 32 33 34 #################################################################### 35 import re 36 37 coord = input().split(";") 38 coord = filter(lambda x: re.match("^[ASDW]{1}\d{1,2}$", x), coord) 39 a, b = 0, 0 40 for c in coord: 41 if c[0] == "W": 42 b += int(c[1:]) 43 elif c[0] == "S": 44 b -= int(c[1:]) 45 elif c[0] == "A": 46 a -= int(c[1:]) 47 elif c[0] == "D": 48 a += int(c[1:]) 49 50 print("{},{}".format(a, b)) 51 #################################################################### 52 input_list = input().split(';') 53 initial = [0,0] 54 55 for item in input_list: 56 if not 2 <= len(item) <= 3: 57 continue 58 59 try: 60 direction = item[0] 61 step = int(item[1:]) 62 if direction in ['A', 'D', 'W', 'S']: 63 if 0 <= step <= 99: 64 if direction == 'A': 65 initial[0] -= step 66 elif direction == 'D': 67 initial[0] += step 68 elif direction == 'S': 69 initial[1] -= step 70 elif direction == 'W': 71 initial[1] += step 72 except: 73 continue 74 75 print(str(initial[0]) + ',' + str(initial[1])) 76 77 ########################################################### 78 79 #不成熟 80 import re 81 d = input().split(";") 82 result = [0,0] 83 84 regex = re.compile(r'1[1-9]?[0-9]$') 85 l = [] 86 for s in d: 87 if regex.match(s): 88 l.append(s) 89 for item in l: 90 letter = item[0] 91 if letter == "A": 92 result[0] = result[0]-int(item[1:]) 93 elif letter == "D": 94 result[0] = result[0]+int(item[1:]) 95 elif letter == "S": 96 result[1] = result[1]-int(item[1:]) 97 else: result[1] = result[1]+int(item[1:]) 98 99 print(str(result[0]) + ',' + str(result[1])) 参考答案