Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80193
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <mmr15@case.edu> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.022 |
| X-Spam-Evidence | '*H*': 0.96; '*S*': 0.00; 'python.': 0.02; 'transform': 0.07; 'string': 0.09; 'appropriate.': 0.09; 'attributes': 0.09; 'cc:addr:python-list': 0.11; '.txt': 0.16; 'as-is': 0.16; "employee's": 0.16; 'formatting,': 0.16; 'subject:variable': 0.16; 'underlying': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'trying': 0.19; 'stack': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; "aren't": 0.24; '(or': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'appear': 0.29; 'am,': 0.29; 'said,': 0.30; 'especially': 0.30; 'file': 0.32; 'this.': 0.32; 'received:209.85': 0.35; 'common': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'data,': 0.36; 'received:10.0': 0.36; 'employee': 0.37; 'list': 0.37; 'list.': 0.37; 'received:209': 0.37; 'received:10': 0.37; 'skip:o 20': 0.38; 'whatever': 0.38; 'issue': 0.38; 'anything': 0.39; 'structure': 0.39; 'read': 0.60; 'mentioned': 0.61; "you're": 0.61; 'name': 0.63; 'places': 0.64; 'total': 0.65; 'situation': 0.65; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'line,': 0.68; 'employees:': 0.84; 'otten': 0.84; 'presumably': 0.84; 'subject:via': 0.84; 'wanting': 0.93 |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:message-id:date:user-agent:mime-version:to :cc:subject:references:in-reply-to:content-type :content-transfer-encoding; bh=5uuG7nPFSsOa6Wppjckb6mCVC/UEiCqUVI8UpxOtEyk=; b=VtPyRDJy59tjObZ9xBLlQ88moOpqFCOsCmHuUzdPq6Y6+LoDpS1CHD1Y2Wc1TjPGO1 x0arK9bPPdm0QE39Xn4t9LWJq+KAnEqLAHNu5ZSpGTIu2nu9HMahpZ77ECrFdZ+gOjIE bqybsSmDJs4SziiCsJwgpTteReSTaOKHgfkQY7sH5q+8hEnQHn6C8CZnE2BIFUZMD2+6 D+DQ3xR0X96suWN0wXWJyLjk20wNVt7MTD+tnJfL0ruFS+WRWuj0zFUgwtyn8UlS+0g7 sw1SruQgNxNwt92wsjTTqzbAKul/eoekWberJ8CliJWgEjI1iX93ZVnbWhrb4EUxmzQV wmHA== |
| X-Gm-Message-State | ALoCoQnSKPfyBRVtIE8eIKP0La5rPmdsE2899s1QYLSzCo8tUc8iKbJfUElNj7BglLYCoN4oxfu/NymXvxYc1dfEHvQSQ3ixZAzjUTU2DrvKiCgX2u/OPMNO7kWMmz//cvgUSEw1QsV2 |
| X-Received | by 10.140.101.68 with SMTP id t62mr44982431qge.92.1415113460021; Tue, 04 Nov 2014 07:04:20 -0800 (PST) |
| X-Received | by 10.140.101.68 with SMTP id t62mr44982413qge.92.1415113459948; Tue, 04 Nov 2014 07:04:19 -0800 (PST) |
| From | Matthew Ruffalo <mmr15@case.edu> |
| X-Google-Original-From | Matthew Ruffalo <matthew.ruffalo@case.edu> |
| Date | Tue, 04 Nov 2014 10:04:18 -0500 |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 |
| MIME-Version | 1.0 |
| To | Fatih Güven <mfthguven@gmail.com> |
| Subject | Re: generating unique variable name via loops |
| References | <8d3d202f-3bb8-4683-8dd7-b065fab06c34@googlegroups.com> |
| In-Reply-To | <8d3d202f-3bb8-4683-8dd7-b065fab06c34@googlegroups.com> |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-Junkmail-Whitelist | YES (by domain whitelist at mpv1.tis.cwru.edu) |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15451.1415113854.18130.python-list@python.org> (permalink) |
| Lines | 55 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1415113854 news.xs4all.nl 2861 [2001:888:2000:d::a6]:60143 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:80193 |
Show key headers only | 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
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