简体   繁体   English

谁能帮我解释这个python代码吗?

[英]Can anyone help me explain this python code?

7.2 Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form: 7.2编写一个提示输入文件名的程序,然后打开该文件并读取该文件,以查找以下形式的行:

  X-DSPAM-Confidence: 0.8475 

Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. 对这些行进行计数,并从每行中提取浮点值,然后计算这些值的平均值,并产生如下所示的输出。

You can download the sample data at http://www.pythonlearn.com/code/mbox-short.txt when you are testing below enter mbox-short.txt as the file name. 在下面进行测试时,可以从http://www.pythonlearn.com/code/mbox-short.txt下载示例数据。输入mbox-short.txt作为文件名。

This is what I did and got the output but is it the right way to do it?: 这是我所做的并获得了输出,但这是正确的方法吗?:

fname = raw_input("Enter file name: ")
fh = open(fname)
sum = 0
count = 0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
    line = line.replace("X-DSPAM-Confidence:","")
    line = line.strip()
    sum = sum + float(line)
    count = count + 1

avg = sum/count

print "Average spam confidence:",float(avg)

However, in few other sides, this one, and GitHub the code has been written differently. 但是,在其他方面,这一点与GitHub的代码编写方式有所不同。 Can anyone explain it to me? 谁能向我解释?

1) 1)

fname = raw_input("Enter file name: ")
fh = open(fname)
tot = 0.0
count = 0
for line in fh:
    if not line.startswith("X-DSPAM-Confidence:") : continue
    words = line.split()
    tot = tot + float(words[1])
    count = count + 1
print "Average spam confidence:", tot/count

What does split() do here? split()在这里做什么? Remove blank spaces and print it on the next line right? 删除空格并在下一行打印出来,对吗? So how does that help? 那有什么帮助呢? And what will float(words[1]) be then? 那么float(words[1])将会是什么呢? Sorry is this sounds silly but this is my 3rd day into Python and I'm just trying to learn. 抱歉,这听起来很傻,但这是我进入Python的第三天,我只是想学习。

2) Lots others have used sum this way: 2)很多其他人以这种方式使用sum:

 a = line.split(':')
     sum = sum + float(a[1])

How does this work? 这是如何运作的?

split will split a single string into multiples as a list, using either blank space or whatever character you pass as the parameter. split将使用空格或您传递的任何字符作为参数将单个字符串拆分为多个列表。 By taking [1] you're using the second of the resulting strings, since indexes start at 0. 通过采用[1]您将使用结果字符串的第二个,因为索引从0开始。

float will ignore leading spaces, so if you split on : and it leaves some spaces it doesn't matter. float将忽略前导空格,因此,如果在:分割,则它会留下一些空格也没关系。

  1. str.split() split a string into a list. str.split()将字符串拆分为列表。 Check the examples in the document for more deatils. 检查文档中的示例以了解更多详细信息。 By the way: 顺便说说:

     >>> '1 2 3 4'.split() ['1', '2', '3', '4'] >>> '1:2:3:4'.split() ['1:2:3:4'] >>> '1:2:3:4'.split(':') ['1', '2', '3', '4'] >>> 'X-DSPAM-Confidence: 0.8475'.split() ['X-DSPAM-Confidence:', '0.8475'] >>> 'X-DSPAM-Confidence: 0.8475'.split(':') ['X-DSPAM-Confidence', ' 0.8475'] 
  2. a[1] is list index in this case. 在这种情况下, a[1]是列表索引。 As the above example: 如上例所示:

     >>> a = 'X-DSPAM-Confidence: 0.8475'.split(':') >>> a[0] 'X-DSPAM-Confidence' >>> a[1] ' 0.8475' 

    It's: 它的:

     ['X-DSPAM-Confidence', ' 0.8475'] ^^^^ ^^^^ a[0] a[1] 
  3. float(x) : Return a floating point number constructed from a number or string x . float(x) :返回由数字或字符串x构造的浮点数。

    It's easy to understand: 很容易理解:

     >>> float('0.8475') 0.8475 >>> a = 'X-DSPAM-Confidence: 0.8475'.split(':') >>> float(a[1]) + 0.5 1.3475 >>> a[1] + 0.5 Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: Can't convert 'float' object to str implicitly 

    If you don't do the convert, Python will raise Type when you run + between string object and float object like the above example. 如果不进行转换,则在字符串对象和浮点对象之间运行+时,Python将引发Type ,如上例所示。

    And however, float() ignores spaces. 但是, float()忽略空格。 So: 所以:

     >>> float(' 0.8475 ') 0.8475 

In your file input you have "X-DSPAM-Confidence" always appearing like - XDSPAM-Confidence: 0.6959 . 在文件输入中, "X-DSPAM-Confidence"始终显示为XDSPAM-Confidence: 0.6959 So there is space due to which your line.split() splits the required line like this: 因此,由于line.split()分割所需的行,因此存在空间,如下所示:

['X-DSPAM-Confidence:', '0.9846']

Since by default space is argument for split command. 由于默认情况下,空格是split命令的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM