简体   繁体   English

Python:列表长度为单个整数

[英]Python: Length of list as single integer

I'm new to Python and I'm trying to output the length of a list as a single integer, eg: 我是Python的新手,正在尝试将列表的长度输出为单个整数,例如:

l1 = ['a', 'b', 'c'] 
len(l1) = 3

However, it is printing on cmdline with 1s down the page, eg: 1 1 1 1 1 1 etc 但是,它在cmdline上向下打印1s,例如:1 1 1 1 1 1等

How can I get it to just output the number rather than a list of 1s? 如何获取仅输出数字而不是1的列表?

(Here's the code:) (以下是代码:)

def Q3():
  from datetime import datetime, timedelta 
  inputauth = open("auth.log", "r") 
  authStrings = inputauth.readlines() 

  failedPass = 'Failed password for'
  for line in authStrings:
    time = line[7:15]
    dateHour = line[0:9]
    countAttack1 = []

    if time in line and failedPass in line:
      if dateHour == 'Feb  3 08':
        countAttack1.append(time)
        length1 = len(countAttack1)
        print(length1)

Ideally, I'd like it to output the number in a print so that I could format it, aka: 理想情况下,我希望它以打印的形式输出数字,以便可以将其格式化,也就是:

print("Attack 1: " + length1)

I think you are looping and ifs are inside a loop. 我认为您正在循环,如果在循环内。 If so, just print the length outside loop scope. 如果是这样,只需在循环范围之外打印长度即可。 Please share the complete code for a better answer 请分享完整的代码以获得更好的答案

Well as Syed Abdul Wahab said, the problem is that the "list" is getting recreated each loop. 正如Syed Abdul Wahab所说的那样,问题在于每个循环都在重新创建“列表”。 This makes so that the print reports "1", as it is the actual length of the list. 这使得打印报告为“ 1”,因为它是列表的实际长度。 The other problem, repetition of the printng is similar - you are actually printing "each time in the loop". 另一个问题是printng的重复是类似的-您实际上是在“每次循环”中打印。

The solution is then simple: you initialize the list outside the loop; 然后,解决方案很简单:您可以在循环外初始化列表; and also report outside the loop. 并在循环外报告。

def Q3():
  from datetime import datetime, timedelta 
  inputauth = open("auth.log", "r") 
  authStrings = inputauth.readlines() 

  failedPass = 'Failed password for'
  countAttack1 = []  # after this line the countAttack will be empty
  for line in authStrings:
    time = line[7:15]
    dateHour = line[0:9]

    if time in line and failedPass in line:
      if dateHour == 'Feb  3 08':
        countAttack1.append(time)

  length1 = len(countAttack1)
  print("Attack 1: " + str(length1))

I'd also like to take a bit of time to link you to string formatting While the documentation is complex it will make printing much easier, above print is trnasformed into: 我还想花一点时间将您链接到字符串格式,尽管文档很复杂,但是打印起来会容易得多,上面的打印形式变成了:

print("Attack 1: {0}".format(length1))


Further analysing the code gives some peculiarities, you check if time is in the line string. 进一步分析代码会带来一些特殊性,请检查time是否line字符串中。 - However just a few codelines above you create time from a slice of line - so it will always be inside line . -但是,您上方只有几条代码line会从一条line创建time -因此它始终位于line (Except for the edge case where line is not of correct length, but that'll error anyways). (除了边缘的情况,其中行的长度不正确,但是无论如何都会出错)。 So that if statement should be simplified to: 因此,if语句应简化为:

  if failedPass in line: 

Here is the function that prints the the length: 这是打印长度的函数:

def print_length():
    if time in line and failedPass in line:    
        if dateHour == 'Feb  3 08':
           countAttack1.append(time)
    length1 = len(countAttack1)
    print(length1)
print_length()
>>>Print length of the List.

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

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