473,226 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

Is there a nicer way to do this?


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.
Oct 4 '07 #1
12 1396
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
Oct 4 '07 #2
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

Oct 4 '07 #3
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.

Oct 4 '07 #4

"Stefan Arentz" <st***********@gmail.comwrote in message
news:87************@keizer.soze.com...
|
| 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


Oct 4 '07 #5
On Oct 4, 5:42 pm, Casey <Casey...@gmail.comwrote:
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!

Oct 4 '07 #6
On Oct 4, 10:53 pm, "Terry Reedy" <tjre...@udel.eduwrote:
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

Oct 4 '07 #7
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 line5.
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-----

Oct 4 '07 #8

"Paul Hankin" <pa*********@gmail.comwrote in message
news:11*********************@57g2000hsv.googlegrou ps.com...
| On Oct 4, 10:53 pm, "Terry Reedy" <tjre...@udel.eduwrote:
| 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.

Oct 5 '07 #9
Stefan Arentz <st***********@gmail.comwrites:
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.
Oct 5 '07 #10
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.
There is:

attributeNames = dict(
("AttributeName.%d" % n, attr) \
for n, attr in enumerate(attributes)
)

Oct 5 '07 #11
Victor B. Gonzalez wrote:
anyhow, I keep getting "SyntaxError: Non-ASCII character '\xc2'..." on line 5.
anyone know what this is?
I too had that problem with KNode. Leading space consists of NO-BREAK SPACE
(unichr(160)) which translates to '\xc2\xa0' in UTF-8. As I don't see this
problem here (using pan) I suspect it may be specific to KMail/KNode.

Peter
Oct 5 '07 #12
On Friday 05 October 2007 3:33:43 am Peter Otten wrote:
Victor B. Gonzalez wrote:
anyhow, I keep getting "SyntaxError: Non-ASCII character '\xc2'..." on
line 5. anyone know what this is?

I too had that problem with KNode. Leading space consists of NO-BREAK SPACE
(unichr(160)) which translates to '\xc2\xa0' in UTF-8. As I don't see this
problem here (using pan) I suspect it may be specific to KMail/KNode.

Peter
You're right, thank you for pointing that out. I am bad with almost anything
like \this but stripping off the leading whitespace solved the issue. thank
you for the heads up!

--
Best Regards
Victor B. Gonzalez
Oct 5 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: James | last post by:
I have a from with 2 fields: Company & Name Depening which is completed, one of the following queries will be run: if($Company){ $query = "Select C* From tblsample Where ID = $Company...
5
by: Scott D | last post by:
I am trying to check and see if a field is posted or not, if not posted then assign $location which is a session variable to $location_other. If it is posted then just assign it to...
2
by: Nick | last post by:
Can someone please tell me how to access elements from a multiple selection list? From what ive read on other posts, this is correct. I keep getting an "Undefined variable" error though... Form...
2
by: Alexander Ross | last post by:
I have a variable ($x) that can have 50 different (string) values. I want to check for 7 of those values and do something based on it ... as I see it I have 2 options: 1) if (($x=="one") ||...
0
by: Michele Simionato | last post by:
Here is an idea for a nicer syntax in cooperative method calls, which is not based on Guido's "autosuper" example. This is just a hack, waiting for a nicer "super" built-in ... Here is example...
6
by: Markus Rosenstihl | last post by:
Hi, I wonder if i can make this nicer: In my case a list is created from a bill which is a tab seperated file and each line looks like this: "Verbindungen Deutsche Telekom vom 03.08.2005 bis...
6
by: Ivan Weiss | last post by:
Hi all, I am writing an app that displays data such as a list of customers, vendors, products etc... in a listview. The listview however is pretty ugly and boring and I am pretty bad at making...
0
by: Graeme Hinchliffe | last post by:
Hiya, (me again :) ) I am using in my C function the field names of a row. The way I am currently pulling out this info is with: ...
1
by: tomasio | last post by:
Dear Pros, How can I achieve a nicer hover-effect for the image "tomasio.design" on the bottom-right of my webpage? I am using the class ".footer a:hover" for a CSS-based mouseover-effect but...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.