如何从lis创建字典键

2024-04-25 05:38:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试创建一个脚本来搜索文本文件中的模式,计算它出现的次数,然后将其作为键值对插入到字典中。你知道吗

代码如下:

fname = raw_input("File name: ")
import re
vars = dict()
lst= list()
count = 0

try:
    fhand = open(fname, "r+")
except:
    print "File not found"
quit()  

for line in fhand:
    line.rstrip()
    if re.search(pattern , line):
        x = re.findall(pattern , line)
        lst.append(x)
    else:
        continue
    for x in lst:
        count += 1

从regex方法中提取文本并将其插入字典以使其看起来像这样的最佳方法是什么:

{'pattern' : count, 'pattern' : count, 'pattern' : count}

Tags: 方法inre脚本for字典countline
3条回答

你可以这样做:

fhand = ["<abc> <abc>", "<abc>", "<d>"]

counts = {}
pattern = re.compile(r'<\w+>') # insert your own regex here

for line in fhand:
    for match in pattern.findall(line):
        # initialize the count for this match to 0 if it does not yet exist
        counts.setdefault(match, 0)
        counts[match] += 1

给予

counts = {'<abc>': 3, '<d>': 1}

首先,我将使用with来打开您的文件,而不仅仅是open。你知道吗

例如:

with open(fname, "r+") as fhand:

而且,我认为你误解了字典的意义。它们是键/值存储,也就是说,每个键都是唯一的。你不能有多把钥匙。你知道吗

我认为更好的解决办法如下:

import collections 

for line in fhand:
line.rstrip()
if re.search(pattern , line):
    x = re.findall(pattern , line)
    lst.append(x)
else:
    continue

counted = collections.Counter(lst)
print counted

这将返回一个包含列表中出现的键/值的字典

你是说这样的事吗?你知道吗

import re

pattern1 = r'([a-z]+)'
pattern2 = r'([0-9])'

regex1 = re.compile(pattern1)
regex2 = re.compile(pattern2)

filename = "somefile.txt"

d = dict()

with open(filename, "r") as f:
    for line in f:
        d[pattern1] = d.get(pattern1, 0) + len(regex1.findall(line));
        d[pattern2] = d.get(pattern2, 0) + len(regex2.findall(line));

print d
# output: {'([0-9])': 9, '([a-z]+)': 23}

相关问题 更多 >