python如何把一个字符串均匀切片?
已知:data_list=['06091214202810', '09101218293214', '04161819272804', '02060715202115'']求解:list_1=['06', '09', '12', '14', '20', '28', '10','09', '10', '12', '18', '29', '32', '14', '04', '16', '18', '19', '27', '28', '04', '02','06', '07', '15', '20',' 21', '15']
是的,没猜错,就是爬取的最近几期彩票的开奖码,可惜是开过的55555
现在的问题是,我需要把'06091214202810',分解成['06', '09', '12', '14', '20', '28', '10'],大佬们有没有简短的好方法?因为后续我打算每期开奖码以期号单独为一个变量,例如:issue_2022103 = ['06', '09', '12', '14', '20', '28', '10'],靠我这个方法顶不住哇。
目前方式:
list_1 = []
for i in data_list:
list_1.append(int(i))
list_1.append(int(i))
list_1.append(int(i))
list_1.append(int(i))
list_1.append(int(i))
list_1.append(int(i))
list_1.append(int(i))
print(list_1) 本帖最后由 cflying 于 2022-9-8 20:49 编辑
import re
data_list=['06091214202810', '09101218293214', '04161819272804', '02060715202115']
for i in data_list:
print(re.findall('\d{2}',i))
或者这种也行
import re
data_list=['06091214202810', '09101218293214', '04161819272804', '02060715202115']
print(re.findall('\d{2}',str(data_list)))
自己改成list即可 本帖为转载帖
见https://blog.csdn.net/qdPython/article/details/105751520
可以这样:
data_list = ['06091214202810', '09101218293214', '04161819272804', '02060715202115']
ist_1 = []
for i in data_list:
for j in range(0, len(i), 2):
ist_1.append(i)
print(ist_1)
列表推导式简化代码:
data_list = ['06091214202810', '09101218293214', '04161819272804', '02060715202115']
ist_1 = for i in data_list for j in range(0, len(i), 2)]
print(ist_1)
data_list = ['06091214202810', '09101218293214', '04161819272804', '02060715202115']
list_1 = for k in data_list for i in range(int(len(k)/2))]
print(list_1)
页:
[1]