Data Tree urgent help!!!!!!

Discussion in 'Python' started by anadionisio257@gmail.com, Feb 19, 2013.

  1. Guest

    Hello!
    I have this lists with information and I need to make a "tree" by associating the information inside the lists. For example:

    l1 = [apple, pear]
    l2 = [dog, cat]
    l3 = [fork, spoon]

    And I need to make something like this:

    l4 = [apple, dog, fork]
    l5 = [apple, dog, spoon]
    l6= [apple, cat, fork]
    l7 = [apple, cat, spoon]
    l8 = [pear, dog, fork]
    etc...

    How can I do this? I could use "for" cycles and "if...else" but with larger lists it gets complicated

    Is there some simple solution that I can use?

    I hope you could help me
     
    , Feb 19, 2013
    #1
    1. Advertising

  2. Peter Otten Guest

    wrote:

    > Hello!
    > I have this lists with information and I need to make a "tree" by
    > associating the information inside the lists. For example:
    >
    > l1 = [apple, pear]
    > l2 = [dog, cat]
    > l3 = [fork, spoon]
    >
    > And I need to make something like this:
    >
    > l4 = [apple, dog, fork]
    > l5 = [apple, dog, spoon]
    > l6= [apple, cat, fork]
    > l7 = [apple, cat, spoon]
    > l8 = [pear, dog, fork]
    > etc...
    >
    > How can I do this? I could use "for" cycles and "if...else" but with
    > larger lists it gets complicated
    >
    > Is there some simple solution that I can use?


    Try itertools.product():

    >>> class Name(str):

    .... def __repr__(self):
    .... return self
    ....
    >>> apple, pear, dog, cat, fork, spoon = map(Name, "apple pear dog cat fork

    spoon".split())
    >>> fruit = [apple, pear]
    >>> pets = [dog, cat]
    >>> cutlery = [fork, spoon]
    >>> from itertools import product
    >>> for item in product(fruit, pets, cutlery):

    .... print item
    ....
    (apple, dog, fork)
    (apple, dog, spoon)
    (apple, cat, fork)
    (apple, cat, spoon)
    (pear, dog, fork)
    (pear, dog, spoon)
    (pear, cat, fork)
    (pear, cat, spoon)
     
    Peter Otten, Feb 19, 2013
    #2
    1. Advertising

  3. Thank you so much!!!!!!! It works perfectly!!!

    ;)
     
    Ana Dionísio, Feb 19, 2013
    #3
  4. Thank you so much!!!!!!! It works perfectly!!!

    ;)
     
    Ana Dionísio, Feb 19, 2013
    #4
  5. Peter Otten <> writes:

    > >>> class Name(str):

    > ... def __repr__(self):
    > ... return self
    > ...
    > >>> apple, pear, dog, cat, fork, spoon = map(Name, "apple pear dog cat fork spoon".split())


    Is there any reason why you introduced the Name class? In Python
    2.7 this works equally well if I just do:

    >>> apple, pear, dog, cat, fork, spoon = map(str, "apple pear dog cat fork spoon".split())


    So I was wondering why you used Name.

    --
    Leo Breebaart <>
     
    Leo Breebaart, Feb 20, 2013
    #5
  6. Peter Otten Guest

    Leo Breebaart wrote:

    > Peter Otten <> writes:
    >
    >> >>> class Name(str):

    >> ... def __repr__(self):
    >> ... return self
    >> ...
    >> >>> apple, pear, dog, cat, fork, spoon = map(Name, "apple pear dog cat
    >> >>> fork spoon".split())

    >
    > Is there any reason why you introduced the Name class? In Python
    > 2.7 this works equally well if I just do:
    >
    >>>> apple, pear, dog, cat, fork, spoon = map(str, "apple pear dog cat fork
    >>>> spoon".split())

    >
    > So I was wondering why you used Name.


    It was more for fun than profit ;) The OP gave

    [apple, dog, fork]

    in his examples, and the "normal" no-nonsense approach using a list of
    strings would produce

    ['apple', 'dog', 'fork']

    I was tempted to carry this even further with

    >>> class Name(str):

    .... def __repr__(self): return self
    ....
    >>> class Namespace(dict):

    .... def __missing__(self, key):
    .... self[key] = result = Name(key)
    .... return result
    ....
    >>> fruit, pets, cutlery = eval("[apple, pear], [dog, cat], [fork, spoon]",

    Namespace())
    >>> fruit

    [apple, pear]

    but resisted until now...
     
    Peter Otten, Feb 20, 2013
    #6
  7. Leo Breebaart wrote:

    > Peter Otten <> writes:
    >
    >> >>> class Name(str):

    >> ... def __repr__(self):
    >> ... return self
    >> ...
    >> >>> apple, pear, dog, cat, fork, spoon = map(Name, "apple pear dog cat
    >> >>> fork spoon".split())

    >
    > Is there any reason why you introduced the Name class? In Python
    > 2.7 this works equally well if I just do:
    >
    >>>> apple, pear, dog, cat, fork, spoon = map(str, "apple pear dog cat fork
    >>>> spoon".split())

    >
    > So I was wondering why you used Name.


    I'm wondering why you used map.


    apple, pear, dog, cat, fork, spoon = "apple pear dog cat fork spoon".split()


    :)


    --
    Steven
     
    Steven D'Aprano, Feb 20, 2013
    #7
  8. On Wed, Feb 20, 2013 at 10:01 PM, Steven D'Aprano
    <> wrote:
    > Leo Breebaart wrote:
    >
    >> Peter Otten <> writes:
    >>
    >>> >>> class Name(str):
    >>> ... def __repr__(self):
    >>> ... return self
    >>> ...
    >>> >>> apple, pear, dog, cat, fork, spoon = map(Name, "apple pear dog cat
    >>> >>> fork spoon".split())

    >>
    >> Is there any reason why you introduced the Name class? In Python
    >> 2.7 this works equally well if I just do:
    >>
    >>>>> apple, pear, dog, cat, fork, spoon = map(str, "apple pear dog cat fork
    >>>>> spoon".split())

    >>
    >> So I was wondering why you used Name.

    >
    > I'm wondering why you used map.
    >
    >
    > apple, pear, dog, cat, fork, spoon = "apple pear dog cat fork spoon".split()


    Why, in case someone monkeypatched split() to return something other
    than strings, of course!

    Sorry, I've been learning Ruby this week, and I fear it may be
    damaging to my mind...

    ChrisA
     
    Chris Angelico, Feb 20, 2013
    #8
  9. Chris Angelico wrote:

    >> I'm wondering why you used map.
    >>
    >>
    >> apple, pear, dog, cat, fork, spoon = "apple pear dog cat fork
    >> spoon".split()

    >
    > Why, in case someone monkeypatched split() to return something other
    > than strings, of course!
    >
    > Sorry, I've been learning Ruby this week, and I fear it may be
    > damaging to my mind...


    :)


    You cannot monkey-patch builtins themselves in Python. Since the string is a
    literal, it is guaranteed to be a builtin string, and the split method is
    guaranteed to be the builtin str.split method.

    This is, however, not true in the earlier example of map(str, ...) since
    either of map or str could be shadowed by a global function of the same
    name, or even monkey-patched across the whole Python environment:

    py> import __builtin__ # Python 2.7
    py> __builtin__.str = lambda x: len(x)
    py> __builtin__.map = lambda func, items: [10000+func(x) for x in items]
    py> map(str, "apple, pear, dog".split())
    [10006, 10005, 10003]


    So unlike Ruby, Python restricts what you can monkey-patch, and discourages
    you from doing it even when you can. Oh, and I can reverse my monkey-patch
    easily:

    py> reload(__builtin__)
    <module '__builtin__' (built-in)>
    py> map(str, "apple, pear, dog".split())
    ['apple,', 'pear,', 'dog']



    --
    Steven
     
    Steven D'Aprano, Feb 20, 2013
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jawwad
    Replies:
    0
    Views:
    340
    jawwad
    Jan 28, 2004
  2. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,136
  3. sharan
    Replies:
    4
    Views:
    693
    CBFalconer
    Oct 30, 2007
  4. sharan
    Replies:
    2
    Views:
    835
    SM Ryan
    Oct 31, 2007
  5. sharan
    Replies:
    1
    Views:
    693
    CBFalconer
    Oct 30, 2007
Loading...

Share This Page