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


Groups > comp.lang.python > #80174

Re: generating unique variable name via loops

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'exist,': 0.09; 'function:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; '"new': 0.16; "'b',": 0.16; "['a',": 0.16; '_index': 0.16; 'dict': 0.16; 'increment': 0.16; 'names?': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:variable': 0.16; 'wrote:': 0.18; 'variable': 0.18; '>>>': 0.22; 'header:User- Agent:1': 0.23; 'directory.': 0.24; 'second': 0.26; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'dialog': 0.31; 'invoke': 0.31; 'names.': 0.31; 'file': 0.32; 'this.': 0.32; 'created': 0.35; 'really': 0.36; 'list': 0.37; 'to:addr:python- list': 0.38; 'generating': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'easy': 0.60; 'letters': 0.60; 'save': 0.62; 'name': 0.63; 'saving': 0.69; 'hand': 0.80; 'subject:via': 0.84; 'lists:': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: generating unique variable name via loops
Date Tue, 04 Nov 2014 14:09:42 +0100
Organization None
References <8d3d202f-3bb8-4683-8dd7-b065fab06c34@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p57bd9623.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
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.15443.1415106598.18130.python-list@python.org> (permalink)
Lines 53
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1415106598 news.xs4all.nl 2916 [2001:888:2000:d::a6]:33003
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:80174

Show key headers only | View raw


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'

Back to comp.lang.python | Previous | Next — Previous in thread | Next 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