A little test for you Guys😜

Christian Gollwitzer auriocus at gmx.de
Wed Sep 23 08:26:15 CEST 2015


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


More information about the Python-list mailing list