Sorting x lists based on one list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philippe C. Martin

    Sorting x lists based on one list

    Hi,

    I'm looking for an easy algorithm - maybe Python can help:

    I start with X lists which intial sort is based on list #1.

    I want to reverse sort list #1 and have all other lists sorted accordingly.

    Any idea is welcome.

    Regards,

    Philippe

  • Philippe C. Martin

    #2
    Re: Sorting x lists based on one list ... maybe an example would make sense:

    l1 = ['a','b','c']
    l2 = ['toto','titi',' tata'] # 'toto' refers to 'a', 'titi' to b' ....
    l3 = ['foo','bar','do e'] # 'foo' refers to 'a' ........

    I want to reverse sort l1 and have l2 and l3 follow accordingly.

    Regards,

    Philippe






    Philippe C. Martin wrote:
    [color=blue]
    > Hi,
    >
    > I'm looking for an easy algorithm - maybe Python can help:
    >
    > I start with X lists which intial sort is based on list #1.
    >
    > I want to reverse sort list #1 and have all other lists sorted
    > accordingly.
    >
    > Any idea is welcome.
    >
    > Regards,
    >
    > Philippe[/color]

    Comment

    • Peter Otten

      #3
      Re: Sorting x lists based on one list ... maybe an example would make sense:

      > Philippe C. Martin wrote:[color=blue]
      >[color=green]
      >> I'm looking for an easy algorithm - maybe Python can help:
      >> I start with X lists which intial sort is based on list #1.
      >> I want to reverse sort list #1 and have all other lists sorted
      >> accordingly.[/color][/color]

      One way, using a helper list with indices:
      [color=blue][color=green][color=darkred]
      >>> l1 = ['a','b','c']
      >>> l2 = ['toto','titi',' tata'] # 'toto' refers to 'a', 'titi' to b' ....
      >>> l3 = ['foo','bar','do e'] # 'foo' refers to 'a' ........
      >>> indices = sorted(range(le n(l1)), key=l1.__getite m__, reverse=True)
      >>> for items in l1, l2, l3:[/color][/color][/color]
      .... items[:] = [items[i] for i in indices]
      ....[color=blue][color=green][color=darkred]
      >>> l1[/color][/color][/color]
      ['c', 'b', 'a'][color=blue][color=green][color=darkred]
      >>> l2[/color][/color][/color]
      ['tata', 'titi', 'toto'][color=blue][color=green][color=darkred]
      >>> l3[/color][/color][/color]
      ['doe', 'bar', 'foo']

      Another way would be to merge the three lists into one of 3-tuples, sort,
      and unmerge, similarly to the DSU pattern -- which raises the question: why
      are you using three lists in the first place?

      Peter


      Comment

      • Philippe C. Martin

        #4
        Re: Sorting x lists based on one list ... maybe an example would make sense:

        > Another way would be to merge the three lists into one of 3-tuples, sort,[color=blue]
        > and unmerge, similarly to the DSU pattern -- which raises the question:
        > why are you using three lists in the first place?[/color]

        :-) Thanks, the lists will evolve and are also stored in 'csv' format in
        external files at one point. I cannot use dictionaries because I need to
        control the sorting (hash).

        In this specific case, list 1 represents students with their information,
        list 2 represents assignments with information such as weight, term, max
        grade ... and list 3 the actual grades.

        Regards,

        Philippe



        Peter Otten wrote:
        [color=blue][color=green]
        >> Philippe C. Martin wrote:
        >>[color=darkred]
        >>> I'm looking for an easy algorithm - maybe Python can help:
        >>> I start with X lists which intial sort is based on list #1.
        >>> I want to reverse sort list #1 and have all other lists sorted
        >>> accordingly.[/color][/color]
        >
        > One way, using a helper list with indices:
        >[color=green][color=darkred]
        >>>> l1 = ['a','b','c']
        >>>> l2 = ['toto','titi',' tata'] # 'toto' refers to 'a', 'titi' to b' ....
        >>>> l3 = ['foo','bar','do e'] # 'foo' refers to 'a' ........
        >>>> indices = sorted(range(le n(l1)), key=l1.__getite m__, reverse=True)
        >>>> for items in l1, l2, l3:[/color][/color]
        > ... items[:] = [items[i] for i in indices]
        > ...[color=green][color=darkred]
        >>>> l1[/color][/color]
        > ['c', 'b', 'a'][color=green][color=darkred]
        >>>> l2[/color][/color]
        > ['tata', 'titi', 'toto'][color=green][color=darkred]
        >>>> l3[/color][/color]
        > ['doe', 'bar', 'foo']
        >
        > Another way would be to merge the three lists into one of 3-tuples, sort,
        > and unmerge, similarly to the DSU pattern -- which raises the question:
        > why are you using three lists in the first place?
        >
        > Peter[/color]

        Comment

        • Philippe C. Martin

          #5
          Re: Sorting x lists based on one list ... maybe an example would make sense:

          I will look at that merge/unmerge thing


          Peter Otten wrote:
          [color=blue][color=green]
          >> Philippe C. Martin wrote:
          >>[color=darkred]
          >>> I'm looking for an easy algorithm - maybe Python can help:
          >>> I start with X lists which intial sort is based on list #1.
          >>> I want to reverse sort list #1 and have all other lists sorted
          >>> accordingly.[/color][/color]
          >
          > One way, using a helper list with indices:
          >[color=green][color=darkred]
          >>>> l1 = ['a','b','c']
          >>>> l2 = ['toto','titi',' tata'] # 'toto' refers to 'a', 'titi' to b' ....
          >>>> l3 = ['foo','bar','do e'] # 'foo' refers to 'a' ........
          >>>> indices = sorted(range(le n(l1)), key=l1.__getite m__, reverse=True)
          >>>> for items in l1, l2, l3:[/color][/color]
          > ... items[:] = [items[i] for i in indices]
          > ...[color=green][color=darkred]
          >>>> l1[/color][/color]
          > ['c', 'b', 'a'][color=green][color=darkred]
          >>>> l2[/color][/color]
          > ['tata', 'titi', 'toto'][color=green][color=darkred]
          >>>> l3[/color][/color]
          > ['doe', 'bar', 'foo']
          >
          > Another way would be to merge the three lists into one of 3-tuples, sort,
          > and unmerge, similarly to the DSU pattern -- which raises the question:
          > why are you using three lists in the first place?
          >
          > Peter[/color]

          Comment

          • Larry Bates

            #6
            Re: Sorting x lists based on one list ... maybe an example would

            Why not merge the lists together using zip() and then
            sort.

            info=zip(l1, l2, l3)
            info.sort()
            info.reverse

            Larry Bates

            Philippe C. Martin wrote:[color=blue]
            > l1 = ['a','b','c']
            > l2 = ['toto','titi',' tata'] # 'toto' refers to 'a', 'titi' to b' ....
            > l3 = ['foo','bar','do e'] # 'foo' refers to 'a' ........
            >
            > I want to reverse sort l1 and have l2 and l3 follow accordingly.
            >
            > Regards,
            >
            > Philippe
            >
            >
            >
            >
            >
            >
            > Philippe C. Martin wrote:
            >
            >[color=green]
            >>Hi,
            >>
            >>I'm looking for an easy algorithm - maybe Python can help:
            >>
            >>I start with X lists which intial sort is based on list #1.
            >>
            >>I want to reverse sort list #1 and have all other lists sorted
            >>accordingly .
            >>
            >>Any idea is welcome.
            >>
            >>Regards,
            >>
            >>Philippe[/color]
            >
            >[/color]

            Comment

            • Philippe C. Martin

              #7
              Re: Sorting x lists based on one list ... maybe an example would make sense:

              I had no clue this was feasible!

              Python folks should get the Nobel price !




              Larry Bates wrote:
              [color=blue]
              > Why not merge the lists together using zip() and then
              > sort.
              >
              > info=zip(l1, l2, l3)
              > info.sort()
              > info.reverse
              >
              > Larry Bates
              >
              > Philippe C. Martin wrote:[color=green]
              >> l1 = ['a','b','c']
              >> l2 = ['toto','titi',' tata'] # 'toto' refers to 'a', 'titi' to b' ....
              >> l3 = ['foo','bar','do e'] # 'foo' refers to 'a' ........
              >>
              >> I want to reverse sort l1 and have l2 and l3 follow accordingly.
              >>
              >> Regards,
              >>
              >> Philippe
              >>
              >>
              >>
              >>
              >>
              >>
              >> Philippe C. Martin wrote:
              >>
              >>[color=darkred]
              >>>Hi,
              >>>
              >>>I'm looking for an easy algorithm - maybe Python can help:
              >>>
              >>>I start with X lists which intial sort is based on list #1.
              >>>
              >>>I want to reverse sort list #1 and have all other lists sorted
              >>>accordingl y.
              >>>
              >>>Any idea is welcome.
              >>>
              >>>Regards,
              >>>
              >>>Philippe[/color]
              >>
              >>[/color][/color]

              Comment

              • Peter Otten

                #8
                Re: Sorting x lists based on one list ... maybe an example would make sense:

                Philippe C. Martin wrote:
                [color=blue]
                > :-) Thanks, the lists will evolve and are also stored in 'csv' format in
                > external files at one point. I cannot use dictionaries because I need to
                > control the sorting (hash).
                >
                > In this specific case, list 1 represents students with their information,
                > list 2 represents assignments with information such as weight, term, max
                > grade ... and list 3 the actual grades.[/color]

                This sounds like you should seriously consider using a database.

                Peter


                Comment

                • Ron Adam

                  #9
                  Re: Sorting x lists based on one list ... maybe an example would

                  Philippe C. Martin wrote:[color=blue][color=green]
                  >>Another way would be to merge the three lists into one of 3-tuples, sort,
                  >>and unmerge, similarly to the DSU pattern -- which raises the question:
                  >>why are you using three lists in the first place?[/color]
                  >
                  >
                  > :-) Thanks, the lists will evolve and are also stored in 'csv' format in
                  > external files at one point. I cannot use dictionaries because I need to
                  > control the sorting (hash).
                  >
                  > In this specific case, list 1 represents students with their information,
                  > list 2 represents assignments with information such as weight, term, max
                  > grade ... and list 3 the actual grades.
                  >
                  > Regards,
                  >
                  > Philippe[/color]

                  Hi Philippe,

                  As Peter suggested this sounds like a database. Especially if it will
                  evolve and grow to the point where it will not all fit in memory. Also
                  data bases already have a lot of ability to query, and generate sorted
                  reports built into them.

                  This is a fairly normal task for a relational data base where you have a
                  transaction list, (grades), that are connected to other lists,
                  (students, and assignments). This saves a lot of space by not
                  duplicating the student information and assignment information for each
                  grade given. It also makes updating student information easier because
                  it only needs to be updated once, and not changed everwhere it's referenced.

                  If your lists are not going to grow larger than what will fit in memory,
                  you can use dictionaries and lists. Loosely like the following...

                  - Read student info from cvs file into dictionary using student_id or
                  number as a key. Names can be used if they are unique. The content of
                  each student file will be a list, with None for missing items.

                  students[student_id] = (name, address, phone, etc....)

                  - Read assignments into dictionary using an assignment_no as key.

                  assignments[assignment_no] = (a_name, description, etc...)

                  - Create a grades list which will contain grade tuples:

                  [(date,student_i d,assignment_no ,grade), ... more grades ... ]


                  You can sort your grades list however you want. If you want to sort by
                  student name instead of student_id, you would use:

                  # Sort grades list by student name.
                  grades.sort(lam bda x,y: cmp(students[x[1]][0], students[y[1]][0]))

                  Assuming the name is in the first field in the student dictionary-value
                  tuple. There are probably other ways to do this that are more readable
                  or faster.

                  The grade list could also be filtered, so if you want to get all the
                  grades for a specific student, or all the grades for a particular
                  assignment you can just loop through grades list and print what you want.

                  An alternative query would be to get all the students who have not
                  completed an assignment: Get a list of all the student who have a grade
                  for the assignment, then use that to get a list from the student
                  dictionary, who are not in the students_comple ted_assignment list.

                  Anyway, just trying to give you some ideas.

                  Cheers,
                  _Ron

                  Comment

                  • Steven Bethard

                    #10
                    Re: Sorting x lists based on one list ... maybe an example would

                    Ron Adam wrote:[color=blue]
                    > You can sort your grades list however you want. If you want to sort by
                    > student name instead of student_id, you would use:
                    >
                    > # Sort grades list by student name.
                    > grades.sort(lam bda x,y: cmp(students[x[1]][0], students[y[1]][0]))
                    >
                    > Assuming the name is in the first field in the student dictionary-value
                    > tuple. There are probably other ways to do this that are more readable
                    > or faster.[/color]

                    Assuming that students[x[1]][0] is what you want to sort on, this may
                    also be written as:

                    grades.sort(key =lambda x: students[x[1]][0])

                    It will probably be somewhat faster, but depending on the size of your
                    data, you may not notice.

                    STeVe

                    Comment

                    • Ron Adam

                      #11
                      Re: Sorting x lists based on one list ... maybe an example would

                      Steven Bethard wrote:
                      [color=blue]
                      > Ron Adam wrote:[color=green]
                      >> grades.sort(lam bda x,y: cmp(students[x[1]][0], students[y[1]][0]))[/color][/color]
                      [color=blue]
                      > Assuming that students[x[1]][0] is what you want to sort on, this may
                      > also be written as:
                      >
                      > grades.sort(key =lambda x: students[x[1]][0])[/color]


                      Yes, I figured there was a better way to write it.

                      Thanks, Steve

                      Comment

                      Working...