[英]How to save for loop outputs as a list
mutate1:变异1:
Hugo_Symbol Start_position Tumor_Seq_Allele1 Variant_Classification
5 POU3F1 38512139 G Missense_Mutation
356140 POU3F1 38511502 C Missense_Mutation
388147 POU3F1 38511377 A Nonsense_Mutation
I tried我试过
>>> startpos = np.zeros(3)
>>> for ind in mutate1.index:
... for i in range(3):
... startpos[i] = int(mutate1['Start_position'][ind]-1)
... print(startpos)
...
[38512138. 0. 0.]
[38512138. 38512138. 0.]
[38512138. 38512138. 38512138.]
[38511501. 38512138. 38512138.]
[38511501. 38511501. 38512138.]
[38511501. 38511501. 38511501.]
[38511376. 38511501. 38511501.]
[38511376. 38511376. 38511501.]
[38511376. 38511376. 38511376.]
However, I want startpos = [38512138, 38511501, 38511376], how should I change the current code?但是,我想要 startpos = [38512138, 38511501, 38511376],我应该如何更改当前代码?
Don't iterate over DataFrames when it isn't needed .不需要时不要迭代 DataFrames 。 Use tolist()
in a list comprehension:在列表tolist()
使用tolist()
:
startpos = [i-1 for i in mutate1["Start_position"].tolist()]
Assuming you are using a Pandas DataFrame, which is what it seems, it is bad practice to iterate through one.假设您使用的是 Pandas DataFrame,看起来是这样,迭代一个数据帧是不好的做法。 There is an inbuilt pandas function called to_list
有一个名为to_list
的内置to_list
函数
So just use startpos = mutate1['Start_position'].to_list()
所以只需使用startpos = mutate1['Start_position'].to_list()
To subtract one value use list comprehension startpos = [x - 1 for x in startpos]
要减去一个值,请使用列表理解startpos = [x - 1 for x in startpos]
Then convert the python list to a numpy array by startpos = np.array(startpos)
然后通过startpos = np.array(startpos)
将 python 列表转换为 numpy 数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.