List and order

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nic

    List and order

    Hello,
    I'm using the NetworkX Python package (https://networkx.lanl.gov/).
    Through this package I wrote the following code:

    import networkx as NX
    G=NX.Graph()
    G.add_edge(1,2)
    G.add_edge(2,3)
    G.add_edge(3,4)
    ddeg=G.degree(w ith_labels=True )
    for (u,v) in G.edges():
    print ddeg[u],ddeg[v]

    As result, I have:

    12
    22
    21

    I'd like to order the numbers included in the couples in a decrescent way:

    12
    12
    22

    And then to write the couples on a single line, always in a decrescent way:
    12 12 22

    I'm not able to do this operation. Can you help me please?
    Thanks a bunch,
    Nic


  • Miki

    #2
    Re: List and order

    Hello Nic,

    Python lists has a very powerfull buid-in "sort".

    edges = list(G.edges())
    edges.sort(key = lambda u, v: (ddeg(u), ddeg(v)))
    for u, v in edges:
    print u,v, # Note the training comma to avoid newline
    print

    HTH,
    Miki
    If it won't be simple, it simply won't be. [Hire me, source code]


    Comment

    • Nic

      #3
      Re: List and order

      Hello Miki,

      Many thanks for the support.
      I tried to insert and execute the code, but the following error happens:

      Traceback (most recent call last):
      File "grafodna.p y", line 10, in ?
      edges.sort(key = lambda u, v: (ddeg(u), ddeg(v)))
      TypeError: <lambda>() takes exactly 2 arguments (1 given)

      Do you know how is it possible to delete it?
      Thanks.

      Nic

      "Miki" <miki.tebeka@gm ail.com> ha scritto nel messaggio
      news:1147691967 .998184.285120@ u72g2000cwu.goo glegroups.com.. .[color=blue]
      > Hello Nic,
      >
      > Python lists has a very powerfull buid-in "sort".
      >
      > edges = list(G.edges())
      > edges.sort(key = lambda u, v: (ddeg(u), ddeg(v)))
      > for u, v in edges:
      > print u,v, # Note the training comma to avoid newline
      > print
      >
      > HTH,
      > Miki
      > http://pythonwise.blogspot.com
      >[/color]


      Comment

      • Peter Otten

        #4
        Re: List and order

        Nic wrote:
        [color=blue]
        > I tried to insert and execute the code, but the following error happens:
        >
        > Traceback (most recent call last):
        > File "grafodna.p y", line 10, in ?
        > edges.sort(key = lambda u, v: (ddeg(u), ddeg(v)))
        > TypeError: <lambda>() takes exactly 2 arguments (1 given)
        >
        > Do you know how is it possible to delete it?[/color]

        Note that

        lambda a, b: ...

        takes two arguments while

        lambda (a, b): ...

        takes one argument which must be a sequence (list, string, generator,...) of
        two items.

        So the above should probably be

        edges = list(G.edges())
        edges.sort(key= lambda (u, v): (ddeg[u], ddeg[v]))
        for u, v in edges:
        print ddeg[u], ddeg[v],
        print


        Here's how I would do it:

        edges = [(ddeg[u], ddeg[v]) for u, v in G.edges()]
        edges.sort()
        for a, b in edges:
        print a, b,
        print

        (all untested)

        Peter

        Comment

        • Nic

          #5
          Re: List and order

          Many thanks.
          Both the cases are OK.
          The only problem is that from:
          12
          22
          21
          In spite of writing
          12 12 22
          it writes
          12 21 22
          Do you know how is it possible to delete also this last trouble?
          Thanks a bunch,
          Nic

          "Peter Otten" <__peter__@web. de> ha scritto nel messaggio
          news:e49u2k$3i2 $02$1@news.t-online.com...[color=blue]
          > Nic wrote:
          >[color=green]
          >> I tried to insert and execute the code, but the following error happens:
          >>
          >> Traceback (most recent call last):
          >> File "grafodna.p y", line 10, in ?
          >> edges.sort(key = lambda u, v: (ddeg(u), ddeg(v)))
          >> TypeError: <lambda>() takes exactly 2 arguments (1 given)
          >>
          >> Do you know how is it possible to delete it?[/color]
          >
          > Note that
          >
          > lambda a, b: ...
          >
          > takes two arguments while
          >
          > lambda (a, b): ...
          >
          > takes one argument which must be a sequence (list, string, generator,...)
          > of
          > two items.
          >
          > So the above should probably be
          >
          > edges = list(G.edges())
          > edges.sort(key= lambda (u, v): (ddeg[u], ddeg[v]))
          > for u, v in edges:
          > print ddeg[u], ddeg[v],
          > print
          >
          >
          > Here's how I would do it:
          >
          > edges = [(ddeg[u], ddeg[v]) for u, v in G.edges()]
          > edges.sort()
          > for a, b in edges:
          > print a, b,
          > print
          >
          > (all untested)
          >
          > Peter[/color]


          Comment

          • Peter Otten

            #6
            Re: List and order

            Nic wrote:
            [color=blue]
            > The only problem is that from:
            > 12
            > 22
            > 21
            > In spite of writing
            > 12 12 22
            > it writes
            > 12 21 22
            > Do you know how is it possible to delete also this last trouble?[/color]

            I thought that the two 12 were a typo, but it seems you want to reorder the
            nodes inside an edge, too. Here's a fix that normalizes the edge before
            sorting the list of edges:

            edges = [sorted((ddeg[u], ddeg[v])) for u, v in G.edges()]
            edges.sort()
            for a, b in edges:
                print a, b,
            print

            Peter


            Comment

            • Nic

              #7
              Re: List and order

              Perfect. Thanks.
              Nic

              "Peter Otten" <__peter__@web. de> ha scritto nel messaggio
              news:e4aahn$l16 $01$1@news.t-online.com...[color=blue]
              > Nic wrote:
              >[color=green]
              >> The only problem is that from:
              >> 12
              >> 22
              >> 21
              >> In spite of writing
              >> 12 12 22
              >> it writes
              >> 12 21 22
              >> Do you know how is it possible to delete also this last trouble?[/color]
              >
              > I thought that the two 12 were a typo, but it seems you want to reorder
              > the
              > nodes inside an edge, too. Here's a fix that normalizes the edge before
              > sorting the list of edges:
              >
              > edges = [sorted((ddeg[u], ddeg[v])) for u, v in G.edges()]
              > edges.sort()
              > for a, b in edges:
              > print a, b,
              > print
              >
              > Peter
              >
              >[/color]


              Comment

              Working...