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: 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 X-Google-Original-From: Matthew Ruffalo 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: =?UTF-8?B?RmF0aWggR8O8dmVu?= 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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=C3=BCven wrote: > I want to generate a unique variable name for list using python. > > list1=3D... > list2=3D... > . > . > . > listx=3D... 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 =2Etxt file and process each line, and wanting to access each employee's data as appropriate. The general structure of this would be >>> employees =3D [] >>> with open('employee_data.txt') as f: =2E.. for line in f: =2E.. # parse_line here isn't anything standard or built-in, it's= =2E.. # what you would write to transform a line of the data =2E.. # file into whatever object you're interested in with =2E.. # 'name', 'salary', etc. attributes =2E.. employee =3D parse_line(line) =2E.. employees.append(employee) =2E.. >>> 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 =3D 0 >>> for employee in employees: =2E.. total +=3D employee.salary =2E.. >>> total 7502000 (or 'sum(employee.salary for employee in employees)' of course.) MMR...