在线等!请问在python中如何让'[1,2,3,6~10]'返回[1,2,3,6,7,8,9,10]列表?跪求!

请问在python中如何让'[1,2,3,6~10]'返回[1,2,3,6,7,8,9,10]列表?谢谢
一只斗牛犬
浏览 346回答 2
2回答

BIG阳

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

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]))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript