Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 需要从列表中计算这些项目_Python_List_Count - Fatal编程技术网

Python 需要从列表中计算这些项目

Python 需要从列表中计算这些项目,python,list,count,Python,List,Count,我需要你的帮助:) 我有一个名为Access日志的大日志文件,有很多这样的行(没有空行): 我正在用python“战斗”,从每一行中提取引用者,并将它们与最常见的50行中的副本数一起列出。我知道使用“awk”很容易,但我想学习如何使用python。我所做的是将空白空间标记为字段分隔符,并在字段十和十一之间取字符串。 arxiu_de_log = open("access_log.log","r") linies = arxiu_de_log.readlines() arxiu_de_log.cl

我需要你的帮助:)

我有一个名为Access日志的大日志文件,有很多这样的行(没有空行):

我正在用python“战斗”,从每一行中提取引用者,并将它们与最常见的50行中的副本数一起列出。我知道使用“awk”很容易,但我想学习如何使用python。我所做的是将空白空间标记为字段分隔符,并在字段十和十一之间取字符串。

arxiu_de_log = open("access_log.log","r")
linies = arxiu_de_log.readlines()
arxiu_de_log.close() 

clean_log=[]
for line in linies:
    try:
        separador_de_linea=line.split(' ')
        camp_de_referer = separador_de_linea[10:11]
        clean_log.append(camp_de_referer)

    except:
        pass

print clean_log
当我运行程序时,我得到的是:

[['"-"'], ['"hxxp://www.joquese.cat/frutos.html"'], ['"hxxp://www.joquese.cat/frutos.swf"'], ['"-"'], ['"hxxp://www.joquese.cat/"']
但我想要类似的东西:

2   "-"

1   "hxxp://www.joquese.cat/frutos.swf"

1   "hxxp://www.joquese.cat/frutos.swf"

1   "hxxp://www.joquese.cat/"

....
import collections
...
counter = collections.Counter(clean_log)

for count in counter.most_common(50):
    print(str(count[1])+"\t"+ str(count[0]))
我已尝试使用类似以下内容对计数器进行编程:

2   "-"

1   "hxxp://www.joquese.cat/frutos.swf"

1   "hxxp://www.joquese.cat/frutos.swf"

1   "hxxp://www.joquese.cat/"

....
import collections
...
counter = collections.Counter(clean_log)

for count in counter.most_common(50):
    print(str(count[1])+"\t"+ str(count[0]))

但是我不能让它正常工作,你能帮我吗?

看来你很快就能得到你想要的了。需要记住的一点是,
separador\u de\u linea[10:11]
是一个列表,而
separador\u de\u linea[10]
是一个字符串。我想你想要字符串:

import collections
count = collections.Counter()
with open("access_log.log","r") as arxiu_de_log:
    for line in arxiu_de_log:
        line = line.strip()
        if line:
            separador_de_linea = line.split()
            camp_de_referer = separador_de_linea[10]
            count[camp_de_referer] += 1

for referer, cnt in count.most_common(50):
    print('{} {}'.format(cnt, referer))
屈服

2 "-"
1 "hxxp://www.joquese.cat/frutos.swf"
1 "hxxp://www.joquese.cat/frutos.html"
这样可以避免创建
clean_log
列表,从而节省内存


小贴士:

  • 而不是

    arxiu_de_log = open("access_log.log","r")
    
    使用

    当Python的执行流离开 带着声明。这样你就不必(记得)打电话了

    明确你自己

  • 尽可能避免调用readlines(),因为这样会加载 将整个文件存储到内存中,并创建所有行的Python列表。 不要这样做,除非你需要所有的行保存在内存中 同时。在许多情况下,一次只需要一行代码。所以 而不是

    linies = arxiu_de_log.readlines()
    
    使用

  • try…except
    语句中使用裸
    except
    是一个坏习惯。 它将捕获比您预期更多的内容,例如
    键盘中断
    SystemExit
    异常。最好的做法是只捕捉错误 你想处理。在这种情况下

    try:
        ...
    except IndexError:
        pass
    
    那就更好了


不能将列表与计数器一起使用,只能使用哈希类型。你为什么把推荐人放在名单上

import collections

with open("access_log.log","r") as arxiu_de_log:
    clean_log = [
        (line.split(' ')+[None]*10)[10]
        for line in arxiu_de_log]

counter = collections.Counter(clean_log)

for count in counter.most_common(50):
    print(str(count[1])+"\t"+ str(count[0]))

Unutbu您的代码运行得非常好,但由于我的“技能”较低,我对Daniel解决方案的理解更好,我只能检查一个“答案”。不知道为什么没有选择此答案,它是最干净的,实际上是正确的。正如我之前所说,两者都是正确的,但由于我的低技能,我对第一个答案的理解更好。我想两个都查一下,但我不能。
try:
    ...
except IndexError:
    pass
import collections

with open("access_log.log","r") as arxiu_de_log:
    clean_log = [
        (line.split(' ')+[None]*10)[10]
        for line in arxiu_de_log]

counter = collections.Counter(clean_log)

for count in counter.most_common(50):
    print(str(count[1])+"\t"+ str(count[0]))