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


Groups > comp.lang.python > #80206

Re: generating unique variable name via loops

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.python
Subject Re: generating unique variable name via loops
Date 2014-11-04 17:14 +0000
Organization A noiseless patient Spider
Message-ID <m3b1hi$5fi$1@dont-email.me> (permalink)
References <8d3d202f-3bb8-4683-8dd7-b065fab06c34@googlegroups.com> <8df0b347-0569-4505-95f8-12baf44a1743@googlegroups.com> <m3ajo4$kit$1@dont-email.me> <95028c6b-b6d2-40ec-9cc7-557cc9e42342@googlegroups.com>

Show all headers | View raw


On Tue, 04 Nov 2014 05:45:04 -0800, Fatih Güven wrote:

> 4 Kasım 2014 Salı 15:19:20 UTC+2 tarihinde Veek M yazdı:
>> Fatih Güven wrote:
>> 
>> > 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?:
>> >> I want to generate a unique variable name for list using python.
>> >> 
>> >> list1=...
>> >> list2=...
>> 
>> for x in range(1,10):
>>     exec("list%d = []" % x)
> 
> This is okay but i can't use the method ".append" for example
> list1.append("abc")

This is one solution using a dictionary of lists to maintain the name 
association. It may not be the best method. It may not be the best 
solution for you. It may not be the answer your instructor is looking 
for, and it contains deliberate syntax errors.

lists = {}

for fn in filenames
    infile = open(fn, "r")
    lists[fn] = []
    for line in infile
        lists[fn].append(line)
    infile.close()

If you have to ask how to do this sort of thing, you probably shouldn't 
be coding employee data processing systems anyway!

If this was a coding assignment for a course, you should have had 
sufficient instruction in the relevant algorithms and language features 
to be able to figure it out yourself.

In either case, what not explain what you tried, what you expected it to 
do, and what it actually did.


-- 
Denis McMahon, denismfmcmahon@gmail.com

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