[Python-de] Dictionarie aus Liste erzeugen
Olaf Delgado
delgado at Mathematik.Uni-Bielefeld.DE
Sat Mar 2 19:58:15 EST 2002
On Sat, Mar 02, 2002 at 07:48:07PM +0100, Albert Hermeling wrote:
>
> import string
> liste=['end_button_color red', 'return_button_color blue']
>
> dict={ }
>
> b=0
> for i in liste:
> a=1
> for ii in string.split(liste[b]):
> if a == 1:
> lings=ii
> else:
> rechts=ii
> a=a+1
> dict[lings]=rechts
> b=b+1
>
> Diese Loesung funktioniert recht gut, aber so ganz gluecklich bin
> ich mit der Loesung aber nicht. An mir nagt der Zweifel, ob es nicht
> vielleicht eine bessere Lösung gibt.
Ich wuerde es ungefaehr so machen:
# --- schnipp! ---
import string
liste=['end_button_color red', 'return_button_color blue']
dict = {}
for zeile in liste:
(schluessel, wert) = string.split(zeile)
dict[schluessel] = wert
# --- schnapp! ---
Gruss,
Olaf
More information about the Python-de
mailing list