NEW TO THIS: Assigning values to strings in list?

Johannes Graumann graumann at clyde.caltech.edu
Fri Jul 19 14:07:20 EDT 2002


Chris Liechti <cliechti at gmx.net> wrote:
> Johannes Graumann <graumann at clyde.caltech.edu> wrote in 
> news:ah9cvk$st at gap.cco.caltech.edu:
>> Basicely I have two lists, one containing the future string-names 
>> and one containing the values I want to give them. The order is
>> identical in both. Right now I'm solving this by a dictionary, but 
>> I would really prefer to have straight strings as a result of my 
>> operation.

> i think i don't understand what you exactly want... maybe you could 
> illustrate you problem with an example of data and how the result should 
> look like or a few lines from the interactive prompt, like:

Sure! Here is what I wrote:
def listreadout(list):
	if  len(list) != (len(otherlist)):
		print '\n\t%d element(s) given, but %d reqired by listreadout!' % (len(otherlist), len(list))
		meckerei()#sys.exit() and some more complaints in here ...
	global lexicon
	lexicon = {}
	for index in range(len(list)):
		lexicon[list[index]] = otherlist[index]

Could be made much more elegant with what Chris gave me ... ;0)
>>>> names = ["one", "two", "tree"]
>>>> values = [1,2,3]
>>>> dict(zip(names,values))
> {'tree': 3, 'two': 2, 'one': 1}

BUT: what I really want is (in therms of Chris's example):
>>>names = ["one","two","three"]
>>>values = [ 1,2,3]
--> DO SOMETHING HERE
>>>print one
1

Joh

-- 



More information about the Python-list mailing list