473,385 Members | 1,356 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,385 software developers and data experts.

Create object for item in list

I wonder how this can be accomplished:

I've got a list, containing several strings. The strings look like
this:

[ "Name1/N1/N2/N3" , "Name2/N1/N2/N3" , "..." ]

I would like to create one object for each item in the list, and the
name of the object should be "str(item.split("/")[0])".

I've tried this, but it won't work:

for item in list:
str(item.split("/")[0]) = class(item)

....
Jul 18 '05 #1
2 1889
"Helge" <he***@hefre.com> wrote in message
news:39**************************@posting.google.c om...
I wonder how this can be accomplished:

I've got a list, containing several strings. The strings look like
this:

[ "Name1/N1/N2/N3" , "Name2/N1/N2/N3" , "..." ]

I would like to create one object for each item in the list, and the
name of the object should be "str(item.split("/")[0])".

I've tried this, but it won't work:

for item in list:
str(item.split("/")[0]) = class(item)

...


A couple of small points before getting to the big issue:

1. "class" is a keyword, and can't be used in the context you have it. You
should define your class (which I'll call "C") earlier in your program.

2. "list" is a type name, and shouldn't be used as a variable name, so I'll
change it to "mylist".

2. The str() function is unnecessary, since item.split("/")[0] is already a
string by definition.

Now, I assume what you are trying to do is get Python to create an instance
of your class C, and assign it to the variable whose name is given by the
evaluation of item.split("/")[0]. In other words, you want Python to
execute a series of statements that look like this:

Name1 = C("Name1/N1/N2/N3")

What you've done doesn't quite accomplish this; the left side of your
assignment expression evaluates to a string, so in effect you're telling
Python to execute:

"Name1" = C("Name1/N1/N2/N3")

(At least that's how I interpret what you mean by "class(item)".) This
doesn't make sense to the interpreter, any more than "Spam" = 7 would. But
you can get Python to execute code provided to it inside a string, as
follows:

for item in mylist:
exec "%s = C('%s')" % (item.split("/")[0], item)

Good luck... Needless to say, you need to be careful about where the
contents of "mylist" come from, or you could end up overwriting other
variables that your program needs to function correctly.

--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
Jul 18 '05 #2

"Helge" <he***@hefre.com> wrote in message
news:39**************************@posting.google.c om...
I wonder how this can be accomplished:

I've got a list, containing several strings. The strings look like
this:

[ "Name1/N1/N2/N3" , "Name2/N1/N2/N3" , "..." ]

I would like to create one object for each item in the list, and the
name of the object should be "str(item.split("/")[0])".


Binding and using dynamic names in the global (or function local) namespace
is generally a nuisance. The standard idiom is to use a separate
dictionary as a namespace for just those items. Something like

myobs = {}
for datastring in namedata:
data = datastring.split('/')
myobs[data[0]] = myclass(*data[1:])

Terry J. Reedy


Jul 18 '05 #3

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

Similar topics

0
by: Helge | last post by:
I wonder how this can be accomplished: I've got a list, containing several strings. The strings look like this: I would like to create one object for each item in the list, and the name...
6
by: orahm | last post by:
I added an object to a listbox. This object is a complex number and not a string so it shows "Consolapplication.Complex" as a list item. If I convert the complex number to a string then it shows a...
6
by: madpython | last post by:
For the pure theory sake and mind expansion i got a question. Experimenting with __new__ i found out how to create a singleton. class SingleStr(object): def __new__(cls,*args,**kwargs):...
19
by: Brett Romero | last post by:
Here's a table of data I'm putting into a collection: CodeId CodeGroup CodeSubGroup Type 1 K K.1 Shar1 2 K ...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
2
by: Big Charles | last post by:
Hello, I would like to create an array-class to be able to call like: Dim oMyCar as New MyCar ' After initializing oMyCar, the object has to be like: oMyCar(0).Brand...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
10
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
5
by: Academia | last post by:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it there.) I have a listbox populated with Objects. The Class has a String field that ToString returns. I assume that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.