这是我需要处理的txt文件:
chr8 148401 153100 duplication
chr8 206001 207100 deletion
chr8 584401 589500 deletion
chr8 615101 616600 deletion
chr8 842601 843200 deletion
chr8 868901 869700 deletion
Run Code Online (Sandbox Code Playgroud)
基本上我想提取两个数字,并做减法.我的代码如下:
#!/usr/bin/python
import os,sys
file = open('/home/xxx/sge_jobs_output/rCEU.bed','r')
for line in file.readlines():
num1 = line.split()[1].split()[0]
num2 = line.split()[1].split()[1].split()[0]
num = int(num2)-int(num1)
print num
Run Code Online (Sandbox Code Playgroud)
我可以成功打印出num1; 但是num2不起作用.所以我们不能连续使用两个以上的.split?
而错误就像:
Traceback (most recent call last):
File "CNV_length_cal.py", line 8, in <module>
num2 = line.split()[1].split()[1].split()[0]
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)
这有什么不对?我对.split命令感到很困惑......但是我找不到关于那个...的教程
这些天我一直在操纵巨大的文本文件.有时我需要删除行.我的做法如下:
f=open('txt','r').readlines()
list=[]
for line in f:
if blablablabla:
list.append(line)
Run Code Online (Sandbox Code Playgroud)
我知道对于大文件,.readlines()是速率限制步骤,但是.append()步骤呢?在readlines之后追加会花费多少额外的时间吗?如果是这样,也许我应该找到直接删除我不想要的行的方法,而不是追加我想要的行.
谢谢
例如,我创建了一组列表:
LIST_chr1=[]
LIST_chr2=[]
LIST_chr3=[]
...
...
Run Code Online (Sandbox Code Playgroud)
现在在处理文本时,对于某一行,如果我得到"chr2",那么我需要使用LIST_chr2这样选择这个列表,我应该如何根据列表名称指向这个特定列表?
谢谢
例如,我有:
dic={'a': 1, 'b': 2, 'c': 3}
Run Code Online (Sandbox Code Playgroud)
现在我想要另一个'c':4添加到字典中。它将覆盖现有的'c':3.
我怎么会dic这样:
dic={'a': 1, 'b': 2, 'c': 3, 'c':4}
Run Code Online (Sandbox Code Playgroud) 传统代码运行良好,如下所示:
Map<Integer, List<Integer>> map = new HashMap<>();
if (!map.containsKey(1)) {
map.put(1, new ArrayList<>());
}
map.get(1).add(2);
Run Code Online (Sandbox Code Playgroud)
现在我想尝试一下 getOrDefault 的神奇之处:
map.getOrDefault(1, new ArrayList<>()).add(2);
Run Code Online (Sandbox Code Playgroud)
但是如果我使用上面的行,map.get(1)则为空。
为什么?