为什么出现次数没有增加?

问题描述 投票:0回答:2

[这里是我的问题:我有一个字典(dico),我想计算两次不同键的次数,它们都出现在文件“ file.tsv”的同一行中,看起来像这个:

sp_345_4567 pe_645_4567876  ap_456_45678    pe_645_4556789 ...
sp_345_567  pe_645_45678 ...
pe_645_45678    ap_456_345678 ...
sp_345_56789    ap_456_345 ...
pe_645_45678    ap_456_345678 ...
sp_345_56789    ap_456_345 ...
...

例如,香蕉和苹果键的值出现在第1行,所以无论它们出现多少次,它们仍然存在,所以我们共有1行,我想在文件

为此,我在每个值后面添加了模式'_\w+',然后使用函数re.search进行了正则表达式。

from itertools import product
import csv

dico = {
    "banana": "sp_345",
    "apple": "ap_456",
    "pear": "pe_345",
    "cherry": "ap_345",
    "coco": "sp_543",
}

counter = {}
with open("file.tsv") as file:
    reader = csv.reader(file, delimiter="\t")
    for line in reader:
        for key1, key2 in product(dico, dico):
            if key1 >= key2:
                continue
            counter[key1, key2] = 0
            k1 = k2 = False
            for el in line:
                if re.search(dico[key1]+'_\w+', el):
                    k1 = True
                elif re.search(dico[key2]+'_\w+', el):
                    k2 = True
                if k1 and k2:
                    counter[key1, key2] += 1
                    break

for key, val in counter.items():
    print(key, val)

但是发生的位置在0处停止:

Apple banana 0
pear banana 0
pear apple 0
python file itertools
2个回答
1
投票

k1k2都不能同时为True,因为您同时使用False进行了初始化,并且最多将True设置为1。

elif re.search(dico[key2]+'_\w+', el):
    k2 = True

应该是

if re.search(dico[key2]+'_\w+', el):
     k2 = True

0
投票

您的电话

counter[key1, key2] = 0

仅应在(key1,key2)还没有值时发生。例如,添加一个测试:

if (key1, key2) not in counter:
    counter[key1, key2] = 0

或者您可以在打开csv之前将所有对的counter [key1,key2]设置为0。如:

for key1, key2 in product(dico, dico):
    if key1 < key2:
        counter[key1, key2] = 0
counter = {}
with open("file.tsv") as file:
    ....  

elif re.search(dico[key2]+'_\w+', el):

应该是

if re.search(dico[key2]+'_\w+', el):

否则,找到键1时将永远找不到键2

© www.soinside.com 2019 - 2024. All rights reserved.