1
\$\begingroup\$
def range_find(pageset):

    pagerangelist=[]

    for page in list(pageset):

        if page in pageset and page-1 in pageset:pagerangelist[-1].append(page)
        elif page in pageset and not (page-1 in pageset):pagerangelist.append([page])

    pagerangestringlist=[]

    for pagerange in pagerangelist:

        if len(pagerange)==1:pagerangestringlist.append(str(pagerange[0]))
        else: pagerangestringlist.append(str(pagerange[0])+'-'+str(pagerange[-1]))      

    return ','.join(pagerangestringlist)
\$\endgroup\$
3
  • \$\begingroup\$ why so many versions? and why haven't you learned to format your posts correctly yet? \$\endgroup\$ Commented Oct 8, 2011 at 0:20
  • \$\begingroup\$ 1)I do not understand the point about formatting...I simply cut and paste into the text field. What am I supposed to do? Or is it a question of the title. 2) I created different versions in response to feedback. If I edit the original post, then it would make it hard to understand the original comments. Or am I missing something about the way this web site works? \$\endgroup\$ Commented Oct 8, 2011 at 1:10
  • 1
    \$\begingroup\$ The textfield isn't a code box, its a general formatting box. You need to specifically tell it that you have put code in your post by using the toolbar button it gives you. Most people who post here edit their posts if they have new versions. \$\endgroup\$ Commented Oct 8, 2011 at 2:00

1 Answer 1

1
\$\begingroup\$
def range_find(pageset):

    pagerangelist=[]

    for page in list(pageset):

There is no point in listifying the set just to iterate over it. I think you meant for page in sorted(pageset) to get access to the page in sorted order.

        if page in pageset and page-1 in pageset:pagerangelist[-1].append(page)
        elif page in pageset and not (page-1 in pageset):pagerangelist.append([page])

You still make it hard to read by putting those on the same line.

    pagerangestringlist=[]

If you've got multiple words I suggest under scores. I also suggest dropping types like list/set from your names. I'd call this page_range_strings.

    for pagerange in pagerangelist:

        if len(pagerange)==1:pagerangestringlist.append(str(pagerange[0]))
        else: pagerangestringlist.append(str(pagerange[0])+'-'+str(pagerange[-1]))      

I'm going to recommend using string formating. It'll be easier to read then concatenating strings.

    return ','.join(pagerangestringlist)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.