Problem involving sets...

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

    Problem involving sets...

    Kind of a fun but confusing problem...

    I have two lists. Each list contains elements of two-element lists.
    l1 = [['c1',1],['c2',2],['c3',4]]
    l2 = [['c1',1],['c2',2],['c4',4],['c3',3]]

    Exactly in this format, where
    superlist[0][0] is always a string
    superlist[0][1] is always an integer

    Now what I would like to do is find the intersect of those two
    super-lists based on superlist[0][0] and then compare the integers to
    find ultimately:
    A list of strings of the intersect of l1/l2, where the l1[x][1] >
    l2[x][1]
    In the case of the above example, that would be simply:
    ['c3']


    But since lists are unhashable, I can't make a set consisting of
    lists... Maybe I am making this problem too difficult by thinking in
    terms of sets. Does anyone have a simple solution????

    -Thanks

  • Felipe Almeida Lessa

    #2
    Re: Problem involving sets...

    Em Sex, 2006-04-14 às 15:43 -0700, flamesrock escreveu:[color=blue]
    > Does anyone have a simple solution????[/color]

    $ python2.4
    Python 2.4.3 (#2, Mar 30 2006, 21:52:26)
    [GCC 4.0.3 (Debian 4.0.3-1)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> l1 = [['c1',1],['c2',2],['c3',4]]
    >>> l2 = [['c1',1],['c2',2],['c4',4],['c3',3]]
    >>> print [tuple(x) for x in l1][/color][/color][/color]
    [('c1', 1), ('c2', 2), ('c3', 4)][color=blue][color=green][color=darkred]
    >>> print [tuple(x) for x in l2][/color][/color][/color]
    [('c1', 1), ('c2', 2), ('c4', 4), ('c3', 3)][color=blue][color=green][color=darkred]
    >>> s1 = frozenset(tuple (x) for x in l1)
    >>> s2 = frozenset(tuple (x) for x in l2)
    >>> print s1[/color][/color][/color]
    frozenset([('c2', 2), ('c1', 1), ('c3', 4)])[color=blue][color=green][color=darkred]
    >>> print s2[/color][/color][/color]
    frozenset([('c2', 2), ('c4', 4), ('c3', 3), ('c1', 1)])[color=blue][color=green][color=darkred]
    >>> print s2-s1[/color][/color][/color]
    frozenset([('c4', 4), ('c3', 3)])[color=blue][color=green][color=darkred]
    >>> print[list(x) for x in s2-s1][/color][/color][/color]
    [['c4', 4], ['c3', 3]]


    --
    Felipe.

    Comment

    • Kent Johnson

      #3
      Re: Problem involving sets...

      flamesrock wrote:[color=blue]
      > Kind of a fun but confusing problem...
      >
      > I have two lists. Each list contains elements of two-element lists.
      > l1 = [['c1',1],['c2',2],['c3',4]]
      > l2 = [['c1',1],['c2',2],['c4',4],['c3',3]]
      >
      > Exactly in this format, where
      > superlist[0][0] is always a string
      > superlist[0][1] is always an integer
      >
      > Now what I would like to do is find the intersect of those two
      > super-lists based on superlist[0][0] and then compare the integers to
      > find ultimately:
      > A list of strings of the intersect of l1/l2, where the l1[x][1] >
      > l2[x][1]
      > In the case of the above example, that would be simply:
      > ['c3'][/color]

      In [5]: l1 = [['c1',1],['c2',2],['c3',4]]

      In [6]: l2 = [['c1',1],['c2',2],['c4',4],['c3',3]]

      In [7]: d=dict(l1)

      In [10]: [k for k,v in l2 if k in d and v < d[k]]
      Out[10]: ['c3']

      Kent

      Comment

      • flamesrock

        #4
        Re: Problem involving sets...

        wowowow now THAT is what I call simple!

        Comment

        • Dave Hughes

          #5
          Re: Problem involving sets...

          Kent Johnson wrote:
          [color=blue]
          > flamesrock wrote:[color=green]
          > > Kind of a fun but confusing problem...
          > >
          > > I have two lists. Each list contains elements of two-element lists.
          > > l1 = [['c1',1],['c2',2],['c3',4]]
          > > l2 = [['c1',1],['c2',2],['c4',4],['c3',3]]
          > >
          > > Exactly in this format, where
          > > superlist[0][0] is always a string
          > > superlist[0][1] is always an integer
          > >
          > > Now what I would like to do is find the intersect of those two
          > > super-lists based on superlist[0][0] and then compare the integers
          > > to find ultimately:
          > > A list of strings of the intersect of l1/l2, where the l1[x][1] >
          > > l2[x][1]
          > > In the case of the above example, that would be simply:
          > > ['c3'][/color]
          >
          > In [5]: l1 = [['c1',1],['c2',2],['c3',4]]
          >
          > In [6]: l2 = [['c1',1],['c2',2],['c4',4],['c3',3]]
          >
          > In [7]: d=dict(l1)
          >
          > In [10]: [k for k,v in l2 if k in d and v < d[k]]
          > Out[10]: ['c3']
          >
          > Kent[/color]

          The dict solution posted above is definitely better than the following,
          but purely out of idle curiosity I was interested to see if this could
          be done in one line using just list comprehensions ...

          Python 2.4.2 (#1, Nov 15 2005, 15:54:06)
          [GCC 3.3.6 (Gentoo 3.3.6, ssp-3.3.6-1.0, pie-8.7.8)] on linux2
          Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
          >>> l1 = [['c1',1],['c2',2],['c3',4]]
          >>> l2 = [['c1',1],['c2',2],['c4',4],['c3',3]]
          >>> [k2 for [k2,v2] in l2 if k2 in [k1 for k1,v1 in l1] and v2 < [v1[/color][/color][/color]
          for k1,v1 in l1 if k1 == k2][0]]
          ['c3']

          .... yup :-)


          Dave.
          --

          Comment

          Working...