我有一个列表,其格式如下:
[ '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']
Run Code Online (Sandbox Code Playgroud)
对于列表的每个元素,我只需要用' - '替换前三个逗号,即列表应如下所示:
[ '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']
Run Code Online (Sandbox Code Playgroud)
我尝试使用替换,但它最终用' - '替换所有','
mylist = [x.replace(",","-") for x in mylist]
Run Code Online (Sandbox Code Playgroud)
我不想使用正则表达式,因为列表中的顺序可能会随着时间的推移而改变.请建议一个更好的方法来做到这一点?
用这个 : x.replace(",","-",3)
str.replace有第三个可选参数count.
帮助str.replace:
S.replace(old, new[, count]) -> string
Run Code Online (Sandbox Code Playgroud)
返回字符串S的副本,其中所有出现的substring old都替换为new.如果给出可选参数计数,则仅替换第一次计数.
| 归档时间: |
|
| 查看次数: |
178 次 |
| 最近记录: |