python - create variable for one tuple item at the end of a list -
i have list of tuples called list_cs
:
('2015-05-14', 685) ('2015-04-15', 680) ('2015-03-20', 675) ('2015-02-11', 680) ('2015-01-13', 685) ('2014-12-10', 685) ('2014-11-25', 685) ('2014-10-09', 685) ('2014-09-15', 690) ('2014-08-21', 680) ('2014-07-22', 680)
for first 5 tuples, want assign scores in second position of tuple unique variables. this:
cs0 = [x[1] x in list_cs[0:1]] cs1 = [x[1] x in list_cs[1:2]] cs2 = [x[1] x in list_cs[2:3]] cs3 = [x[1] x in list_cs[3:4]] cs4 = [x[1] x in list_cs[4:5]]
i want assign score in second position of last tuple unique variable. never know how long list might be; want last one. but, can't figure out how last one.
i've tried of following none of them work.
cs1st = [x[1] x in list_cs[-1:-0]] cs1st = [x[1] x in list_cs[-0:-1]] cs1st = [x[1] x in list_cs[:-1]] cs1st = [x[1] x in list_cs[-1]]
how can this?
you don't need list comprehension fetch 1 item.
cs0 = list_cs[0][1] cs1 = list_cs[1][1] …
or, generalize,
cs0, cs1, cs2, cs3, cs4 = [list_cs[i][1] in range(5)]
to last score:
cs1st = list_cs[-1][1]
Comments
Post a Comment