Select 每个列表中的一个随机项目嵌套在一个列表中,它们之间不同,并生成一个列表 n 次(重采样)
Select one random item from each list nested in a list, different between them, and generate a list n times (RESAMPLING)
我有一个 txt 文件,其中的基因代码由类似于此结构的制表符分隔:
ENSG00000111111 ENSG00000111111 ENSG00000111111 ENSG00000111555
ENSG00000111111 ENSG00000111111 ENSG00000111111 ENSG00000111222
ENSG00000111111 ENSG00000111111 ENSG00000111111 ENSG00000333555
并且我想创建一个列表,其中从每一行中随机选择一项并且所选项目必须不同。最后我想重复这个过程n次以获得具有这种结构的输出文件:
ENSG00000111111 ENSG00000111222 ENSG00000333555
ENSG00000111555 ENSG00000111222 ENSG00000333555
ENSG00000111555 ENSG00000111222 ENSG00000111111
...
(每行对应随机生成的每个项目列表)
.目前我有这个脚本:
其中:all_cand 是 txt 输入文件
#!/usr/bin/python
import sys
import os
import random
from itertools
import numpy as np
def rand_cand (all_cand):
cand_list= []
main_list = []
cand_file= open(all_cand, "r")
for _ in itertools.repeat(None, 10):
for line in cand_file:
cand_rows = line.split()
cand_list.append(cand_rows)
for item in cand_list:
aux_old = np.random.choice(item, replace=False)
if not aux_old in main_list:
main_list.append(aux_old)
else:
aux_new = np.random.choice(item, replace=False)
main_list.append(aux_new)
print(main_list)
与我的脚本相关,每个生成的列表都包含重复,我认为这是由于 If 循环。我尝试将要附加到列表中的每个项目与已经存储但失败的项目进行比较......所以我的错误输出之一是:
ENSG00000111111 ENSG00000111111 ENSG00000111111
ENSG00000111111 ENSG00000111111 ENSG00000111222
ENSG00000111111 ENSG00000111111 ENSG00000111111
ENSG00000111555 ENSG00000111111 ENSG00000111111
...
先谢谢了!希望能把我的问题解释清楚
我还没有测试过,但这应该可以了
import csv
import itertools
import random
with open('file.txt') as infile:
data = list(csv.reader(infile, delimiter='\t'))
answer = []
combos = list(itertools.combinations_with_replacement([0,1,2,3], 3))
for _ in range(n):
selection = []
while len(set(selection)) != 3:
inds = random.choice(combos)
rows = random.sample(data, 3)
selection = [row[i] for i,row in zip(inds, rows)]
answer.append(selection)
我有一个 txt 文件,其中的基因代码由类似于此结构的制表符分隔:
ENSG00000111111 ENSG00000111111 ENSG00000111111 ENSG00000111555
ENSG00000111111 ENSG00000111111 ENSG00000111111 ENSG00000111222
ENSG00000111111 ENSG00000111111 ENSG00000111111 ENSG00000333555
并且我想创建一个列表,其中从每一行中随机选择一项并且所选项目必须不同。最后我想重复这个过程n次以获得具有这种结构的输出文件:
ENSG00000111111 ENSG00000111222 ENSG00000333555
ENSG00000111555 ENSG00000111222 ENSG00000333555
ENSG00000111555 ENSG00000111222 ENSG00000111111
...
(每行对应随机生成的每个项目列表) .目前我有这个脚本: 其中:all_cand 是 txt 输入文件
#!/usr/bin/python
import sys
import os
import random
from itertools
import numpy as np
def rand_cand (all_cand):
cand_list= []
main_list = []
cand_file= open(all_cand, "r")
for _ in itertools.repeat(None, 10):
for line in cand_file:
cand_rows = line.split()
cand_list.append(cand_rows)
for item in cand_list:
aux_old = np.random.choice(item, replace=False)
if not aux_old in main_list:
main_list.append(aux_old)
else:
aux_new = np.random.choice(item, replace=False)
main_list.append(aux_new)
print(main_list)
与我的脚本相关,每个生成的列表都包含重复,我认为这是由于 If 循环。我尝试将要附加到列表中的每个项目与已经存储但失败的项目进行比较......所以我的错误输出之一是:
ENSG00000111111 ENSG00000111111 ENSG00000111111
ENSG00000111111 ENSG00000111111 ENSG00000111222
ENSG00000111111 ENSG00000111111 ENSG00000111111
ENSG00000111555 ENSG00000111111 ENSG00000111111
...
先谢谢了!希望能把我的问题解释清楚
我还没有测试过,但这应该可以了
import csv
import itertools
import random
with open('file.txt') as infile:
data = list(csv.reader(infile, delimiter='\t'))
answer = []
combos = list(itertools.combinations_with_replacement([0,1,2,3], 3))
for _ in range(n):
selection = []
while len(set(selection)) != 3:
inds = random.choice(combos)
rows = random.sample(data, 3)
selection = [row[i] for i,row in zip(inds, rows)]
answer.append(selection)