Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is there a nicer way to do this?

Reply
Thread Tools

Is there a nicer way to do this?

 
 
Stefan Arentz
Guest
Posts: n/a
 
      10-04-2007

Is there a better way to do the following?

attributes = ['foo', 'bar']

attributeNames = {}
n = 1
for attribute in attributes:
attributeNames["AttributeName.%d" % n] = attribute
n = n + 1

It works, but I am wondering if there is a more pythonic way to
do this.

S.
 
Reply With Quote
 
 
 
 
Amaury Forgeot d'Arc
Guest
Posts: n/a
 
      10-04-2007
Hello,
Stefan Arentz a écrit :
> Is there a better way to do the following?
>
> attributes = ['foo', 'bar']
>
> attributeNames = {}
> n = 1
> for attribute in attributes:
> attributeNames["AttributeName.%d" % n] = attribute
> n = n + 1
>
> It works, but I am wondering if there is a more pythonic way to
> do this.
>
> S.


You could use enumerate() to number the items (careful it starts with 0):

attributes = ['foo', 'bar']
attributeNames = {}
for n, attribute in enumerate(attributes):
attributeNames["AttributeName.%d" % (n+1)] = attribute


Then use a generator expression to feed the dict:

attributes = ['foo', 'bar']
attributeNames = dict(("AttributeName.%d" % (n+1), attribute)
for n, attribute in enumerate(attributes))

Hope this helps,

--
Amaury
 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      10-04-2007
> attributes = ['foo', 'bar']
>
> attributeNames = {}
> n = 1
> for attribute in attributes:
> attributeNames["AttributeName.%d" % n] = attribute
> n = n + 1
>
> It works, but I am wondering if there is a more pythonic way to
> do this.


"Better" may be subjective, but you could do something like

attributes = ['foo', 'bar']
attributeNames = dict(
("AttributeName.%d" % (i+1), attrib)
for i, attrib
in enumerate(attributes)
)

HTH,

-tkc





 
Reply With Quote
 
Casey
Guest
Posts: n/a
 
      10-04-2007
Not sure if this is really better or even more pythonic, but if you
like one-liners that exercise the language:

attributeNames = dict( [("AttributeName.%d" % (n+1), attribute) for
n,attribute in enumerate(attributes)] )

What this does is create a list (using a list comprehension and the
enumerate function) of ("AttributeName.x", attribute) tuples which is
then be used to initialize a dictionary.

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      10-04-2007

"Stefan Arentz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
|
| Is there a better way to do the following?
|
| attributes = ['foo', 'bar']
|
| attributeNames = {}
| n = 1
| for attribute in attributes:
| attributeNames["AttributeName.%d" % n] = attribute
| n = n + 1
|
| It works, but I am wondering if there is a more pythonic way to
| do this.

Perhaps better is using enumerate:

>>> attributeNames = {}
>>> for n,attribute in enumerate(['foo', 'bar']):

attributeNames["AttributeName.%d" % (n+1)] = attribute

>>> attributeNames

{'AttributeName.1': 'foo', 'AttributeName.2': 'bar'}

However, mapping indexes to names should be more useful:

>>> aNames = dict(enumerate(['foo', 'bar']))
>>> aNames

{0: 'foo', 1: 'bar'}


Terry Jan Reedy




 
Reply With Quote
 
Casey
Guest
Posts: n/a
 
      10-04-2007
On Oct 4, 5:42 pm, Casey <(E-Mail Removed)> wrote:
> Not sure if this is really better or even more pythonic, but if you
> like one-liners that exercise the language:


Hmm, I guess it WAS more pythonic, since three of us gave essentially
identical responses in a 10-minute period. That being said, I'd
recommend a good comment before the one-liner describing what the
output dictionary is going to look like. Anyone who reads your code
(including yourself, two weeks later) will thank you!

 
Reply With Quote
 
Paul Hankin
Guest
Posts: n/a
 
      10-04-2007
On Oct 4, 10:53 pm, "Terry Reedy" <(E-Mail Removed)> wrote:
> However, mapping indexes to names should be more useful:
>
> >>> aNames = dict(enumerate(['foo', 'bar']))
> >>> aNames

>
> {0: 'foo', 1: 'bar'}


If you are right that a map from indices to name is best, there's no
need for a dict: the original list already provides such a mapping.

--
Paul Hankin

 
Reply With Quote
 
Victor B. Gonzalez
Guest
Posts: n/a
 
      10-04-2007
On Thursday 04 October 2007 5:07:51 pm Stefan Arentz wrote:
> Is there a better way to do the following?
>
> attributes = ['foo', 'bar']
>
> attributeNames = {}
> n = 1
> for attribute in attributes:
> attributeNames["AttributeName.%d" % n] = attribute
> n = n + 1
>
> It works, but I am wondering if there is a more pythonic way to
> do this.
>
> S.


just curious. why are you bothering in creating a dictionary? why not just
iterate over attributes? why duplicate it and make it bigger? I personally
think the dictionary is unnecessary but I may be wrong.

anyhow, I keep getting "SyntaxError: Non-ASCII character '\xc2'..." on line 5.
anyone know what this is? I couldn't run the script but from looking at it,
it appears you're making some pointless keys when indexes may be better.

--
Best Regards
Victor B. Gonzalez

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQBHBWaVIPvSgiLGOqARAr2yAJ98q55LiVQdHMGNdhYi4w aRdcsaSQCfXhDs
1fu16P1RZOHspfXsqzqtceE=
=DIPF
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      10-05-2007

"Paul Hankin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
| On Oct 4, 10:53 pm, "Terry Reedy" <(E-Mail Removed)> wrote:
| > However, mapping indexes to names should be more useful:
| >
| > >>> aNames = dict(enumerate(['foo', 'bar']))
| > >>> aNames
| >
| > {0: 'foo', 1: 'bar'}
|
| If you are right that a map from indices to name is best, there's no
| need for a dict: the original list already provides such a mapping.

Right. The dict should be reversed, mapping names to indexes, if there
were a use.



 
Reply With Quote
 
Stefan Arentz
Guest
Posts: n/a
 
      10-05-2007
Stefan Arentz <(E-Mail Removed)> writes:

> Is there a better way to do the following?
>
> attributes = ['foo', 'bar']
>
> attributeNames = {}
> n = 1
> for attribute in attributes:
> attributeNames["AttributeName.%d" % n] = attribute
> n = n + 1
>
> It works, but I am wondering if there is a more pythonic way to
> do this.


Thank you all. The trick was enumerate(), which I did not know yet

S.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
nicer way to remove prefix of a string if it exists News123 Python 3 07-14-2010 01:40 PM
is there an nicer way for this expression? Remco Hh Ruby 17 11-21-2007 05:04 PM
nicer way to convert array of int to array of string S Kanakakorn Ruby 3 02-09-2007 08:28 PM
Nicer way of strip and replace? Markus Rosenstihl Python 6 10-12-2005 09:09 AM
Newbie Q - Nicer way to lc something? Chris Smith Perl Misc 16 10-04-2003 05:11 PM



Advertisments