How to read columns from a file and add each column in separate lists in python 3.4 -
i need dynamic code:
if file data looks below, how can add each column of in 3 list separately in python 3.4.1?
0 4 5 1 0 0 1 56 96 i tried , read data file , stored in list like: scores = [['0','4', '5'],['1','0','0], ['1', '56','96']]. don't know how write code put each first letter of array 3 separate lists or arrays. like: list1 = [0, 1,1], list2 = [4,0,56] , list3 = [5,0,96]
thanks
basically, have list of rows, , want list of columns. called transposing , can written concisely in python this:
columns = zip(*scores) after doing this, columns[0] contain first column, columns[1] second column, , on. columns tuples. if need lists can apply list function result:
columns = map(list, zip(*scores)) this dark-magic-looking syntax first uses * operator unpacks list of arguments. here means zip(*scores) equivalent to:
zip(['0','4', '5'], ['1','0','0'], ['1', '56','96']) note how each element of scores list different argument of zip function. use zip function.
Comments
Post a Comment