x*z 发帖数: 1010 | 1 应该很简单,自己也能想出解决办法,但是肯定不是最优的,想找个
最优解。问题就是展开如下的range,变成一个sequence。
[0-3,4,8-9],13,[102-103,150],1000,...
-》
[0,1,2,3,4,8,9,13,102,103,150,1000,...]
我现在的想法就是先去匹配[],把[]全提出来后再按','去split,
然后再分别处理[]里面的内容,这样太复杂了,有什么简单办法么? | X****r 发帖数: 3557 | 2 What []s are for? i.e. what's the difference between
[1,2],3 and 1,2,3?
【在 x*z 的大作中提到】 : 应该很简单,自己也能想出解决办法,但是肯定不是最优的,想找个 : 最优解。问题就是展开如下的range,变成一个sequence。 : [0-3,4,8-9],13,[102-103,150],1000,... : -》 : [0,1,2,3,4,8,9,13,102,103,150,1000,...] : 我现在的想法就是先去匹配[],把[]全提出来后再按','去split, : 然后再分别处理[]里面的内容,这样太复杂了,有什么简单办法么?
| D*****r 发帖数: 6791 | 3 应该是[]里的内容可以带-,外面没有这种。
其实递归做很好做,但是如果输入当字符串处理,递归时候,传字符串岂不是复制了大
量的中间结果了?
要么就一个数一个数的读,遇到‘-’就进栈,然后出栈的时候,一口气出到截止的数
。直到出完。
【在 X****r 的大作中提到】 : What []s are for? i.e. what's the difference between : [1,2],3 and 1,2,3?
| M*P 发帖数: 6456 | 4 there is no difference in the example,just remove [] and split by , and
match -
【在 X****r 的大作中提到】 : What []s are for? i.e. what's the difference between : [1,2],3 and 1,2,3?
| X****r 发帖数: 3557 | 5 It is very easy if we just ignore '[]'s:
import re
re.sub('(\d+)-(\d+)', lambda m: ','.join(map(str, range(int(m.group(1)), int
(m.group(2))+1))), '[0-3,4,8-9],13,[102-103,150],1000', 0).replace('[', '').
replace(']', '')
【在 M*P 的大作中提到】 : there is no difference in the example,just remove [] and split by , and : match -
| l********a 发帖数: 1154 | 6
int
).
顶这个,very pythonic
re,lambda,map齐活儿了
【在 X****r 的大作中提到】 : It is very easy if we just ignore '[]'s: : import re : re.sub('(\d+)-(\d+)', lambda m: ','.join(map(str, range(int(m.group(1)), int : (m.group(2))+1))), '[0-3,4,8-9],13,[102-103,150],1000', 0).replace('[', ''). : replace(']', '')
| P********e 发帖数: 2610 | 7 我很好奇,你怎么什么语言都熟阿?
int
).
【在 X****r 的大作中提到】 : It is very easy if we just ignore '[]'s: : import re : re.sub('(\d+)-(\d+)', lambda m: ','.join(map(str, range(int(m.group(1)), int : (m.group(2))+1))), '[0-3,4,8-9],13,[102-103,150],1000', 0).replace('[', ''). : replace(']', '')
| X****r 发帖数: 3557 | 8 哪里有。Python我只是过得去而已。一个程序员总要写些脚本什么的。
【在 P********e 的大作中提到】 : 我很好奇,你怎么什么语言都熟阿? : : int : ).
| L*******r 发帖数: 1011 | 9 :D 你的过得去还是比大部分人强的.
【在 X****r 的大作中提到】 : 哪里有。Python我只是过得去而已。一个程序员总要写些脚本什么的。
| v*s 发帖数: 946 | 10 看懂了,受教了,原来re.sub 可以带lambda进去玩, 牛!!
int
).
【在 X****r 的大作中提到】 : It is very easy if we just ignore '[]'s: : import re : re.sub('(\d+)-(\d+)', lambda m: ','.join(map(str, range(int(m.group(1)), int : (m.group(2))+1))), '[0-3,4,8-9],13,[102-103,150],1000', 0).replace('[', ''). : replace(']', '')
| | | j*****k 发帖数: 1198 | 11 How to convert [1,2,3,4,5,6, 7,10, 15,17,19,21] to
'1-8, 10, 15-23-2'?
Thanks | p**o 发帖数: 3409 | 12
def parse( s ) :
result = ''
lst = [ eval(i) for i in s[1:-1].split(', ') ]
head, p0 = lst[0], lst[1]
diff0 = p0 - head
flag = True
num_consecutives = 2
for p in lst[2:] :
if not flag :
flag = True
diff0 = p - p0
p0 = p
continue
diff = p - p0
if flag and diff0 <> diff :
if 2 == num_consecutives :
ss = str(head)
head = p0
diff0 = p - p0
else:
ss = str(head) + '-' + str(p0)
if 1 <> diff0 :
ss += '-' + str(diff0)
head = p
flag = False
num_consecutives = 2
result += ss + ', '
else:
num_consecutives += 1
p0 = p
#end_for
if 2 == num_consecutives :
ss = str(head)
else:
ss = str(head) + '-' + str(p0)
if 1 <> diff0 :
ss += '-' + str(diff0)
result += ss
return result
if __name__ == '__main__' :
ss = ['[1, 2, 3, 4, 5, 6, 7, 10, 15, 17, 19, 21]',\
'[1, 3, 4, 5, 6, 7, 10, 15, 17, 19, 21, 24]',\
'[1, 3, 5, 6, 7, 10, 12, 15, 17, 19, 21, 24, 27, 30]' ]
for s in ss :
print( s )
print( parse( s ) )
print
[1, 2, 3, 4, 5, 6, 7, 10, 15, 17, 19, 21]
1-7, 10, 15-21-2
[1, 3, 4, 5, 6, 7, 10, 15, 17, 19, 21, 24]
1, 3-7, 10, 15-21-2, 24
[1, 3, 5, 6, 7, 10, 12, 15, 17, 19, 21, 24, 27, 30]
1-5-2, 6, 7, 10, 12, 15-21-2, 24-30-3
【在 j*****k 的大作中提到】 : How to convert [1,2,3,4,5,6, 7,10, 15,17,19,21] to : '1-8, 10, 15-23-2'? : Thanks
| x*z 发帖数: 1010 | 13 应该很简单,自己也能想出解决办法,但是肯定不是最优的,想找个
最优解。问题就是展开如下的range,变成一个sequence。
[0-3,4,8-9],13,[102-103,150],1000,...
-》
[0,1,2,3,4,8,9,13,102,103,150,1000,...]
我现在的想法就是先去匹配[],把[]全提出来后再按','去split,
然后再分别处理[]里面的内容,这样太复杂了,有什么简单办法么? | X****r 发帖数: 3557 | 14 What []s are for? i.e. what's the difference between
[1,2],3 and 1,2,3?
【在 x*z 的大作中提到】 : 应该很简单,自己也能想出解决办法,但是肯定不是最优的,想找个 : 最优解。问题就是展开如下的range,变成一个sequence。 : [0-3,4,8-9],13,[102-103,150],1000,... : -》 : [0,1,2,3,4,8,9,13,102,103,150,1000,...] : 我现在的想法就是先去匹配[],把[]全提出来后再按','去split, : 然后再分别处理[]里面的内容,这样太复杂了,有什么简单办法么?
| D*****r 发帖数: 6791 | 15 应该是[]里的内容可以带-,外面没有这种。
其实递归做很好做,但是如果输入当字符串处理,递归时候,传字符串岂不是复制了大
量的中间结果了?
要么就一个数一个数的读,遇到‘-’就进栈,然后出栈的时候,一口气出到截止的数
。直到出完。
【在 X****r 的大作中提到】 : What []s are for? i.e. what's the difference between : [1,2],3 and 1,2,3?
| M*P 发帖数: 6456 | 16 there is no difference in the example,just remove [] and split by , and
match -
【在 X****r 的大作中提到】 : What []s are for? i.e. what's the difference between : [1,2],3 and 1,2,3?
| X****r 发帖数: 3557 | 17 It is very easy if we just ignore '[]'s:
import re
re.sub('(\d+)-(\d+)', lambda m: ','.join(map(str, range(int(m.group(1)), int
(m.group(2))+1))), '[0-3,4,8-9],13,[102-103,150],1000', 0).replace('[', '').
replace(']', '')
【在 M*P 的大作中提到】 : there is no difference in the example,just remove [] and split by , and : match -
| l********a 发帖数: 1154 | 18
int
).
顶这个,very pythonic
re,lambda,map齐活儿了
【在 X****r 的大作中提到】 : It is very easy if we just ignore '[]'s: : import re : re.sub('(\d+)-(\d+)', lambda m: ','.join(map(str, range(int(m.group(1)), int : (m.group(2))+1))), '[0-3,4,8-9],13,[102-103,150],1000', 0).replace('[', ''). : replace(']', '')
| P********e 发帖数: 2610 | 19 我很好奇,你怎么什么语言都熟阿?
int
).
【在 X****r 的大作中提到】 : It is very easy if we just ignore '[]'s: : import re : re.sub('(\d+)-(\d+)', lambda m: ','.join(map(str, range(int(m.group(1)), int : (m.group(2))+1))), '[0-3,4,8-9],13,[102-103,150],1000', 0).replace('[', ''). : replace(']', '')
| X****r 发帖数: 3557 | 20 哪里有。Python我只是过得去而已。一个程序员总要写些脚本什么的。
【在 P********e 的大作中提到】 : 我很好奇,你怎么什么语言都熟阿? : : int : ).
| | | L*******r 发帖数: 1011 | 21 :D 你的过得去还是比大部分人强的.
【在 X****r 的大作中提到】 : 哪里有。Python我只是过得去而已。一个程序员总要写些脚本什么的。
| v*s 发帖数: 946 | 22 看懂了,受教了,原来re.sub 可以带lambda进去玩, 牛!!
int
).
【在 X****r 的大作中提到】 : It is very easy if we just ignore '[]'s: : import re : re.sub('(\d+)-(\d+)', lambda m: ','.join(map(str, range(int(m.group(1)), int : (m.group(2))+1))), '[0-3,4,8-9],13,[102-103,150],1000', 0).replace('[', ''). : replace(']', '')
| j*****k 发帖数: 1198 | 23 How to convert [1,2,3,4,5,6, 7,10, 15,17,19,21] to
'1-8, 10, 15-23-2'?
Thanks | p**o 发帖数: 3409 | 24
def parse( s ) :
result = ''
lst = [ eval(i) for i in s[1:-1].split(', ') ]
head, p0 = lst[0], lst[1]
diff0 = p0 - head
flag = True
num_consecutives = 2
for p in lst[2:] :
if not flag :
flag = True
diff0 = p - p0
p0 = p
continue
diff = p - p0
if flag and diff0 <> diff :
if 2 == num_consecutives :
ss = str(head)
head = p0
diff0 = p - p0
else:
ss = str(head) + '-' + str(p0)
if 1 <> diff0 :
ss += '-' + str(diff0)
head = p
flag = False
num_consecutives = 2
result += ss + ', '
else:
num_consecutives += 1
p0 = p
#end_for
if 2 == num_consecutives :
ss = str(head)
else:
ss = str(head) + '-' + str(p0)
if 1 <> diff0 :
ss += '-' + str(diff0)
result += ss
return result
if __name__ == '__main__' :
ss = ['[1, 2, 3, 4, 5, 6, 7, 10, 15, 17, 19, 21]',\
'[1, 3, 4, 5, 6, 7, 10, 15, 17, 19, 21, 24]',\
'[1, 3, 5, 6, 7, 10, 12, 15, 17, 19, 21, 24, 27, 30]' ]
for s in ss :
print( s )
print( parse( s ) )
print
[1, 2, 3, 4, 5, 6, 7, 10, 15, 17, 19, 21]
1-7, 10, 15-21-2
[1, 3, 4, 5, 6, 7, 10, 15, 17, 19, 21, 24]
1, 3-7, 10, 15-21-2, 24
[1, 3, 5, 6, 7, 10, 12, 15, 17, 19, 21, 24, 27, 30]
1-5-2, 6, 7, 10, 12, 15-21-2, 24-30-3
【在 j*****k 的大作中提到】 : How to convert [1,2,3,4,5,6, 7,10, 15,17,19,21] to : '1-8, 10, 15-23-2'? : Thanks
| w****i 发帖数: 964 | 25 def parse(s):
d2 = [1, 1] + [s[i]*2-s[i-1]-s[i+1] for i in xrange(1, len(s)-1)] + [1,
1]
outstr = ''
for i in xrange(len(s)):
if d2[i+1] == 0: continue
if d2[i] != 0 and d2[i+2] != 0: outstr += ', %d' % s[i]
if d2[i] == 0:
outstr += ', %d-%d' % (s[start],s[i])
if s[i] - s[i-1] > 1: outstr += '-%d' % (s[i]-s[i-1])
d2[i+2] = 1
if d2[i+2] == 0: start = i
return outstr[2:] | w****i 发帖数: 964 | 26 def parse(s):
d2 = [1, 1] + [s[i]*2-s[i-1]-s[i+1] for i in xrange(1, len(s)-1)] + [1,
1]
outstr = ''
for i in xrange(len(s)):
if d2[i+1] == 0: continue
if d2[i] != 0 and d2[i+2] != 0: outstr += ', %d' % s[i]
if d2[i] == 0:
outstr += ', %d-%d' % (s[start],s[i])
if s[i] - s[i-1] > 1: outstr += '-%d' % (s[i]-s[i-1])
d2[i+2] = 1
if d2[i+2] == 0: start = i
return outstr[2:] | w****i 发帖数: 964 | 27 def parse(s):
d2 = [1, 1] + [s[i]*2-s[i-1]-s[i+1] for i in xrange(1, len(s)-1)] + [1,
1]
outstr = ''
for i in xrange(len(s)):
if d2[i+1] == 0: continue
if d2[i] != 0 and d2[i+2] != 0: outstr += ', %d' % s[i]
if d2[i] == 0:
outstr += ', %d-%d' % (s[start],s[i])
if s[i] - s[i-1] > 1: outstr += '-%d' % (s[i]-s[i-1])
d2[i+2] = 1
if d2[i+2] == 0: start = i
return outstr[2:] | j*****k 发帖数: 1198 | 28 Both solutions are very good. In westxi's solution, it would be better if
change %d to %s, s[i] - s[i-1] > 1 to s[i] - s[i-1] != 1
Thanks, very much | j*****k 发帖数: 1198 | 29 Pulo, if the array is [1, 3, 5, 6, 7, 10, 12, 15, 17, 19, 21, 24, 27, 30, 8,
10], your code will have a problem to output the last one | j*****k 发帖数: 1198 | 30 Here is another version:
def parse(m_Array):
m_Out = '%s'%m_Array[0]
m_Flag = 0
m_Array = m_Array + [1]
for i in range(1,len(m_Array)-1):
if (m_Array[i] - m_Array[i - 1]) == (m_Array[i + 1] - m_Array[i]):
m_Flag += 1
if m_Flag == 0:
m_Out += '%s'%m_Array[i]
else:
if m_Flag >= 1:
m_Out += ':%s'% m_Array[i]
m_Step = m_Array[i] - m_Array[i-1]
if m_Step <> 1:
m_Out += ':%s'%m_Step
if i<>len(m_Array)-2: m_Out += ', '
m_Flag = -1
elif m_Flag ==0:
m_Out += ", %s"% m_Array[i]
elif m_Flag == -1:
m_Out += "%s"% m_Array[i]
m_Flag = 0
return m_Out | | | p**o 发帖数: 3409 | 31 我假设输入的list是排过序的,而且没有重复元素
你可以先把list转成set再转回list,完成排重和排序
lst = [1, 3, 5, 6, 7, 10, 12, 15, 17, 19, 21, 24, 27, 30, 8, 10]
lst = list( set( lst ) )
输出
[1, 3, 5, 6, 7, 8, 10, 12, 15, 17, 19, 21, 24, 27, 30]
1-5-2, 6-8, 10, 12, 15-21-2, 24-30-3
8,
【在 j*****k 的大作中提到】 : Pulo, if the array is [1, 3, 5, 6, 7, 10, 12, 15, 17, 19, 21, 24, 27, 30, 8, : 10], your code will have a problem to output the last one
|
|