Python将字符串解析为文件中的浮点列表的有效方法
这个文档每行有一个单词和上万个浮点数,我想把它转换成一个字典,用这个单词作为键,用一个向量来表示所有浮点数。 我就是这样做的,但由于文件的大小约为20k行,每个行的值约为10k,因此该过程花费的时间有点太长。我找不到更有效的解析方法。只是一些不能保证减少运行时间的替代方法Python将字符串解析为文件中的浮点列表的有效方法,python,performance,parsing,Python,Performance,Parsing,这个文档每行有一个单词和上万个浮点数,我想把它转换成一个字典,用这个单词作为键,用一个向量来表示所有浮点数。 我就是这样做的,但由于文件的大小约为20k行,每个行的值约为10k,因此该过程花费的时间有点太长。我找不到更有效的解析方法。只是一些不能保证减少运行时间的替代方法 with open("googlenews.word2vec.300d.txt") as g_file: i = 0; #dict of words: [lots of floats] google_words =
with open("googlenews.word2vec.300d.txt") as g_file:
i = 0;
#dict of words: [lots of floats]
google_words = {}
for line in g_file:
google_words[line.split()[0]] = [float(line.split()[i]) for i in range(1, len(line.split()))]
只是不要打电话。分开不止一次
with open("googlenews.word2vec.300d.txt") as g_file:
i = 0;
#dict of words: [lots of floats]
google_words = {}
for line in g_file:
temp = line.split()
google_words[temp[0]] = [float(temp[i]) for i in range(1, len(temp))]
以下是此类文件的简单生成器:
s = "x"
for i in range (10000):
s += " 1.2345"
print (s)
前一个版本需要一些时间。
只有一次拆分调用的版本是即时的。只需多次调用line.split即可
with open("googlenews.word2vec.300d.txt") as g_file:
i = 0;
#dict of words: [lots of floats]
google_words = {}
for line in g_file:
temp = line.split()
google_words[temp[0]] = [float(temp[i]) for i in range(1, len(temp))]
以下是此类文件的简单生成器:
s = "x"
for i in range (10000):
s += " 1.2345"
print (s)
前一个版本需要一些时间。
只有一个拆分调用的版本是即时的。在您的解决方案中,您可以为每个单词执行慢速行。拆分两次。考虑以下修改:
with open("googlenews.word2vec.300d.txt") as g_file:
i = 0;
#dict of words: [lots of floats]
google_words = {}
for line in g_file:
word, *numbers = line.split()
google_words[word] = [float(number) for number in numbers]
我在这里使用的一个高级概念是解包:
字,*number=line.split
Python允许将iterable值解压为多LPLE变量:
a, b, c = [1, 2, 3]
# This is practically equivalent to
a = 1
b = 2
c = 3
*是一个快捷方式,用于获取剩菜,将其放入列表并将列表指定给名称:
a, *rest = [1, 2, 3, 4]
# results in
a == 1
rest == [2, 3, 4]
在您的解决方案中,您为每个单词执行慢行分割,两次。考虑以下修改:
with open("googlenews.word2vec.300d.txt") as g_file:
i = 0;
#dict of words: [lots of floats]
google_words = {}
for line in g_file:
word, *numbers = line.split()
google_words[word] = [float(number) for number in numbers]
我在这里使用的一个高级概念是解包:
字,*number=line.split
Python允许将iterable值解压为多LPLE变量:
a, b, c = [1, 2, 3]
# This is practically equivalent to
a = 1
b = 2
c = 3
*是一个快捷方式,用于获取剩菜,将其放入列表并将列表指定给名称:
a, *rest = [1, 2, 3, 4]
# results in
a == 1
rest == [2, 3, 4]
你也可以使用这个模块,它应该比你正在做的更有效率
可能是这样的:
import csv
d = {}
with (open("huge_file_so_huge.txt", "r")) as g_file:
for row in csv.reader(g_file, delimiter=" "):
d[row[0]] = list(map(float, row[1:]))
你也可以使用这个模块,它应该比你正在做的更有效率
可能是这样的:
import csv
d = {}
with (open("huge_file_so_huge.txt", "r")) as g_file:
for row in csv.reader(g_file, delimiter=" "):
d[row[0]] = list(map(float, row[1:]))
更好地定义花费的时间有点太长-需要多长时间,现实一点,您希望花费多长时间?我可能会调用line.split一次,并将其分配给变量,而不是将该调用保留在列表中。这样,您就可以详细地重复它,正如我要说的……更好地定义需要花费的时间有点太长-需要多长时间,并且现实地说,您希望花费多长时间?我可能会调用line.split一次,并将其分配给一个变量,而不是将该调用保留在列表中。这样你就可以详细地重复我要说的…谢谢!现在效果好多了。但有一个问题是,数字中的*在做什么?它是否是C、C++中的指针?@ VisturZuaNaZiZo,Python没有内置指针;我在更新的答案中解释了这个语法糖。谢谢!现在效果好多了。但有一个问题是,数字中的*在做什么?它是否是C、C++中的指针?@ VisturZuaNaZiZo,Python没有内置指针;我在最新的答案中解释了这个语法糖。