python - Initialise list correctly -
i reading file line line. each line read in such gets split list (of length 13) contains sublists (of arbitrary length (usually between 1 , four). mandated data structure of file reading.
i carry out check on each line see if contains desired data , want save if does.
it saved list (called results
) containing 13 fields, each again containing several lists obtain structure following: results[0] = [line1[0], line2[0], ...]; results[1] = [line1[1], line2[1], ...]; ...; results[12] = [line1[12], line2[12], ...]
(results[i][j]
list of length 1 containing string).
i solve by:
results = [] in line: results[i].append(line[i])
to though list results
needs initialised first can't manually don't know if first line contains entry want save priori , don't want initialise random values have delete later deleting first entry in list quite inefficient far know. how solve problem?
if know need hold 13 sub-lists, create 13 empty lists:
results = [[] _ in range(13)]
note: cannot do:
results = [[]] * 13
because mirror reference single list 13 times; mutating entry mutate of them. see this answer more details.
Comments
Post a Comment