Skip to content

Instantly share code, notes, and snippets.

@graham
Last active August 29, 2015 14:11
Show Gist options
  • Save graham/98e47f064b09ee44047a to your computer and use it in GitHub Desktop.
Save graham/98e47f064b09ee44047a to your computer and use it in GitHub Desktop.
Deconstruct the oneliner
oneliner = 'output.writerow(map(lambda x: x[1], filter(lambda x: True if x[0] else False, zip(filter_map,line))))'
# First, we zip two lists together, in this case we take something like:
# x = [ {}, {}, {'one':1} ]
# y = [ [one, one, one], [two, two, two], [three, three, three] ]
# and turn it into:
# z = [ ({}, [one, one, one]), ({}, [two, two, two]), ({'one':1}, [three, three, three]) ]
test_list = zip(filter_map, line)
# then we want to exclude the items in that list where the first item is falsy.
# filter( lambda x: True if x[0] else False, test_list)
# but we can do that another way.
new_test_list = []
# di = the dictionary, line_data = a line of data from the file we are reading
for di, data_line in test_list:
if di:
# We only keep the data_line around because that's all we care about now
new_test_list.append(data_line)
output.writerow(new_test_list)
#boom, row [ [three, three, three] ] is written to file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment