Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #39211 > unrolled thread
| Started by | anadionisio257@gmail.com |
|---|---|
| First post | 2013-02-19 04:14 -0800 |
| Last post | 2013-02-20 22:47 +1100 |
| Articles | 9 — 6 participants |
Back to article view | Back to comp.lang.python
Data Tree urgent help!!!!!! anadionisio257@gmail.com - 2013-02-19 04:14 -0800
Re: Data Tree urgent help!!!!!! Peter Otten <__peter__@web.de> - 2013-02-19 13:34 +0100
Re: Data Tree urgent help!!!!!! Ana Dionísio <anadionisio257@gmail.com> - 2013-02-19 04:50 -0800
Re: Data Tree urgent help!!!!!! Ana Dionísio <anadionisio257@gmail.com> - 2013-02-19 04:50 -0800
Re: Data Tree urgent help!!!!!! Leo Breebaart <leo@lspace.org> - 2013-02-20 10:18 +0000
Re: Data Tree urgent help!!!!!! Peter Otten <__peter__@web.de> - 2013-02-20 11:46 +0100
Re: Data Tree urgent help!!!!!! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-20 22:01 +1100
Re: Data Tree urgent help!!!!!! Chris Angelico <rosuav@gmail.com> - 2013-02-20 22:16 +1100
Re: Data Tree urgent help!!!!!! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-20 22:47 +1100
| From | anadionisio257@gmail.com |
|---|---|
| Date | 2013-02-19 04:14 -0800 |
| Subject | Data Tree urgent help!!!!!! |
| Message-ID | <4abbe91f-a57e-47de-9cf3-5b9574201c83@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-02-19 13:34 +0100 |
| Message-ID | <mailman.2018.1361277272.2939.python-list@python.org> |
| In reply to | #39211 |
anadionisio257@gmail.com 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)
[toc] | [prev] | [next] | [standalone]
| From | Ana Dionísio <anadionisio257@gmail.com> |
|---|---|
| Date | 2013-02-19 04:50 -0800 |
| Message-ID | <9ffd04db-ef2c-4774-97e9-4e5f0755f68d@googlegroups.com> |
| In reply to | #39212 |
Thank you so much!!!!!!! It works perfectly!!! ;)
[toc] | [prev] | [next] | [standalone]
| From | Ana Dionísio <anadionisio257@gmail.com> |
|---|---|
| Date | 2013-02-19 04:50 -0800 |
| Message-ID | <mailman.2020.1361278253.2939.python-list@python.org> |
| In reply to | #39212 |
Thank you so much!!!!!!! It works perfectly!!! ;)
[toc] | [prev] | [next] | [standalone]
| From | Leo Breebaart <leo@lspace.org> |
|---|---|
| Date | 2013-02-20 10:18 +0000 |
| Message-ID | <aojm6oFu09lU1@mid.individual.net> |
| In reply to | #39212 |
Peter Otten <__peter__@web.de> 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@lspace.org>
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-02-20 11:46 +0100 |
| Message-ID | <mailman.2100.1361357186.2939.python-list@python.org> |
| In reply to | #39334 |
Leo Breebaart wrote:
> Peter Otten <__peter__@web.de> 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...
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-02-20 22:01 +1100 |
| Message-ID | <5124ad18$0$29975$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #39334 |
Leo Breebaart wrote: > Peter Otten <__peter__@web.de> 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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-02-20 22:16 +1100 |
| Message-ID | <mailman.2105.1361359003.2939.python-list@python.org> |
| In reply to | #39342 |
On Wed, Feb 20, 2013 at 10:01 PM, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > Leo Breebaart wrote: > >> Peter Otten <__peter__@web.de> 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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-02-20 22:47 +1100 |
| Message-ID | <5124b7ec$0$29981$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #39346 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web