这是Python
temp_list=['1','2','3','5','7','8']
temp_list.sort()
print temp_list
test=""
first=""
last=""
start=0
for i in range(len(temp_list)):
if i==0:
None
else:
if (int(temp_list[i-1])+1)==int(temp_list[i]):
print temp_list[i-1]
print temp_list[i]
if start==0:
first=temp_list[i-1]
last=temp_list[i]
start=1;
else:
last=temp_list[i]
if len(temp_list)==i+1:
if start==0:
test+=(temp_list[i-1]+","+temp_list[i])
else:
if len(test)!=0:#add
test+=(","+first+"-"+last)
start=0
else:
test+=(first+"-"+last)
start=0
else:
if start==0:
test+=(temp_list[i-1]+","+temp_list[i])
else:
if len(test)!=0:#add
test+=(","+first+"-"+last)
start=0
else:
test+=(first+"-"+last)
start=0
print test
这是示例代码 此结果->;1-35,7,7-8
我要转换后面的数字集:
例1) ['1', '2', '3', '5', '7', '8'] -&燃气轮机; 1-3,5,7-8段
例2) ['0', '2', '3', '4', '5', '7', '8'] -&燃气轮机; 0,2-5,7-8个
请帮助我的大脑
这应该起作用:
它的工作原理是删除重复的数字,对它们进行排序,建立一个范围列表,然后格式化它们。示例:
相关问题 更多 >
编程相关推荐