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


Groups > comp.lang.python > #80193

Re: generating unique variable name via loops

From Matthew Ruffalo <mmr15@case.edu>
Date 2014-11-04 10:04 -0500
Subject Re: generating unique variable name via loops
References <8d3d202f-3bb8-4683-8dd7-b065fab06c34@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.15451.1415113854.18130.python-list@python.org> (permalink)

Show all headers | View raw


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...

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web