Parsing & question about usages off classes!
Dennis Lee Bieber
wlfraed at ix.netcom.com
Sun Nov 3 17:26:06 EST 2002
Frans fed this fish to the penguins on Sunday 03 November 2002 11:58 am:
Yes, it's me again...
> I want to create several new list. The name off the new lists will
> come from another list. That way I can create several lists, depending
> on the data received from logfiles. Is that possible ?
>
> Or isnt that a good idea either ? :)
>
> So basicly:
> my_list['some','data']
> variable = my_list[0]
> variable = list
>
> Now this doesnt work, but the result I want is a new list with the
> name 'some'.
>
NOT recommended but seems to work on my system...
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', '__doc__': None}
>>> alist = ['log1', 'log3', 'log2', 'logn']
>>> i = 0
>>> for lg in alist:
... globals()[lg] = [i,'log data', i*20]
... i = i+1
...
>>> globals()
{'lg': 'logn', 'log2': [2, 'log data', 40], 'log3': [1, 'log data',
20], 'log1': [0, 'log data', 0], 'i': 4, 'alist': ['log1', 'log3',
'log2', 'logn'], '__builtins__': <module '__builtin__' (built-in)>,
'logn': [3, 'log data', 60], '__name__': '__main__', '__doc__': None}
>>> log1
[0, 'log data', 0]
>>> log2
[2, 'log data', 40]
>>> log3
[1, 'log data', 20]
>>> logn
[3, 'log data', 60]
But again, you have the problem that you either have to KNOW the name
to code it (as in the last displays), OR you need to access it by using:
globals()[string_containing_name]
If you have to use that reference anyways, why hassle with modifying
the global scope -- just create your own dictionary and reference it
(as my prior post does).
--
--
> ============================================================== <
> wlfraed at ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wulfraed at dm.net | Bestiaria Support Staff <
> ============================================================== <
> Bestiaria Home Page: http://www.beastie.dm.net/ <
> Home Page: http://www.dm.net/~wulfraed/ <
More information about the Python-list
mailing list