python - converting types of repeated list elements -



python - converting types of repeated list elements -

i have list of lists , know each type of elements [str, str, str, int, int, int, str, int]. have convert function guesses type:

def convert(val): constructors = [int, str] c in constructors: try: homecoming c(val) except valueerror: pass

how possible replace convert function, because know type of each element (please see below total code)?

from __future__ import absolute_import, division, print_function itertools import groupby info = [["test", "a", "b01", 828288, 1, 7, 'c', 5], ["test", "a", "b01", 828288, 1, 7, 't', 6], ["test", "a", "b01", 171878, 3, 7, 'c', 5], ["test", "a", "b01", 171878, 3, 7, 't', 6], ["test", "a", "b01", 871963, 3, 9, 'a', 5], ["test", "a", "b01", 871963, 3, 9, 'g', 6], ["test", "a", "b01", 1932523, 1, 10, 't', 4], ["test", "a", "b01", 1932523, 1, 10, 'a', 5], ["test", "a", "b01", 1932523, 1, 10, 'x', 6], ["test", "a", "b01", 667214, 1, 14, 't', 4], ["test", "a", "b01", 667214, 1, 14, 'g', 5], ["test", "a", "b01", 667214, 1, 14, 'g', 6]] def convert(val): constructors = [int, str] c in constructors: try: homecoming c(val) except valueerror: pass def main(): open("/home/mic/tmp/test.txt") f: line in f: try: info = [convert(part.strip()) part in line.split(',')] print(data) except indexerror: go on

update give thanks responses have given me new ideas , hence modified code (method 1 - 4), not working:

#!/usr/bin/env python __future__ import absolute_import, division, print_function itertools import groupby import csv parts = [["test", "a", "b01", 828288, 1, 7, 'c', 5], ["test", "a", "b01", 828288, 1, 7, 't', 6], ["test", "a", "b01", 171878, 3, 7, 'c', 5], ["test", "a", "b01", 171878, 3, 7, 't', 6], ["test", "a", "b01", 871963, 3, 9, 'a', 5], ["test", "a", "b01", 871963, 3, 9, 'g', 6], ["test", "a", "b01", 1932523, 1, 10, 't', 4], ["test", "a", "b01", 1932523, 1, 10, 'a', 5], ["test", "a", "b01", 1932523, 1, 10, 'x', 6], ["test", "a", "b01", 667214, 1, 14, 't', 4], ["test", "a", "b01", 667214, 1, 14, 'g', 5], ["test", "a", "b01", 667214, 1, 14, 'g', 6]] def iter_something(rows): key_names = ['type', 'name', 'sub_name', 'pos', 's_type', 'x_type'] chr_key_names = ['letter', 'no'] keys, grouping in groupby(rows, lambda row: row[:6]): result = dict(zip(key_names, keys)) result['chr'] = [dict(zip(chr_key_names, row[6:])) row in group] yield result def main(): #method 1 converters = [str, str, str, int, int, int, str, int] open("/home/mic/tmp/test.txt") f: parts = (line.strip().split(',') line in f) column = (con(part) con, part in zip(converters, parts)) object_ in iter_something(column): print(object_) #method 2 open("/home/mic/tmp/test.txt") f: parts = (line.strip().split(',') line in f) parts[3], parts[4], parts[5], parts[7] = int(parts[3]),\ int(parts[4]),\ int(parts[5]),\ int(parts[7]) column = (con(part) con, part in zip(converters, parts)) object_ in iter_something(column): print(object_) #method 3 converters = [str, str, str, int, int, int, str, int] open("/home/mic/tmp/test.txt", 'rb') f: reader = csv.reader(f, skipinitialspace=true) object_ in iter_something(reader): print(object_) #method 4 open("/home/mic/tmp/test.txt", 'rb') f: reader = csv.reader(f, skipinitialspace=true) reader[3], reader[4], reader[5], reader[7] = int(reader[3]),\ int(reader[4]),\ int(reader[5]),\ int(reader[7]) object_ in iter_something(reader): print(object_) if __name__ == '__main__': main()

you utilize zip() pair type column:

converters = [str, str, str, int, int, int, str, int] line in f: info = [convert(part.strip()) convert, part in zip(converters, line.split(','))]

in update 1 time again making same error did in other question; getting confused between rows , columns , applying technique rows:

parts = (line.strip().split(',') line in f) column = ([con(col) con, col in zip(converters, row)] row in parts)

can reiterate considering using csv module 1 time more, did previous question? reinventing csv-parsing wheel little here:

with open("/home/mic/tmp/test.txt") f: reader = csv.reader(f, skipinitialspace=true) converted = ([conv(col) conv, col in zip(converters, row)] row in reader)

python python-2.7

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -