Converting a csv file into a list of tuples with python

You can ponder this: import csv def fitem(item): item=item.strip() try: item=float(item) except ValueError: pass return item with open(‘/tmp/test.csv’, ‘r’) as csvin: reader=csv.DictReader(csvin) data={k.strip():[fitem(v)] for k,v in reader.next().items()} for line in reader: for k,v in line.items(): k=k.strip() data[k].append(fitem(v)) print data Prints: {‘Price’: [6.05, 8.05, 6.54, 6.05, 7.05, 7.45, 5.45, 6.05, 6.43, 7.05, 8.05, 3.05], ‘Type’: [‘orange’, … Read more

How to use ast.literal_eval in a pandas dataframe and handle exceptions

I would do it simply requiring a string type from each entry: from ast import literal_eval df[‘column_2’] = df.column_1.apply(lambda x: literal_eval(str(x))) If You need to advanced Exception handling, You could do, for example: def f(x): try: return literal_eval(str(x)) except Exception as e: print(e) return [] df[‘column_2’] = df.column_1.apply(lambda x: f(x))

Get all pairs in a list using LINQ

Slight reformulation of cgeers answer to get you the tuples you want instead of arrays: var combinations = from item1 in list from item2 in list where item1 < item2 select Tuple.Create(item1, item2); (Use ToList or ToArray if you want.) In non-query-expression form (reordered somewhat): var combinations = list.SelectMany(x => list, (x, y) => Tuple.Create(x, … Read more