Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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 在CSV文件中查找多个匹配项对_Python_List_Python 2.7_Csv_Find Occurrences - Fatal编程技术网

Python 在CSV文件中查找多个匹配项对

Python 在CSV文件中查找多个匹配项对,python,list,python-2.7,csv,find-occurrences,Python,List,Python 2.7,Csv,Find Occurrences,我正在尝试编写一个Python脚本,该脚本将搜索CSV文件,并确定两个项目相邻出现的次数 例如,假设CSV如下所示: red,green,blue,red,yellow,green,yellow,red,green,purple,blue,yellow,red,blue,blue,green,purple,red,blue,blue,red,green 我想知道“红、绿”相邻出现的次数(但我想找到一个解决方案,它不仅仅针对CSV中的单词) 到目前为止,我认为将CSV转换为列表可能是一个良好的

我正在尝试编写一个Python脚本,该脚本将搜索CSV文件,并确定两个项目相邻出现的次数

例如,假设CSV如下所示:

red,green,blue,red,yellow,green,yellow,red,green,purple,blue,yellow,red,blue,blue,green,purple,red,blue,blue,red,green 
我想知道“红、绿”相邻出现的次数(但我想找到一个解决方案,它不仅仅针对CSV中的单词)

到目前为止,我认为将CSV转换为列表可能是一个良好的开端:

import csv
with open('examplefile.csv', 'rb') as f:
    reader = csv.reader(f)
    your_list = list(reader)

print your_list
返回:

[['red', 'green', 'blue', 'red', 'yellow', 'green', 'yellow', 'red', 'green', 'purple', 'blue', 'yellow', 'red', 'blue', 'blue', 'green', 'purple', 'red', 'blue', 'blue', 'red', 'green ']]

在这个列表中,有三个出现的
“红色”、“绿色”
-我可以使用什么样的方法/模块/循环结构来确定列表中两个项目是否有一个以上的出现,并且在列表中彼此相邻

这将一次性检查“红色”、“绿色”和“绿色”、“红色”组合:

pair = ('red', 'green')
positions = [i for i in xrange(len(l)-1) if ((l[i],l[i+1]) == pair or (l[i+1],l[i]) == pair)]
print positions
>>> [0, 7] # notice that your last entry was 'green ' not 'green'
输出打印模式开始的第i个索引

使用您的示例进行测试(最后更正为“绿色”):


你要找的东西叫做bigrams(两个单词的一对)。您通常会在文本挖掘/NLP类型的问题中看到这些问题。试试这个:

from itertools import islice, izip
from collections import Counter
print Counter(izip(your_list, islice(your_list, 1, None)))
返回:

计数器({('red','green'):3,('red','blue'):2,('yellow','red'): 2、(‘绿色’、‘紫色’):2、(‘蓝色’、‘蓝色’):2、(‘蓝色’、‘红色’):2, (‘紫色’、‘蓝色’):1、(‘红色’、‘黄色’):1、(‘绿色’、‘蓝色’):1, (‘紫色’、‘红色’):1、(‘蓝色’、‘黄色’):1、(‘蓝色’、‘绿色’):1, ('yellow','green'):1,('green','yellow'):1})

如果需要只获取出现次数超过1次的项,请将计数器对象视为python dict

counts = Counter(izip(your_list, islice(your_list, 1, None)))
print [k for k,v in counts.iteritems() if v  > 1]
因此,您只需拥有相关的配对:

[('red','blue'),('red','green'),('yellow','red'),('green'), “紫色”)、(“蓝色”、“蓝色”)、(“蓝色”、“红色”)]


看到这篇我借用了一些代码的帖子:

@DGI这太棒了,谢谢!你介意把这条线的工作原理分解一下吗<代码>计数器(izip(你的列表,islice(你的列表,1,无))当你试图一起查找集合词时,是否可以采用这种方法?例如,“星球大战”、“太空球”?@gillbates,islice从元素1开始迭代列表,直到最后。izip从元素0开始用slice将列表拉开,从而将相邻的单词分组在一起。计数器然后遍历压缩对并统计出现的次数。如果不清楚,请在python中查找切片和压缩,然后查看itertools如何将列表上的这些操作转换为迭代器。@DG1假设您有一个CSV文件,看起来像“红星咖啡馆、蓝牛咖啡馆、舒适咖啡馆、咖啡馆……等等”,我想知道“红星咖啡馆”和“蓝牛咖啡馆”出现在CSV文件中的相邻位置。我可以调整bigrams方法吗?因为我正在寻找一组单词作为一对?明白。如果“你的列表=['红星咖啡馆'、'蓝牛咖啡馆'、'星巴克'、'蓝牛咖啡馆'、'鲍勃咖啡馆'、'红星咖啡馆'、'蓝牛咖啡馆'”]`然后这个方法将完全按照原样工作……它不是解析元素中的单词,只是匹配列表中的元素。
counts = Counter(izip(your_list, islice(your_list, 1, None)))
print [k for k,v in counts.iteritems() if v  > 1]