Find consecutive numbers in the Python list (learned by netizens)

Source: Internet
Author: User
Tags python list

#-*-Coding:utf-8-*-

# python:2.x

__author__ = ' Administrator '

#学习这个要求的: http://wsky.org/archives/162.html

Import Sys

From itertools Import *

From operator Import *

Def parse (f):

d={}

For line in open (F, ' R '):

_line=line.split ()

D.setdefault (_line[0],[]). Append (_line[1])

For key in D.keys ():

data=sorted (Map (Lambda X:int (x[1:]), D[key]))

Sys.stdout.write (key+ ")

For K,g in GroupBy (Enumerate (data), Lambda (i,x): i-x):

Ret=map (Itemgetter (1), g)

If Len (ret) >1:

Sys.stdout.write (' s%d-%d '% (Ret[0],ret[-1]))

Elif Len (ret) ==1:

Sys.stdout.write (str (' s%s ')% (ret[0]+ ', '))

Sys.stdout.write (' \ n ')

If __name__== ' __main__ ':

F=SYS.ARGV[1]

Parse (f)

Find consecutive numbers in the Python list (learned by netizens)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.