Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从python列表中的文本文件中获取匹配列_Python_Numpy_Pandas_Delimiter - Fatal编程技术网

从python列表中的文本文件中获取匹配列

从python列表中的文本文件中获取匹配列,python,numpy,pandas,delimiter,Python,Numpy,Pandas,Delimiter,我有一个文本文件,看起来像: (来自ipython) cat路径\u到\u文件 0 0.25 truth fact 1 0.25 train home find travel ........ 199 0.25 video box store office 我还有一张单子 vec = [(76, 0.04334748761500331), (128, 0.03697806086341099), (81, 0.03131634819532892), (1, 0.

我有一个文本文件,看起来像: (来自ipython) cat路径\u到\u文件

0   0.25    truth fact 
1   0.25    train home find travel
........
199 0.25    video box store office
我还有一张单子

vec = [(76, 0.04334748761500331),
 (128, 0.03697806086341099),
 (81, 0.03131634819532892),
 (1, 0.03131634819532892)]
现在,我只想从vec中获取匹配的第一列和文本文件的第一列,并显示vec的1,2列和文本文件的第3列作为我的输出

若我有和vec相同格式的文本文件,我可以使用set(a)和set(b)。 但测试文件中的值是用制表符隔开的(这是执行以下操作时的样子)

打开(路径到文件)为f时: lines=f.read().splitlines()

输出为:

['0\t0.25\ttruth fact lie
.........................
 '198\t0.25\tfan genre bit enjoy ',
 '199\t0.25\tvideo box store office  ']

将vec转换为
dict
,并使用
“\t”
作为分隔符拆分行应该可以:

vecdict = dict(vec)

output = []
for l in open('path_to_file'):
    words = l.split('\t')
    key = float(words[0])
    if vecdict.has_key(key):
        output.append("%s %f %s"%(words[0], vecdict[key], ' '.join(words[2:])) )
输出
应该是字符串列表

PS:如果您有多个分隔符,或者不确定是哪个分隔符,您可以重复调用
split
,或者
re
,例如

print re.findall("[\w]+", "this has    multiple delimiters\tHere")

>> ["this", "has", "multiple", "delimiters", "Here"]

将vec转换为
dict
,并使用
“\t”
作为分隔符拆分行应该可以:

vecdict = dict(vec)

output = []
for l in open('path_to_file'):
    words = l.split('\t')
    key = float(words[0])
    if vecdict.has_key(key):
        output.append("%s %f %s"%(words[0], vecdict[key], ' '.join(words[2:])) )
输出
应该是字符串列表

PS:如果您有多个分隔符,或者不确定是哪个分隔符,您可以重复调用
split
,或者
re
,例如

print re.findall("[\w]+", "this has    multiple delimiters\tHere")

>> ["this", "has", "multiple", "delimiters", "Here"]

将vec转换为
dict
,并使用
“\t”
作为分隔符拆分行应该可以:

vecdict = dict(vec)

output = []
for l in open('path_to_file'):
    words = l.split('\t')
    key = float(words[0])
    if vecdict.has_key(key):
        output.append("%s %f %s"%(words[0], vecdict[key], ' '.join(words[2:])) )
输出
应该是字符串列表

PS:如果您有多个分隔符,或者不确定是哪个分隔符,您可以重复调用
split
,或者
re
,例如

print re.findall("[\w]+", "this has    multiple delimiters\tHere")

>> ["this", "has", "multiple", "delimiters", "Here"]

将vec转换为
dict
,并使用
“\t”
作为分隔符拆分行应该可以:

vecdict = dict(vec)

output = []
for l in open('path_to_file'):
    words = l.split('\t')
    key = float(words[0])
    if vecdict.has_key(key):
        output.append("%s %f %s"%(words[0], vecdict[key], ' '.join(words[2:])) )
输出
应该是字符串列表

PS:如果您有多个分隔符,或者不确定是哪个分隔符,您可以重复调用
split
,或者
re
,例如

print re.findall("[\w]+", "this has    multiple delimiters\tHere")

>> ["this", "has", "multiple", "delimiters", "Here"]
使用NumPy:

import numpy as np
import numpy.lib.recfunctions as rfn

dtype = [('index', int), ('text', object)]
table = np.loadtxt(path_to_file, dtype=dtype, usecols=(0,2), delimiter='\t')

dtype = [('index', int), ('score', float)]
array = np.array(vec, dtype=dtype)

joined = rfn.join_by('index', table, array)

for row in joined:
      print row['index'], row['score'], row['text']
如果您非常关心性能,您也可以使用
np.savetxt()
进行输出,但我认为这样更容易理解。

使用NumPy:

import numpy as np
import numpy.lib.recfunctions as rfn

dtype = [('index', int), ('text', object)]
table = np.loadtxt(path_to_file, dtype=dtype, usecols=(0,2), delimiter='\t')

dtype = [('index', int), ('score', float)]
array = np.array(vec, dtype=dtype)

joined = rfn.join_by('index', table, array)

for row in joined:
      print row['index'], row['score'], row['text']
如果您非常关心性能,您也可以使用
np.savetxt()
进行输出,但我认为这样更容易理解。

使用NumPy:

import numpy as np
import numpy.lib.recfunctions as rfn

dtype = [('index', int), ('text', object)]
table = np.loadtxt(path_to_file, dtype=dtype, usecols=(0,2), delimiter='\t')

dtype = [('index', int), ('score', float)]
array = np.array(vec, dtype=dtype)

joined = rfn.join_by('index', table, array)

for row in joined:
      print row['index'], row['score'], row['text']
如果您非常关心性能,您也可以使用
np.savetxt()
进行输出,但我认为这样更容易理解。

使用NumPy:

import numpy as np
import numpy.lib.recfunctions as rfn

dtype = [('index', int), ('text', object)]
table = np.loadtxt(path_to_file, dtype=dtype, usecols=(0,2), delimiter='\t')

dtype = [('index', int), ('score', float)]
array = np.array(vec, dtype=dtype)

joined = rfn.join_by('index', table, array)

for row in joined:
      print row['index'], row['score'], row['text']


如果您非常关心性能,您也可以使用
np.savetxt()
来执行输出,但我认为这样更容易理解。

您可以指定要与
拆分一起使用的分隔符,例如
拆分(“\t”)
然后将第一个元素与向量进行比较您可以指定用于
拆分的分隔符,例如
拆分(“\t”)
然后将第一个元素与向量进行比较您可以指定用于
拆分的分隔符,例如
拆分(“\t”)
然后将第一个元素与向量进行比较您可以指定用于
拆分的分隔符,例如
拆分(“\t”)
,然后将第一个元素与向量进行比较。虽然有效,但输出是单个字符串,值之间没有空格:['90.0256546355272dark light\n']。我试图得到的值是90 0.25字符串。此外,此dict函数会自动对vec进行排序。是他们在dict中保持列表未排序的方式。您是否使用了我编辑的版本(我在大约6分钟前更新了代码,在第一个和第二个字段之间添加了空格)是的,有效。我又加了一个空格
output.append(words[0]+'+str(vecdict[key])++'+''.join(words[2:])
Yeah刚才也发现了这一点-切换到使用字符串格式(请参阅上次更新)以删除笨拙的字符串连接。谢谢。虽然有效,但输出是单个字符串,值之间没有空格:['90.0256546355272dark light\n']。我试图得到的值是90 0.25字符串。此外,此dict函数会自动对vec进行排序。是他们在dict中保持列表未排序的方式。您是否使用了我编辑的版本(我在大约6分钟前更新了代码,在第一个和第二个字段之间添加了空格)是的,有效。我又加了一个空格
output.append(words[0]+'+str(vecdict[key])++'+''.join(words[2:])
Yeah刚才也发现了这一点-切换到使用字符串格式(请参阅上次更新)以删除笨拙的字符串连接。谢谢。虽然有效,但输出是单个字符串,值之间没有空格:['90.0256546355272dark light\n']。我试图得到的值是90 0.25字符串。此外,此dict函数会自动对vec进行排序。是他们在dict中保持列表未排序的方式。您是否使用了我编辑的版本(我在大约6分钟前更新了代码,在第一个和第二个字段之间添加了空格)是的,有效。我又加了一个空格
output.append(words[0]+'+str(vecdict[key])++'+''.join(words[2:])
Yeah刚才也发现了这一点-切换到使用字符串格式(请参阅上次更新)以删除笨拙的字符串连接。谢谢。虽然有效,但输出是单个字符串,值之间没有空格:['90.0256546355272dark light\n']。我试图得到的值是90 0.25字符串。此外,此dict函数会自动对vec进行排序。是他们在dict中保持列表未排序的方式。您是否使用了我编辑的版本(我在大约6分钟前更新了代码,在第一个和第二个字段之间添加了空格)是的,有效。我又加了一个空格
output.append(words[0]+'+str(vecdict[key])++'+''.join(words[2:])
Yeah刚刚也发现了这一点-切换到使用字符串格式(请参阅上次更新)来删除冗杂的字符串连接。工作正常!是否可以保持原始的vec顺序。使用上面的代码,输出按第一列的asc顺序排序。当然,请尝试
order=np.searchsorted(表['index'],数组['index'])