Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A better way to assign list into a var

Tags:

python

Was coding something in Python. Have a piece of code, wanted to know if it can be done more elegantly...

# Statistics format is - done|remaining|200's|404's|size
statf = open(STATS_FILE, 'r').read()
starf = statf.strip().split('|')
done  = int(starf[0])
rema  = int(starf[1])
succ  = int(starf[2])
fails = int(starf[3])
size  = int(starf[4])
...

This goes on. I wanted to know if after splitting the line into a list, is there any better way to assign each list into a var. I have close to 30 lines assigning index values to vars. Just trying to learn more about Python that's it...

like image 989
Srikar Appalaraju Avatar asked Dec 07 '22 01:12

Srikar Appalaraju


1 Answers

done, rema, succ, fails, size, ... = [int(x) for x in starf]

Better:

labels = ("done", "rema", "succ", "fails", "size")

data = dict(zip(labels, [int(x) for x in starf]))

print data['done']
like image 157
leoluk Avatar answered Dec 09 '22 13:12

leoluk