请问在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]列表?谢谢
ABOUTYOU
浏览 268回答 2
2回答

大话西游666

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]'))

桃花长相依

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