【发布时间】:2013-06-22 17:31:35
【问题描述】:
我有一个格式如下的列表:
[ '2013,june,25,11,img1.ams.expertcity.com,/builds/g2m/1172/G2M_Mac_x86,84.83.189.112,3', '2013,june,25,11,img1.ams.expertcity.com,/builds/g2m/1172/G2MInstallerExtractor.exe,85.164.14.248,6', '2013,june,25,11,img1.syd.expertcity.com,/builds/g2m/1172/G2MCoreInstExtractor.exe,99.245.80.126,19']
我只需要将列表中每个元素的前三个逗号替换为“-”,即列表应如下所示:
[ '2013-june-25-11,img1.ams.expertcity.com,/builds/g2m/1172/G2M_Mac_x86,84.83.189.112,3', '2013-june-25-11,img1.ams.expertcity.com,/builds/g2m/1172/G2MInstallerExtractor.exe,85.164.14.248,6', '2013-june-25-11,img1.syd.expertcity.com,/builds/g2m/1172/G2MCoreInstExtractor.exe,99.245.80.126,19']
我尝试使用替换,但最终将所有“,”替换为“-”
mylist = [x.replace(",","-") for x in mylist]
我不想使用正则表达式,因为列表中的顺序可能会随着时间而改变。 请建议一个更好的方法来做到这一点?
【问题讨论】: