Assigning variables in loop Python -
i have piece of:
as, bs, cs, ds, es = ([] in range(5)) line in infile: line = line.rstrip() a, b, c, d, e = line.split('\t') += [a] bs += [b] cs += [c] ds += [d] es += [e]
since have many more 5, want loop takes less lines of code writing of out. like:
as, bs, cs, ds, es = ([] in range(5)) dynamic_variable_list = [as, bs, cs, ds, es] line in infile: line = line.rstrip() in range(len(line.split('\t'))): dynamic_variable_list[i] += line.split('\t')[i]
which in case stores individual characters in list, whereas:
dynamic_variable_list[i] += line.split('\t')
stores tab delimited entries each of variables of dynamic_variable_list
. need store of tab delimited entries in separate variables top example shows.
dynamic_variable_list[i] += line.split('\t')[i]
can rewritten as
as +=
rather than
as += [a]
either surround in brackets before, or use append
. or rather, split list , make more clear.
dynamic_variable_list = [as, bs, cs, ds, es] line in infile: columns = line.rstrip().split('\t') assert(len(columns)==len(dynamic_variable_list)) index, col in enumerate(columns): dynamic_variable_list[index].append(col)
Comments
Post a Comment