Created
September 17, 2014 00:34
-
-
Save brantfaircloth/fef13aa6e2387a8ac3c0 to your computer and use it in GitHub Desktop.
shuffle population labels in a csv file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from random import shuffle | |
# create a list of all labels - NOTE that i've used 2 here because we have | |
# 2 individuals in pop A and 2 in pop B | |
label_list = ["population-A"] * 2 + ["population-B"] * 2 | |
# do something 100 times | |
for i in xrange(100): | |
# you can remove this | |
print "Draw # {}\n".format(i) | |
# shuffle the list in place (meaning there is no return - the list is literately shuffled) | |
shuffle(label_list) | |
# open a csv file (no header) | |
with open("test.csv", "rU") as infile: | |
# for each line in the file | |
for line_number, line in enumerate(infile): | |
# strip the newline at the end and split the line on commas | |
line_split = line.strip().split(",") | |
# replace the 2nd item in the list with a selection from the shuffled labels | |
line_split[1] = label_list[line_number] | |
# glue all that stuff back together | |
new_line = ",".join(line_split) | |
# print it out (this should be replaced by writing to an output file | |
print new_line | |
print "\n" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
individual-1 | population-A | 00 | 11 | 10 | 01 | 11 | 00 | |
---|---|---|---|---|---|---|---|---|
individual-2 | population-A | 00 | 10 | 10 | 01 | 11 | 00 | |
individual-3 | population-B | 11 | 10 | 10 | 01 | 11 | 00 | |
individual-4 | population-B | 11 | 10 | 10 | 01 | 11 | 00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Expected output is something like the following: