Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80174
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: generating unique variable name via loops |
| Date | 2014-11-04 14:09 +0100 |
| Organization | None |
| References | <8d3d202f-3bb8-4683-8dd7-b065fab06c34@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15443.1415106598.18130.python-list@python.org> (permalink) |
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
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