在线等!请问在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]列表?谢谢
慕姐4208626
浏览 340回答 2
2回答

慕田峪4524236

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