如何从列表中创建字典键
how do I create dictionary keys from a list
我一直在尝试创建一个脚本来搜索文本文件中的模式,计算它出现的次数,然后将其作为键值对插入到字典中。
代码如下:
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
从正则表达式方法中提取文本并将其插入字典以使其看起来像这样的最佳方法是什么:
{'pattern' : count, 'pattern' : count, 'pattern' : count}
你的意思是这样的吗?
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}
你可以这样做:
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:
另外,我认为你误解了字典的意义。它们是 key/value 家商店,也就是说,每把钥匙都是独一无二的。您不能拥有超过一把钥匙。
我认为更好的解决方案如下:
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
这将 return 一个字典,其中包含您列表的 key/value 次出现,
我一直在尝试创建一个脚本来搜索文本文件中的模式,计算它出现的次数,然后将其作为键值对插入到字典中。
代码如下:
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
从正则表达式方法中提取文本并将其插入字典以使其看起来像这样的最佳方法是什么:
{'pattern' : count, 'pattern' : count, 'pattern' : count}
你的意思是这样的吗?
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}
你可以这样做:
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:
另外,我认为你误解了字典的意义。它们是 key/value 家商店,也就是说,每把钥匙都是独一无二的。您不能拥有超过一把钥匙。
我认为更好的解决方案如下:
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
这将 return 一个字典,其中包含您列表的 key/value 次出现,