Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #96985 > unrolled thread
| Started by | Python_Teacher <ljfc2000@yahoo.com> |
|---|---|
| First post | 2015-09-22 11:43 -0700 |
| Last post | 2015-09-23 08:26 +0200 |
| Articles | 14 — 13 participants |
Back to article view | Back to comp.lang.python
A little test for you Guys😜 Python_Teacher <ljfc2000@yahoo.com> - 2015-09-22 11:43 -0700
Re: A little test for you Guys😜 Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-22 13:14 -0600
Re: A little test for you Guys😜 "Sven R. Kunze" <srkunze@mail.de> - 2015-09-22 21:42 +0200
Re: A little test for you Guys😜 Akira Li <4kir4.1i@gmail.com> - 2015-09-22 22:48 +0300
Re: A little test for you Guys😜 James Harris <james.harris.1@gmail.com> - 2015-09-22 13:28 -0700
Re: A little test for you Guys😜 sohcahtoa82@gmail.com - 2015-09-22 14:18 -0700
Re: A little test for you Guys😜 "James Harris" <james.harris.1@gmail.com> - 2015-09-22 22:31 +0100
Re: A little test for you Guys😜 Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-22 15:33 -0600
Re: A little test for you Guys😜 Lj Fc <ljfc2000@yahoo.com> - 2015-09-22 15:21 -0700
Re: A little test for you Guys😜 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-23 00:32 +0100
Re: A little test for you Guys😜 Chris Angelico <rosuav@gmail.com> - 2015-09-23 09:45 +1000
Re: A little test for you Guys😜 MRAB <python@mrabarnett.plus.com> - 2015-09-23 00:56 +0100
Re: A little test for you Guys😜 alister <alister.nospam.ware@ntlworld.com> - 2015-09-23 18:06 +0000
Re: A little test for you Guys😜 Christian Gollwitzer <auriocus@gmx.de> - 2015-09-23 08:26 +0200
| From | Python_Teacher <ljfc2000@yahoo.com> |
|---|---|
| Date | 2015-09-22 11:43 -0700 |
| Subject | A little test for you Guys😜 |
| Message-ID | <78fc66f6-04f9-4b84-8410-2e74fb75fbb4@googlegroups.com> |
you have 10 minutes😂 Good luck!!
1. What is PEP8 ?
2. What are the different ways to distribute some python source code ?
2 Lists
Let's define the function plural :
def plural(words):
plurals = []
for word in words:
plurals.append(word + 's')
return plurals
for word in plural(['cabagge','owl','toy']):
print word
Question : How could the code of the function plural be optimised?
3 Dictionaries
Here are two dictionnaries :
input = {
'foo1': 'bar1',
'chose': 'truc',
'foo2': 'bar2',
}
output = {
'bar1': 'foo1',
'truc': 'chose',
'bar2': 'foo2'
}
Question : Propose a function that returns output when you provide input ?
4 Iterators
Let's consider this program :
def program_1():
yield 1
yield 2
yield 3
g = program_1()
a = list(g)
b = list(g)
c = g()
Question : At the end of the program,
1. What is the type of g ?
2. What is the value of a ?
3. What is the value of b ?
4. What is the value of c ?
5 Decorators
Let's consider now :
def str2print(f):
def str2print_wrap(*args, **kwargs):
"""wrapper"""
s = f(*args, **kwargs)
print s
return str2print_wrap
def hello(s):
""" Return "Hello $s" """
return "%s %s" % ("Hello", s)
Questions :
1. Decorate the method 'hello' with 'str2printf' and write the corresponding code.
2. What is the effect of the decorator on a call to the new method 'hello' ?
3. What is the return value of hello.__doc__
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-09-22 13:14 -0600 |
| Message-ID | <mailman.67.1442949299.28679.python-list@python.org> |
| In reply to | #96985 |
On Tue, Sep 22, 2015 at 12:43 PM, Python_Teacher via Python-list
<python-list@python.org> wrote:
> you have 10 minutes😂 Good luck!!
Sorry, I'm more interested in critiquing the questions than answering them.
> Let's define the function plural :
>
> def plural(words):
> plurals = []
> for word in words:
> plurals.append(word + 's')
> return plurals
>
> for word in plural(['cabagge','owl','toy']):
> print word
>
> Question : How could the code of the function plural be optimised?
Optimized in what way? To be faster? Use less memory? Be more readable?
> 3 Dictionaries
>
> Here are two dictionnaries :
>
> input = {
> 'foo1': 'bar1',
> 'chose': 'truc',
> 'foo2': 'bar2',
> }
> output = {
> 'bar1': 'foo1',
> 'truc': 'chose',
> 'bar2': 'foo2'
> }
>
> Question : Propose a function that returns output when you provide input ?
def f(maybe_input):
if maybe_input == input:
return output
I don't think I really understand what it is that you're asking for here.
> 3. What is the return value of hello.__doc__
hello.__doc__ isn't a function, so it doesn't have a return value. I
think you mean to ask what the expression evaluates to.
[toc] | [prev] | [next] | [standalone]
| From | "Sven R. Kunze" <srkunze@mail.de> |
|---|---|
| Date | 2015-09-22 21:42 +0200 |
| Message-ID | <mailman.68.1442950966.28679.python-list@python.org> |
| In reply to | #96985 |
Hmm, why not. :D
On 22.09.2015 20:43, Python_Teacher via Python-list wrote:
> you have 10 minutes😂 Good luck!!
>
>
> 1. What is PEP8 ?
A PEP.
> 2. What are the different ways to distribute some python source code ?
unison, rsync, scp, ftp, sftp, samba, http, https, mail, git, ....
> 2 Lists
>
> Let's define the function plural :
>
> def plural(words):
> plurals = []
> for word in words:
> plurals.append(word + 's')
> return plurals
>
> for word in plural(['cabagge','owl','toy']):
> print word
>
> Question : How could the code of the function plural be optimised?
Don't optimized until you need to. So, we leave it as is. ;)
> 3 Dictionaries
>
> Here are two dictionnaries :
>
> input = {
> 'foo1': 'bar1',
> 'chose': 'truc',
> 'foo2': 'bar2',
> }
> output = {
> 'bar1': 'foo1',
> 'truc': 'chose',
> 'bar2': 'foo2'
> }
>
> Question : Propose a function that returns output when you provide input ?
# :-P
def function(input):
return output
> 4 Iterators
>
> Let's consider this program :
>
> def program_1():
> yield 1
> yield 2
> yield 3
>
> g = program_1()
> a = list(g)
> b = list(g)
> c = g()
>
> Question : At the end of the program,
>
> 1. What is the type of g ?
> 2. What is the value of a ?
> 3. What is the value of b ?
> 4. What is the value of c ?
The program ends with a traceback. So, my variables are all gone. :(
> 5 Decorators
>
> Let's consider now :
>
> def str2print(f):
> def str2print_wrap(*args, **kwargs):
> """wrapper"""
> s = f(*args, **kwargs)
> print s
> return str2print_wrap
>
> def hello(s):
> """ Return "Hello $s" """
> return "%s %s" % ("Hello", s)
>
> Questions :
>
> 1. Decorate the method 'hello' with 'str2printf' and write the corresponding code.
@str2print
def hello(s):
""" Return "Hello $s" but actually returns None """
return "%s %s" % ("Hello", s)
> 2. What is the effect of the decorator on a call to the new method 'hello' ?
It prints "Hello {s}" and returns None.
> 3. What is the return value of hello.__doc__
'wrapper'
Best,
Sven
[toc] | [prev] | [next] | [standalone]
| From | Akira Li <4kir4.1i@gmail.com> |
|---|---|
| Date | 2015-09-22 22:48 +0300 |
| Message-ID | <mailman.69.1442951315.28679.python-list@python.org> |
| In reply to | #96985 |
Python_Teacher via Python-list <python-list@python.org> writes:
...
> Let's define the function plural :
>
> def plural(words):
> plurals = []
> for word in words:
> plurals.append(word + 's')
> return plurals
>
> for word in plural(['cabagge','owl','toy']):
> print word
plural() should accept a single word. To handle list of words, call
map(plural, words)
...
> def str2print(f):
> def str2print_wrap(*args, **kwargs):
> """wrapper"""
> s = f(*args, **kwargs)
> print s
> return str2print_wrap
>
> def hello(s):
> """ Return "Hello $s" """
> return "%s %s" % ("Hello", s)
Use functools.wraps() to preserve the function info for introspection:
import functools
def prints_result(function):
@functools.wraps(function)
def wrapper(*args, **kwargs):
result = function(*args, **kwargs)
print(result)
return result #XXX return
return wrapper
@prints_result
def hello(...):
pass
[toc] | [prev] | [next] | [standalone]
| From | James Harris <james.harris.1@gmail.com> |
|---|---|
| Date | 2015-09-22 13:28 -0700 |
| Message-ID | <8e38d7e1-cda6-490f-8f79-99b23ff5706b@googlegroups.com> |
| In reply to | #96985 |
On Tuesday, September 22, 2015 at 7:45:00 PM UTC+1, Lj Fc wrote:
> you have 10 minutes😂 Good luck!!
A good set of questions, IMO. Am answering as someone coming back to Python after a few years.
> 1. What is PEP8 ?
Coding guidelines, I think.
> 2. What are the different ways to distribute some python source code ?
I don't know what that's getting at as it specifically mentions source code apart from tar/gzip or zip. Maybe git or other scm?
> 2 Lists
>
> Let's define the function plural :
>
> def plural(words):
> plurals = []
> for word in words:
> plurals.append(word + 's')
> return plurals
>
> for word in plural(['cabagge','owl','toy']):
> print word
>
> Question : How could the code of the function plural be optimised?
I would go for
[word + 's' for word in words]
> 3 Dictionaries
>
> Here are two dictionnaries :
>
> input = {
> 'foo1': 'bar1',
> 'chose': 'truc',
> 'foo2': 'bar2',
> }
> output = {
> 'bar1': 'foo1',
> 'truc': 'chose',
> 'bar2': 'foo2'
> }
>
> Question : Propose a function that returns output when you provide input ?
def f(input):
output = {}
for k,v in input.items():
output[v] = k
return output
> 4 Iterators
>
> Let's consider this program :
>
> def program_1():
> yield 1
> yield 2
> yield 3
>
> g = program_1()
> a = list(g)
> b = list(g)
> c = g()
>
> Question : At the end of the program,
>
> 1. What is the type of g ?
> 2. What is the value of a ?
> 3. What is the value of b ?
> 4. What is the value of c ?
Good one. I checked this and only got 1 and 2 right.
> 5 Decorators
No idea!
James
[toc] | [prev] | [next] | [standalone]
| From | sohcahtoa82@gmail.com |
|---|---|
| Date | 2015-09-22 14:18 -0700 |
| Message-ID | <5218c7f9-74ea-4ca0-abd1-46a9bcd3dc2a@googlegroups.com> |
| In reply to | #96985 |
On Tuesday, September 22, 2015 at 11:45:00 AM UTC-7, Lj Fc wrote:
> you have 10 minutes😂 Good luck!!
>
>
> 1. What is PEP8 ?
>
> 2. What are the different ways to distribute some python source code ?
>
> 2 Lists
>
> Let's define the function plural :
>
> def plural(words):
> plurals = []
> for word in words:
> plurals.append(word + 's')
> return plurals
>
> for word in plural(['cabagge','owl','toy']):
> print word
>
> Question : How could the code of the function plural be optimised?
>
> 3 Dictionaries
>
> Here are two dictionnaries :
>
> input = {
> 'foo1': 'bar1',
> 'chose': 'truc',
> 'foo2': 'bar2',
> }
> output = {
> 'bar1': 'foo1',
> 'truc': 'chose',
> 'bar2': 'foo2'
> }
>
> Question : Propose a function that returns output when you provide input ?
>
> 4 Iterators
>
> Let's consider this program :
>
> def program_1():
> yield 1
> yield 2
> yield 3
>
> g = program_1()
> a = list(g)
> b = list(g)
> c = g()
>
> Question : At the end of the program,
>
> 1. What is the type of g ?
> 2. What is the value of a ?
> 3. What is the value of b ?
> 4. What is the value of c ?
>
> 5 Decorators
>
> Let's consider now :
>
> def str2print(f):
> def str2print_wrap(*args, **kwargs):
> """wrapper"""
> s = f(*args, **kwargs)
> print s
> return str2print_wrap
>
> def hello(s):
> """ Return "Hello $s" """
> return "%s %s" % ("Hello", s)
>
> Questions :
>
> 1. Decorate the method 'hello' with 'str2printf' and write the corresponding code.
> 2. What is the effect of the decorator on a call to the new method 'hello' ?
> 3. What is the return value of hello.__doc__
Pretty sure this guy is asking us to do his homework. :-P
[toc] | [prev] | [next] | [standalone]
| From | "James Harris" <james.harris.1@gmail.com> |
|---|---|
| Date | 2015-09-22 22:31 +0100 |
| Message-ID | <mtsh8r$p77$1@dont-email.me> |
| In reply to | #96994 |
<sohcahtoa82@gmail.com> wrote in message news:5218c7f9-74ea-4ca0-abd1-46a9bcd3dc2a@googlegroups.com... ... > Pretty sure this guy is asking us to do his homework. :-P Maybe (and I hope not) but asking what PEP8 is could be easily found on the internet and asking what the values would be at the end of the program in question 4 could be easily found by trying it. James
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-09-22 15:33 -0600 |
| Message-ID | <mailman.71.1442957624.28679.python-list@python.org> |
| In reply to | #96994 |
On Tue, Sep 22, 2015 at 3:18 PM, <sohcahtoa82@gmail.com> wrote: > On Tuesday, September 22, 2015 at 11:45:00 AM UTC-7, Lj Fc wrote: >> you have 10 minutes😂 Good luck!! > > Pretty sure this guy is asking us to do his homework. :-P Well, looks like it was due 2 hours ago.
[toc] | [prev] | [next] | [standalone]
| From | Lj Fc <ljfc2000@yahoo.com> |
|---|---|
| Date | 2015-09-22 15:21 -0700 |
| Message-ID | <e455eadd-c3af-4216-9c13-b87f110d17ec@googlegroups.com> |
| In reply to | #96994 |
On Tuesday, September 22, 2015 at 11:19:00 PM UTC+2, sohca...@gmail.com wrote:
> On Tuesday, September 22, 2015 at 11:45:00 AM UTC-7, Lj Fc wrote:
> > you have 10 minutes😂 Good luck!!
> >
> >
> > 1. What is PEP8 ?
> >
> > 2. What are the different ways to distribute some python source code ?
> >
> > 2 Lists
> >
> > Let's define the function plural :
> >
> > def plural(words):
> > plurals = []
> > for word in words:
> > plurals.append(word + 's')
> > return plurals
> >
> > for word in plural(['cabagge','owl','toy']):
> > print word
> >
> > Question : How could the code of the function plural be optimised?
> >
> > 3 Dictionaries
> >
> > Here are two dictionnaries :
> >
> > input = {
> > 'foo1': 'bar1',
> > 'chose': 'truc',
> > 'foo2': 'bar2',
> > }
> > output = {
> > 'bar1': 'foo1',
> > 'truc': 'chose',
> > 'bar2': 'foo2'
> > }
> >
> > Question : Propose a function that returns output when you provide input ?
> >
> > 4 Iterators
> >
> > Let's consider this program :
> >
> > def program_1():
> > yield 1
> > yield 2
> > yield 3
> >
> > g = program_1()
> > a = list(g)
> > b = list(g)
> > c = g()
> >
> > Question : At the end of the program,
> >
> > 1. What is the type of g ?
> > 2. What is the value of a ?
> > 3. What is the value of b ?
> > 4. What is the value of c ?
> >
> > 5 Decorators
> >
> > Let's consider now :
> >
> > def str2print(f):
> > def str2print_wrap(*args, **kwargs):
> > """wrapper"""
> > s = f(*args, **kwargs)
> > print s
> > return str2print_wrap
> >
> > def hello(s):
> > """ Return "Hello $s" """
> > return "%s %s" % ("Hello", s)
> >
> > Questions :
> >
> > 1. Decorate the method 'hello' with 'str2printf' and write the corresponding code.
> > 2. What is the effect of the decorator on a call to the new method 'hello' ?
> > 3. What is the return value of hello.__doc__
>
> Pretty sure this guy is asking us to do his homework. :-P
See Not that Easy Dude...Simple Questions are sometimes the Toughest!! KISS😜
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-09-23 00:32 +0100 |
| Message-ID | <mailman.80.1442964771.28679.python-list@python.org> |
| In reply to | #96985 |
On 22/09/2015 19:43, Python_Teacher via Python-list wrote:
> you have 10 minutes😂 Good luck!!
>
>
> 1. What is PEP8 ?
It's the one between PEP7 and PEP9.
>
> 2. What are the different ways to distribute some python source code ?
Write on sheet of paper, fold into paper dart, throw from window.
>
> 2 Lists
Tut, tut, tut.
>
> Let's define the function plural :
>
> def plural(words):
> plurals = []
> for word in words:
> plurals.append(word + 's')
> return plurals
>
> for word in plural(['cabagge','owl','toy']):
> print word
>
> Question : How could the code of the function plural be optimised?
It is all ready optimised for programmer time so don't bother with it
unless there are unforeseen bugs.
>
> 3 Dictionaries
>
> Here are two dictionnaries :
>
> input = {
> 'foo1': 'bar1',
> 'chose': 'truc',
> 'foo2': 'bar2',
> }
> output = {
> 'bar1': 'foo1',
> 'truc': 'chose',
> 'bar2': 'foo2'
> }
>
> Question : Propose a function that returns output when you provide input ?
def function():
return input("Who cares?")
>
> 4 Iterators
>
> Let's consider this program :
>
> def program_1():
> yield 1
> yield 2
> yield 3
>
> g = program_1()
> a = list(g)
> b = list(g)
> c = g()
>
> Question : At the end of the program,
>
> 1. What is the type of g ?
> 2. What is the value of a ?
> 3. What is the value of b ?
> 4. What is the value of c ?
How the hell would I know?
>
> 5 Decorators
>
> Let's consider now :
>
> def str2print(f):
> def str2print_wrap(*args, **kwargs):
> """wrapper"""
> s = f(*args, **kwargs)
> print s
> return str2print_wrap
>
> def hello(s):
> """ Return "Hello $s" """
> return "%s %s" % ("Hello", s)
>
> Questions :
>
> 1. Decorate the method 'hello' with 'str2printf' and write the corresponding code.
> 2. What is the effect of the decorator on a call to the new method 'hello' ?
> 3. What is the return value of hello.__doc__
>
Can't afford decorators, they cost an arm and a leg in the UK.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-09-23 09:45 +1000 |
| Message-ID | <mailman.82.1442965545.28679.python-list@python.org> |
| In reply to | #96985 |
On Wed, Sep 23, 2015 at 9:32 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: >> >> 1. What is the type of g ? >> 2. What is the value of a ? >> 3. What is the value of b ? >> 4. What is the value of c ? > > > How the hell would I know? Basic schooling, Mark, basic schooling. 1. Newton meters squared per kilogram squared. 2. One ampere is equal to one coulomb per second. 3. Quite considerable; given the amount of traffic that /b/ sees, I would expect it to sell for a high price. 4. 3e8 meters per second. Bless me, what DO they teach them at these schools... ChrisA
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2015-09-23 00:56 +0100 |
| Message-ID | <mailman.85.1442966183.28679.python-list@python.org> |
| In reply to | #96985 |
On 2015-09-23 00:32, Mark Lawrence wrote:
> On 22/09/2015 19:43, Python_Teacher via Python-list wrote:
>> you have 10 minutes😂 Good luck!!
>>
>>
>> 1. What is PEP8 ?
>
> It's the one between PEP7 and PEP9.
>
>>
>> 2. What are the different ways to distribute some python source code ?
>
> Write on sheet of paper, fold into paper dart, throw from window.
>
>>
>> 2 Lists
>
> Tut, tut, tut.
>
>>
>> Let's define the function plural :
>>
>> def plural(words):
>> plurals = []
>> for word in words:
>> plurals.append(word + 's')
>> return plurals
>>
>> for word in plural(['cabagge','owl','toy']):
>> print word
>>
>> Question : How could the code of the function plural be optimised?
>
> It is all ready optimised for programmer time so don't bother with it
> unless there are unforeseen bugs.
>
>>
>> 3 Dictionaries
>>
>> Here are two dictionnaries :
>>
>> input = {
>> 'foo1': 'bar1',
>> 'chose': 'truc',
>> 'foo2': 'bar2',
>> }
>> output = {
>> 'bar1': 'foo1',
>> 'truc': 'chose',
>> 'bar2': 'foo2'
>> }
>>
>> Question : Propose a function that returns output when you provide input ?
>
> def function():
> return input("Who cares?")
>
You have a couple of problems:
1. 'input' is already bound to a dict.
2. From question 2, it's clear that Python 2 is being used, so you
should be using 'raw_input' instead.
[snip]
[toc] | [prev] | [next] | [standalone]
| From | alister <alister.nospam.ware@ntlworld.com> |
|---|---|
| Date | 2015-09-23 18:06 +0000 |
| Message-ID | <mtupmr$t32$1@speranza.aioe.org> |
| In reply to | #97012 |
On Wed, 23 Sep 2015 00:56:19 +0100, MRAB wrote:
> On 2015-09-23 00:32, Mark Lawrence wrote:
>> On 22/09/2015 19:43, Python_Teacher via Python-list wrote:
>>> you have 10 minutes😂 Good luck!!
>>>
>>>
>>> 1. What is PEP8 ?
>>
>> It's the one between PEP7 and PEP9.
>>
>>
>>> 2. What are the different ways to distribute some python source code ?
>>
>> Write on sheet of paper, fold into paper dart, throw from window.
>>
>>
>>> 2 Lists
>>
>> Tut, tut, tut.
>>
>>
>>> Let's define the function plural :
>>>
>>> def plural(words):
>>> plurals = []
>>> for word in words:
>>> plurals.append(word + 's')
>>> return plurals
>>>
>>> for word in plural(['cabagge','owl','toy']):
>>> print word
>>>
>>> Question : How could the code of the function plural be optimised?
>>
>> It is all ready optimised for programmer time so don't bother with it
>> unless there are unforeseen bugs.
>>
>>
>>> 3 Dictionaries
>>>
>>> Here are two dictionnaries :
>>>
>>> input = {
>>> 'foo1': 'bar1', 'chose': 'truc', 'foo2': 'bar2',
>>> }
>>> output = {
>>> 'bar1': 'foo1', 'truc': 'chose', 'bar2': 'foo2'
>>> }
>>>
>>> Question : Propose a function that returns output when you provide
>>> input ?
>>
>> def function():
>> return input("Who cares?")
>>
> You have a couple of problems:
>
> 1. 'input' is already bound to a dict.
>
> 2. From question 2, it's clear that Python 2 is being used, so you
> should be using 'raw_input' instead.
>
> [snip]
the question also shadows a builtin :-)
--
This is for all ill-treated fellows
Unborn and unbegot,
For them to read when they're in trouble
And I am not.
-- A. E. Housman
[toc] | [prev] | [next] | [standalone]
| From | Christian Gollwitzer <auriocus@gmx.de> |
|---|---|
| Date | 2015-09-23 08:26 +0200 |
| Message-ID | <mttgij$3rp$1@dont-email.me> |
| In reply to | #96985 |
You've got a lot of sensible answers, but let me add to this one:
Am 22.09.15 um 20:43 schrieb Python_Teacher:
> input = {
> 'foo1': 'bar1',
> 'chose': 'truc',
> 'foo2': 'bar2',
> }
> output = {
> 'bar1': 'foo1',
> 'truc': 'chose',
> 'bar2': 'foo2'
> }
This one can be done as a dict comprehension:
>>> p = {'foo1': 'bar1', 'foo2': 'bar2', 'chose': 'truc'}
>>> { v:k for k,v in p.items()}
{'bar1': 'foo1', 'truc': 'chose', 'bar2': 'foo2'}
list/dict comprehension is actually one of the features in Python that I
like most, because it can greatly ease such transformations.
....and, as others said, these questions are lightyears apart from
showing that somebody understands Python programming. They can be solved
by trying it or googling, and that is what a real programmer would
actually do if he is stuck.
Christian
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web