Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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
用于计算输出中列出的最多commmon字符串的Python计数器_Python_Regex_Python 2.7_Counter - Fatal编程技术网

用于计算输出中列出的最多commmon字符串的Python计数器

用于计算输出中列出的最多commmon字符串的Python计数器,python,regex,python-2.7,counter,Python,Regex,Python 2.7,Counter,嘿,伙计们,我一直在编写这段代码来计算文本文档中出现的字符串数,这是在我前面的问题上,代码如下所示: from collections import Counter with open("C:\\Documents and Settings\\Zha\\Desktop\\stringResult\\sguresult_lenght10.txt") as f: content = f.read() a = Counter(content.split()).most_common()

嘿,伙计们,我一直在编写这段代码来计算文本文档中出现的字符串数,这是在我前面的问题上,代码如下所示:

from collections import Counter
with open("C:\\Documents and Settings\\Zha\\Desktop\\stringResult\\sguresult_lenght10.txt") as f:
    content = f.read()
a = Counter(content.split()).most_common()    
for line in a:
        print line
打印行的结果示例如下

('KERNEL32.dll', 58)
('not', 49)
('.data', 49)
("nRX|'", 20)
('xZGVr', 20)
('TyN*u', 20)
('[Wu^D', 20)
逗号前的部分是字符串,下一部分是发生次数 现在的问题是,我需要删除所有括号外和“”之间的单引号,只留下字符串和数字,即

KERNEL32.dll, 58
not, 49
.data, 49
etc

我一直在尝试使用regex,但似乎我可能弄错了模式,因为单引号有时是字符串本身的第一部分。有什么简单的方法可以解决这个问题吗?谢谢

使用
*
语法:

for line in a:
    print *line
*
语法告诉函数将集合中的元素视为单个参数

例如:

>>>foo = ('Hello', 'World', 1, 2, 3,)
>>>print foo
('Hello', 'World', 1, 2, 3)
>>>print *foo
'Hello' 'World' 1 2 3
您可以使用以下选项:

for line in a:
    print '%s, %s' % (line[0], line[1])
我的回答删除了报价,无需添加新的条件

a= dict(Counter(content.split()).most_common())

for i,j in a.items():
    print i,j