Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80171 > unrolled thread

generating unique variable name via loops

Started byFatih Güven <mfthguven@gmail.com>
First post2014-11-04 03:29 -0800
Last post2014-11-04 10:04 -0500
Articles 16 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  generating unique variable name via loops Fatih Güven <mfthguven@gmail.com> - 2014-11-04 03:29 -0800
    Re: generating unique variable name via loops Fatih Güven <mfthguven@gmail.com> - 2014-11-04 04:48 -0800
      Re: generating unique variable name via loops Veek M <vek.m1234@gmail.com> - 2014-11-04 18:49 +0630
        Re: generating unique variable name via loops Peter Otten <__peter__@web.de> - 2014-11-04 14:37 +0100
          Re: generating unique variable name via loops Fatih Güven <mfthguven@gmail.com> - 2014-11-04 05:53 -0800
            Re: generating unique variable name via loops Peter Otten <__peter__@web.de> - 2014-11-04 16:00 +0100
              Re: generating unique variable name via loops Fatih Güven <mfthguven@gmail.com> - 2014-11-04 07:36 -0800
                Re: generating unique variable name via loops Peter Otten <__peter__@web.de> - 2014-11-04 17:06 +0100
            Re: generating unique variable name via loops Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-04 17:15 +0000
            Re: generating unique variable name via loops Tim Chase <python.list@tim.thechases.com> - 2014-11-04 09:05 -0600
        Re: generating unique variable name via loops Fatih Güven <mfthguven@gmail.com> - 2014-11-04 05:45 -0800
          Re: generating unique variable name via loops Veek M <vek.m1234@gmail.com> - 2014-11-04 19:20 +0630
          Re: generating unique variable name via loops Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-04 17:14 +0000
        Re: generating unique variable name via loops Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-04 09:00 -0500
    Re: generating unique variable name via loops Peter Otten <__peter__@web.de> - 2014-11-04 14:09 +0100
    Re: generating unique variable name via loops Matthew Ruffalo <mmr15@case.edu> - 2014-11-04 10:04 -0500

#80171 — generating unique variable name via loops

FromFatih Güven <mfthguven@gmail.com>
Date2014-11-04 03:29 -0800
Subjectgenerating unique variable name via loops
Message-ID<8d3d202f-3bb8-4683-8dd7-b065fab06c34@googlegroups.com>
Hi,

I want to generate a unique variable name for list using python.

list1=...
list2=...
.
.
.
listx=... where x is a number.

You can remember it from saving a file in a directory. If you have already created a "new file", save dialog sugget that <<"new file " is already exist, do you want to save it as "new file (1)"?>> so I want to apply it for list names. 

Do you any idea about this.

[toc] | [next] | [standalone]


#80173

FromFatih Güven <mfthguven@gmail.com>
Date2014-11-04 04:48 -0800
Message-ID<8df0b347-0569-4505-95f8-12baf44a1743@googlegroups.com>
In reply to#80171
4 Kasım 2014 Salı 13:29:34 UTC+2 tarihinde Fatih Güven yazdı:
Editted: Grammar revision.
> Hi,
> 
> I want to generate a unique variable name for list using python.
> 
> list1=...
> list2=...
> .
> .
> .
> listx=... where x is a number.
> 
> You can remember it from saving a file in a directory. If you have already created a "new file", save dialog suggest that <<"new file " is already exist, do you want to save it as "new file (1)"?>> so I want to apply it for list names. 
> 
> Do you have any idea about this.

[toc] | [prev] | [next] | [standalone]


#80176

FromVeek M <vek.m1234@gmail.com>
Date2014-11-04 18:49 +0630
Message-ID<m3ajo4$kit$1@dont-email.me>
In reply to#80173
Fatih Güven wrote:

> 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?:
>> I want to generate a unique variable name for list using python.
>> 
>> list1=...
>> list2=...

for x in range(1,10):
    exec("list%d = []" % x)

[toc] | [prev] | [next] | [standalone]


#80178

FromPeter Otten <__peter__@web.de>
Date2014-11-04 14:37 +0100
Message-ID<mailman.15445.1415108267.18130.python-list@python.org>
In reply to#80176
Veek M wrote:

> Fatih Güven wrote:
> 
>> 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?:
>>> I want to generate a unique variable name for list using python.
>>> 
>>> list1=...
>>> list2=...
> 
> for x in range(1,10):
>     exec("list%d = []" % x)

Why would you do this? 

[toc] | [prev] | [next] | [standalone]


#80181

FromFatih Güven <mfthguven@gmail.com>
Date2014-11-04 05:53 -0800
Message-ID<ad63b305-5ffd-41b3-bb58-080386a90154@googlegroups.com>
In reply to#80178
4 Kasım 2014 Salı 15:37:59 UTC+2 tarihinde Peter Otten yazdı:
> Veek M wrote:
> 
> > Fatih Güven wrote:
> > 
> >> 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?:
> >>> I want to generate a unique variable name for list using python.
> >>> 
> >>> list1=...
> >>> list2=...
> > 
> > for x in range(1,10):
> >     exec("list%d = []" % x)
> 
> Why would you do this?

I have a structured and repetitive data. I want to read a .txt file line by line and classified it to call easily. For example employee1 has a name, a salary, shift, age etc. and employee2 and other 101 employee have all of it. 

Call employee1.name or employee2.salary and assign it to a new variable, something etc.

[toc] | [prev] | [next] | [standalone]


#80191

FromPeter Otten <__peter__@web.de>
Date2014-11-04 16:00 +0100
Message-ID<mailman.15450.1415113261.18130.python-list@python.org>
In reply to#80181
Fatih Güven wrote:

> 4 Kasım 2014 Salı 15:37:59 UTC+2 tarihinde Peter Otten yazdı:
>> Veek M wrote:
>> 
>> > Fatih Güven wrote:
>> > 
>> >> 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?:
>> >>> I want to generate a unique variable name for list using python.
>> >>> 
>> >>> list1=...
>> >>> list2=...
>> > 
>> > for x in range(1,10):
>> >     exec("list%d = []" % x)
>> 
>> Why would you do this?
> 
> I have a structured and repetitive data. 

I was actually asking "Veek M".

> I want to read a .txt file line
> by line and classified it to call easily. For example employee1 has a
> name, a salary, shift, age etc. and employee2 and other 101 employee have
> all of it.
> 
> Call employee1.name or employee2.salary and assign it to a new variable,
> something etc.

I can only repeat my previous advice. Instead of creating variables for 
employee1, employee2, and so on make a list of employees:

$ cat employees.txt
Peter,3000
Paul,2000
Mary,1000

$ cat employees.py
#!/usr/bin/env python3
import csv

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

if __name__ == "__main__":
    employees = []
    with open("employees.txt") as f:
        for row in csv.reader(f):
            employees.append(Employee(row[0], int(row[1])))

    for employee in employees:
        print(employee.name, "-- salary:", employee.salary, "doubloons")

$ python3 employees.py 
Peter -- salary: 3000 doubloons
Paul -- salary: 2000 doubloons
Mary -- salary: 1000 doubloons

You wouldn't want to reference Paul as employee2 -- what if the order in the 
text file changed? Instead you can make a dict that maps name to employee...

    employees_by_name = {}
    for employee in employees:
        name = employee.name
        if name in employees_by_name:
            raise ValueError("duplicate name {}".format(name))
        employees_by_name[name] = employee

and use that dict to look up an employee:

    while True:
        name = input("enter a name ")
        if name == "":
            print("That's all folks")
            break
        if name not in employees_by_name:
            print("unknown name")
        else:
            print("Salary:", employees_by_name[name].salary, "doubloons")

$ python3 employees.py 
Peter -- salary: 3000 doubloons
Paul -- salary: 2000 doubloons
Mary -- salary: 1000 doubloons
enter a name Peter
Salary: 3000 doubloons
enter a name Mary
Salary: 1000 doubloons
enter a name paul
unknown name
enter a name Paul
Salary: 2000 doubloons
enter a name 
That's all folks
$ 

[toc] | [prev] | [next] | [standalone]


#80197

FromFatih Güven <mfthguven@gmail.com>
Date2014-11-04 07:36 -0800
Message-ID<67a561c5-82db-4a98-a9d2-d5ea18a74c50@googlegroups.com>
In reply to#80191
4 Kasım 2014 Salı 17:01:17 UTC+2 tarihinde Peter Otten yazdı:
> Fatih Güven wrote:
> 
> > 4 Kasım 2014 Salı 15:37:59 UTC+2 tarihinde Peter Otten yazdı:
> >> Veek M wrote:
> >> 
> >> > Fatih Güven wrote:
> >> > 
> >> >> 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?:
> >> >>> I want to generate a unique variable name for list using python.
> >> >>> 
> >> >>> list1=...
> >> >>> list2=...
> >> > 
> >> > for x in range(1,10):
> >> >     exec("list%d = []" % x)
> >> 
> >> Why would you do this?
> > 
> > I have a structured and repetitive data. 
> 
> I was actually asking "Veek M".
> 
> > I want to read a .txt file line
> > by line and classified it to call easily. For example employee1 has a
> > name, a salary, shift, age etc. and employee2 and other 101 employee have
> > all of it.
> > 
> > Call employee1.name or employee2.salary and assign it to a new variable,
> > something etc.
> 
> I can only repeat my previous advice. Instead of creating variables for 
> employee1, employee2, and so on make a list of employees:
> 
> $ cat employees.txt
> Peter,3000
> Paul,2000
> Mary,1000
> 
> $ cat employees.py
> #!/usr/bin/env python3
> import csv
> 
> class Employee:
>     def __init__(self, name, salary):
>         self.name = name
>         self.salary = salary
> 
> if __name__ == "__main__":
>     employees = []
>     with open("employees.txt") as f:
>         for row in csv.reader(f):
>             employees.append(Employee(row[0], int(row[1])))
> 
>     for employee in employees:
>         print(employee.name, "-- salary:", employee.salary, "doubloons")
> 
> $ python3 employees.py 
> Peter -- salary: 3000 doubloons
> Paul -- salary: 2000 doubloons
> Mary -- salary: 1000 doubloons
> 
> You wouldn't want to reference Paul as employee2 -- what if the order in the 
> text file changed? Instead you can make a dict that maps name to employee...
> 
>     employees_by_name = {}
>     for employee in employees:
>         name = employee.name
>         if name in employees_by_name:
>             raise ValueError("duplicate name {}".format(name))
>         employees_by_name[name] = employee
> 
> and use that dict to look up an employee:
> 
>     while True:
>         name = input("enter a name ")
>         if name == "":
>             print("That's all folks")
>             break
>         if name not in employees_by_name:
>             print("unknown name")
>         else:
>             print("Salary:", employees_by_name[name].salary, "doubloons")
> 
> $ python3 employees.py 
> Peter -- salary: 3000 doubloons
> Paul -- salary: 2000 doubloons
> Mary -- salary: 1000 doubloons
> enter a name Peter
> Salary: 3000 doubloons
> enter a name Mary
> Salary: 1000 doubloons
> enter a name paul
> unknown name
> enter a name Paul
> Salary: 2000 doubloons
> enter a name 
> That's all folks
> $


Thanks for your concern, I will try this. Actually, the main focus is that are there any other Paul in my team. So i want to create a simple ID for employee to distinguish two Paul. I belive that you have a solution for this.

[toc] | [prev] | [next] | [standalone]


#80198

FromPeter Otten <__peter__@web.de>
Date2014-11-04 17:06 +0100
Message-ID<mailman.15455.1415117210.18130.python-list@python.org>
In reply to#80197
Fatih Güven wrote:

> 4 Kasım 2014 Salı 17:01:17 UTC+2 tarihinde Peter Otten yazdı:
>> Fatih Güven wrote:
>> 
>> > 4 Kasım 2014 Salı 15:37:59 UTC+2 tarihinde Peter Otten yazdı:
>> >> Veek M wrote:
>> >> 
>> >> > Fatih Güven wrote:
>> >> > 
>> >> >> 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?:
>> >> >>> I want to generate a unique variable name for list using python.
>> >> >>> 
>> >> >>> list1=...
>> >> >>> list2=...
>> >> > 
>> >> > for x in range(1,10):
>> >> >     exec("list%d = []" % x)
>> >> 
>> >> Why would you do this?
>> > 
>> > I have a structured and repetitive data.
>> 
>> I was actually asking "Veek M".
>> 
>> > I want to read a .txt file line
>> > by line and classified it to call easily. For example employee1 has a
>> > name, a salary, shift, age etc. and employee2 and other 101 employee
>> > have all of it.
>> > 
>> > Call employee1.name or employee2.salary and assign it to a new
>> > variable, something etc.
>> 
>> I can only repeat my previous advice. Instead of creating variables for
>> employee1, employee2, and so on make a list of employees:
>> 
>> $ cat employees.txt
>> Peter,3000
>> Paul,2000
>> Mary,1000
>> 
>> $ cat employees.py
>> #!/usr/bin/env python3
>> import csv
>> 
>> class Employee:
>>     def __init__(self, name, salary):
>>         self.name = name
>>         self.salary = salary
>> 
>> if __name__ == "__main__":
>>     employees = []
>>     with open("employees.txt") as f:
>>         for row in csv.reader(f):
>>             employees.append(Employee(row[0], int(row[1])))
>> 
>>     for employee in employees:
>>         print(employee.name, "-- salary:", employee.salary, "doubloons")
>> 
>> $ python3 employees.py
>> Peter -- salary: 3000 doubloons
>> Paul -- salary: 2000 doubloons
>> Mary -- salary: 1000 doubloons
>> 
>> You wouldn't want to reference Paul as employee2 -- what if the order in
>> the text file changed? Instead you can make a dict that maps name to
>> employee...
>> 
>>     employees_by_name = {}
>>     for employee in employees:
>>         name = employee.name
>>         if name in employees_by_name:
>>             raise ValueError("duplicate name {}".format(name))
>>         employees_by_name[name] = employee
>> 
>> and use that dict to look up an employee:
>> 
>>     while True:
>>         name = input("enter a name ")
>>         if name == "":
>>             print("That's all folks")
>>             break
>>         if name not in employees_by_name:
>>             print("unknown name")
>>         else:
>>             print("Salary:", employees_by_name[name].salary, "doubloons")
>> 
>> $ python3 employees.py
>> Peter -- salary: 3000 doubloons
>> Paul -- salary: 2000 doubloons
>> Mary -- salary: 1000 doubloons
>> enter a name Peter
>> Salary: 3000 doubloons
>> enter a name Mary
>> Salary: 1000 doubloons
>> enter a name paul
>> unknown name
>> enter a name Paul
>> Salary: 2000 doubloons
>> enter a name
>> That's all folks
>> $
> 
> 
> Thanks for your concern, I will try this. Actually, the main focus is that
> are there any other Paul in my team. So i want to create a simple ID for
> employee to distinguish two Paul. I belive that you have a solution for
> this.

The easiest is to use the index:

#!/usr/bin/env python3
import csv

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

if __name__ == "__main__":
    employees = []
    with open("employees.txt") as f:
        for row in csv.reader(f):
            employees.append(Employee(row[0], int(row[1])))

    for index, employee in enumerate(employees, 1):
        print("#{}, name: {}".format(index, employee.name))

    while True:
        index = input("enter an index (1...{}) ".format(len(employees)))
        if index == "":
            print("That's all folks")
            break
        index = int(index) -1
        employee = employees[index]
        print("Name: {0.name}, Salary: {0.salary}".format(employee))

If the ID must stay the same for two runs of the script you have to put it 
into the text file as another column. However, you are soon reaching 
territory where a database is more convenient than a text file.

[toc] | [prev] | [next] | [standalone]


#80207

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-11-04 17:15 +0000
Message-ID<m3b1ka$5fi$2@dont-email.me>
In reply to#80181
On Tue, 04 Nov 2014 05:53:04 -0800, Fatih Güven wrote:

> Call employee1.name or employee2.salary and assign it to a new variable,
> something etc.

1) Put the file into a database.
2) database calls

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#80216

FromTim Chase <python.list@tim.thechases.com>
Date2014-11-04 09:05 -0600
Message-ID<mailman.15462.1415125749.18130.python-list@python.org>
In reply to#80181
On 2014-11-04 05:53, Fatih Güven wrote:
> > > for x in range(1,10):
> > >     exec("list%d = []" % x)
> > 
> > Why would you do this?
> 
> I have a structured and repetitive data. I want to read a .txt file
> line by line and classified it to call easily. For example
> employee1 has a name, a salary, shift, age etc. and employee2 and
> other 101 employee have all of it. 
> 
> Call employee1.name or employee2.salary and assign it to a new
> variable, something etc. -- 

This sounds remarkably like a CSV or tab-delimited file.  If so, the
way to do it would be

  import csv
  with open("data.txt", "rb") as f:
    dr = csv.DictReader(f)
    for row in dr:
      do_something(row["Name"], row["salary"])


If the file format is more complex, it's often useful to create a
generator to simplify the logic:

  class Person:
    def __init__(self,
        name="",
        salary=0,
        shift="",
        ):
      self.name = name
      self.salary = salary
      self.shift = shift
    def various_person_methods(self, ...):
      pass

  def people_from_file(f):
    "build Person objects as you iterate over the file"
    for row in file:
      person = Person( ... )
      yield person

  with open("data.txt", "r"):
    for person in people_from_file(f):
      do_something(person)

You can then reuse that generator with multiple files if you need.

-tkc



[toc] | [prev] | [next] | [standalone]


#80179

FromFatih Güven <mfthguven@gmail.com>
Date2014-11-04 05:45 -0800
Message-ID<95028c6b-b6d2-40ec-9cc7-557cc9e42342@googlegroups.com>
In reply to#80176
4 Kasım 2014 Salı 15:19:20 UTC+2 tarihinde Veek M yazdı:
> Fatih Güven wrote:
> 
> > 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?:
> >> I want to generate a unique variable name for list using python.
> >> 
> >> list1=...
> >> list2=...
> 
> for x in range(1,10):
>     exec("list%d = []" % x)

This is okay but i can't use the method ".append" for example list1.append("abc")

[toc] | [prev] | [next] | [standalone]


#80180

FromVeek M <vek.m1234@gmail.com>
Date2014-11-04 19:20 +0630
Message-ID<m3alib$r7m$1@dont-email.me>
In reply to#80179
Fatih Güven wrote:

> This is okay but i can't use the method ".append" for example
> list1.append("abc")

works for me

[toc] | [prev] | [next] | [standalone]


#80206

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-11-04 17:14 +0000
Message-ID<m3b1hi$5fi$1@dont-email.me>
In reply to#80179
On Tue, 04 Nov 2014 05:45:04 -0800, Fatih Güven wrote:

> 4 Kasım 2014 Salı 15:19:20 UTC+2 tarihinde Veek M yazdı:
>> Fatih Güven wrote:
>> 
>> > 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?:
>> >> I want to generate a unique variable name for list using python.
>> >> 
>> >> list1=...
>> >> list2=...
>> 
>> for x in range(1,10):
>>     exec("list%d = []" % x)
> 
> This is okay but i can't use the method ".append" for example
> list1.append("abc")

This is one solution using a dictionary of lists to maintain the name 
association. It may not be the best method. It may not be the best 
solution for you. It may not be the answer your instructor is looking 
for, and it contains deliberate syntax errors.

lists = {}

for fn in filenames
    infile = open(fn, "r")
    lists[fn] = []
    for line in infile
        lists[fn].append(line)
    infile.close()

If you have to ask how to do this sort of thing, you probably shouldn't 
be coding employee data processing systems anyway!

If this was a coding assignment for a course, you should have had 
sufficient instruction in the relevant algorithms and language features 
to be able to figure it out yourself.

In either case, what not explain what you tried, what you expected it to 
do, and what it actually did.


-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#80182

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2014-11-04 09:00 -0500
Message-ID<mailman.15446.1415109642.18130.python-list@python.org>
In reply to#80176
On Tue, 04 Nov 2014 18:49:14 +0630, Veek M <vek.m1234@gmail.com> declaimed
the following:

>Fatih Güven wrote:
>
>> 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?:
>>> I want to generate a unique variable name for list using python.
>>> 
>>> list1=...
>>> list2=...
>
>for x in range(1,10):
>    exec("list%d = []" % x)

	Of course, now that you have them... what can you do with them? Without
scattering exec() calls all over the code.


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#80174

FromPeter Otten <__peter__@web.de>
Date2014-11-04 14:09 +0100
Message-ID<mailman.15443.1415106598.18130.python-list@python.org>
In reply to#80171
Fatih Güven wrote:

> I want to generate a unique variable name for list using python.
> 
> list1=...
> list2=...
> .
> .
> .
> listx=... where x is a number.
> 
> You can remember it from saving a file in a directory. If you have already
> created a "new file", save dialog sugget that <<"new file " is already
> exist, do you want to save it as "new file (1)"?>> so I want to apply it
> for list names.
> 
> Do you any idea about this.

Do you really want to use

list1, list2, list3, ... as variable names? You shouldn't. Instead use a 
dict or a list of lists:

>>> list_of_lists = []
>>> list_of_lists.append([1, 2, 3])
>>> list_of_lists.append(["a", "b", "c"])
>>> list_of_lists.append(["foo", "bar", "baz"])

You can then access the second list with

>>> letters = list_of_lists[1]
>>> print(letters)
['a', 'b', 'c']

On the other hand if you just want to generate names an easy to understand 
approach is to increment a global variable every time you invoke the name-
generating function:

>>> _index = 1
>>> def next_name():
...     global _index
...     name = "list{}".format(_index)
...     _index += 1
...     return name
... 
>>> next_name()
'list1'
>>> next_name()
'list2'
>>> next_name()
'list3'

[toc] | [prev] | [next] | [standalone]


#80193

FromMatthew Ruffalo <mmr15@case.edu>
Date2014-11-04 10:04 -0500
Message-ID<mailman.15451.1415113854.18130.python-list@python.org>
In reply to#80171
Hi-

Questions like this appear so often in various places (mailing lists,
forums, sites like Stack Overflow) that I think a very blunt/candid
answer is appropriate. This is especially true since there's always
someone who responds to the question as-is with some monstrosity of
exec() and string formatting, instead of addressing the underlying issue
of using the right data structure for what you're trying to accomplish.

On 11/04/2014 06:29 AM, Fatih Güven wrote:
> I want to generate a unique variable name for list using python.
>
> list1=...
> list2=...
> .
> .
> .
> listx=... where x is a number.
*Incorrect.* You do not want to do this. You think you do, but that's
presumably because you aren't familiar with common data structures that
are available in Python. As Peter Otten said, this is *exactly* the
right situation to use a list.

You mentioned having structured and repetitive data, wanting to read a
.txt file and process each line, and wanting to access each employee's
data as appropriate. The general structure of this would be

>>> employees = []
>>> with open('employee_data.txt') as f:
...     for line in f:
...         # parse_line here isn't anything standard or built-in, it's
...         # what you would write to transform a line of the data
...         # file into whatever object you're interested in with
...         # 'name', 'salary', etc. attributes
...         employee = parse_line(line)
...         employees.append(employee)
...
>>> employees[0].salary
150000

Your line1 is now employees[0] and so on. Now, you can *easily* answer
questions like "what's the total salary of all employees?"

>>> total = 0
>>> for employee in employees:
...     total += employee.salary
...
>>> total
7502000

(or 'sum(employee.salary for employee in employees)' of course.)

MMR...

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web