我正在处理大数据,因此找到一种阅读数据的好方法非常重要.我对不同的阅读方法感到有些困惑.
1.f=gzip.open(file,'r')
for line in f:
process line
#how can I process nth line? can I?
2.f=gzip.open(file,'r').readlines()
#f is a list
f[10000]
#we can process nth line
3.f=gzip.open(file,'r')
while True:
linelist=list(islice(f,4))
4.for line in fileinput.input():
process line
Run Code Online (Sandbox Code Playgroud)
2和3有什么区别?我发现他们的内存使用情况是一样的.islice()还需要先将整个文件加载到内存中(但稍后才会逐位加载).而且我听说第四种方法消耗的内存最少,它实际上是一点一点地处理,对吧?对于10GB级文件,您会推荐哪种文件读取方法?欢迎任何想法/信息.谢谢
编辑:我认为我的一个问题是我需要随机选择特定的行.说:
f1=open(inputfile1, 'r')
while True:
line_group1 = list(islice(f1, 3))
if not line_group1:
break
#then process specific lines say, the second line.
processed 2nd line
if ( ....):
LIST1.append(line_group1[0])
LIST1.append(processed 2nd line)
LIST1.append(line_group1[2])
Run Code Online (Sandbox Code Playgroud)
然后...... 喜欢
with open(file,'r') as f,
for line in f: …
Run Code Online (Sandbox Code Playgroud) 我是熊猫的新来者,对于像这样的数据框:
N Chem Val
A Sodium 9
B Sodium 10
A Chlorid 7
B Chlorid 10
A Sodium 17
Run Code Online (Sandbox Code Playgroud)
我想像grep
在bash中一样选择包含'A'
在第一列和'Sodium'
第三列中的行:
A Sodium 9
A Sodium 17
Run Code Online (Sandbox Code Playgroud)
我应该怎么做?我想我需要使用df[].str.contains()
?谢谢
我有一个包含8列的列表,其中前6列是相同的.
6 99999715 99999771 NM_001013399 0 - 23 0.0714286
6 99999715 99999771 NM_001013399 0 - 24 0.0178571
6 99999715 99999771 NM_001013399 0 - 25 0.1250000
Run Code Online (Sandbox Code Playgroud)
我需要计算第7列和第8列的平均值,以及$ 7*$ 8,并获得如下格式:
6 99999715 99999771 NM_001013399 0 - ave($7) ave($8) ave($7*$8)
Run Code Online (Sandbox Code Playgroud)
我该怎么做?谢谢
我有两种方法来表示Python对象json.dumps()
第一的:
person = {
"name": "John",
"age": 30,
"city": "New York"
}
Run Code Online (Sandbox Code Playgroud)
第二:
class Person:
def _init_(self, name, age, city):
self.name = name
self.age = age
self.city = city
person = Person("John", 30, "New York")
Run Code Online (Sandbox Code Playgroud)
然后我就尝试了p1 = json.dumps(person)
,第二种方式就说it's not JSON serializable
。
那么基本上对于Python来说,json.dumps只适用于像dict对象这样的内置对象?
其实很简单的问题:我有一个python列表,如:
['1','2','3','4']
Run Code Online (Sandbox Code Playgroud)
只是想知道如何剥离那些单引号?我想要[1,2,3,4]
我有太多的相关特征(~100),导致分辨率低。我怎样才能提高分辨率?
sns.heatmap(Feature_corr, cbar = True, square = True, annot=False,annot_kws={'size': 15}, cmap= 'coolwarm')
Run Code Online (Sandbox Code Playgroud) 这是我长久以来的困惑。如果多个进程同时将相同的记录写入同一个数据库表中,它们会发生冲突吗?
类似的问题,例如,一台主机不断向许多其他机器分发请求,例如向机器 A 的表中插入一条记录。不知何故,此操作在机器 A 上非常慢,并且主控制器将向机器 B 重新发送完全相同的请求。那么会发生什么呢?会有冲突吗?
我需要处理超大的txt输入文件,我通常使用.readlines()来首先读取整个文件,并将其转换为列表.
我知道这确实是内存成本,而且可能很慢,但我还需要利用LIST特性来操作特定的行,如下所示:
#!/usr/bin/python
import os,sys
import glob
import commands
import gzip
path= '/home/xxx/scratch/'
fastqfiles1=glob.glob(path+'*_1.recal.fastq.gz')
for fastqfile1 in fastqfiles1:
filename = os.path.basename(fastqfile1)
job_id = filename.split('_')[0]
fastqfile2 = os.path.join(path+job_id+'_2.recal.fastq.gz')
newfastq1 = os.path.join(path+job_id+'_1.fastq.gz')
newfastq2 = os.path.join(path+job_id+'_2.fastq.gz')
l1= gzip.open(fastqfile1,'r').readlines()
l2= gzip.open(fastqfile2,'r').readlines()
f1=[]
f2=[]
for i in range(0,len(l1)):
if i % 4 == 3:
b1=[ord(x) for x in l1[i]]
ave1=sum(b1)/float(len(l1[i]))
b2=[ord(x) for x in str(l2[i])]
ave2=sum(b2)/float(len(l2[i]))
if (ave1 >= 20 and ave2>= 20):
f1.append(l1[i-3])
f1.append(l1[i-2])
f1.append(l1[i-1])
f1.append(l1[i])
f2.append(l2[i-3])
f2.append(l2[i-2])
f2.append(l2[i-1])
f2.append(l2[i])
output1=gzip.open(newfastq1,'w')
output1.writelines(f1)
output1.close()
output2=gzip.open(newfastq2,'w') …
Run Code Online (Sandbox Code Playgroud) 尝试将字符串转换为数字矢量,
### Clean the string
def names_to_words(names):
print('a')
words = re.sub("[^a-zA-Z]"," ",names).lower().split()
print('b')
return words
### Vectorization
def Vectorizer():
Vectorizer= CountVectorizer(
analyzer = "word",
tokenizer = None,
preprocessor = None,
stop_words = None,
max_features = 5000)
return Vectorizer
### Test a string
s = 'abc...'
r = names_to_words(s)
feature = Vectorizer().fit_transform(r).toarray()
Run Code Online (Sandbox Code Playgroud)
但是当我陶醉时:
['g', 'o', 'm', 'd']
Run Code Online (Sandbox Code Playgroud)
有错误:
ValueError: empty vocabulary; perhaps the documents only contain stop words
Run Code Online (Sandbox Code Playgroud)
这样的单字母字符串似乎存在问题。我该怎么办?谢谢
我总是对 Java Collections (set, map) 删除“复杂对象”感到困惑,我的意思是一些自定义类而不仅仅是原始类型。
我正在尝试:
public class Main {
public static void main(String[] args) {
// set
Set<Node> set = new HashSet<>();
set.add(new Node(1,2));
set.add(new Node(3,4));
System.out.println(set);
set.remove(new Node(1,2));
System.out.println(set + "\n");
// tree set
TreeSet<Node> tset = new TreeSet<>((a, b) -> a.name - b.name);
tset.add(new Node(1,2));
tset.add(new Node(3,4));
System.out.println(tset);
tset.remove(new Node(1,2));
System.out.println(tset);
}
}
class Node {
int name;
int price;
Node(int name, int price) {
this.name = name;
this.price = price;
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,打印输出将是:
Set:
[Node@5ba23b66, …
Run Code Online (Sandbox Code Playgroud)