请问在python中如何让'[1,2,3,6~10]'返回[1,2,3,6,7,8,9,10]列表?谢谢
2 回答
BIG阳
TA贡献1859条经验 获得超6个赞
deff(s):parts=s[1:-1].split(',')l=[]forpartinparts:ranges=part.split('~')iflen(ranges)==1:l.append(int(ranges[0]))else:foriinrange(int(ranges[0]),int(ranges[1])+1):l.append(i)returnlif__name__=='__main__':print(f('[1,2,3,6~10]'))
慕码人8056858
TA贡献1803条经验 获得超6个赞
fromfunctoolsimportreducesampleListStr='[1,2,3,6~10]'sampleList=sampleListStr.strip('[]').split(',')result=list(reduce(lambdax,y:x+y,[[int(element)]if'~'notinelementelselist(reduce(lambdax,y:range(x,y+1),[int(element1)forelement1inelement.split('~')]))forelementinsampleList]))
添加回答
举报
0/150
提交
取消