Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 - Fatal编程技术网

Python 我正在寻找印刷品;蠕虫“;从一个集合中只有一次。有办法吗?

Python 我正在寻找印刷品;蠕虫“;从一个集合中只有一次。有办法吗?,python,Python,这是到目前为止我的脚本,我们有一个文件要打开和排序。当我打印结果时,它会打印蠕虫的名称,就像每个蠕虫打印10-12次一样。有没有办法让它们只显示一次?如果您能提供任何帮助,我将不胜感激 from __future__ import print_function ''' Week Two Assignment 1 - File Processing ''' ''' Complete the script below to do the following: 1) Add your name,

这是到目前为止我的脚本,我们有一个文件要打开和排序。当我打印结果时,它会打印蠕虫的名称,就像每个蠕虫打印10-12次一样。有没有办法让它们只显示一次?如果您能提供任何帮助,我将不胜感激

from __future__ import print_function
'''

Week Two Assignment 1 - File Processing
'''

'''
Complete the script below to do the following:
1) Add your name, date, assignment number to the top of this script
2) Open the file redhat.txt 
   a) Iterate through each line of the file
   b) Split eachline into individual fields (hint str.split() method)
   c) Examine each field of the resulting field list
   d) If the word "worm" appears in the field then add the worm name to the set of worms
   e) Once you have processed all the lines in the file
      sort the set 
      iterate through the set of worm names
      print each unqiue worm name 
3) Submit
   NamingConvention: lastNameFirstInitial_Assignment_.ext
   for example:  hosmerC_WK2-1_script.py
                 hosmerC_WK2-2_screenshot.jpg
   A) Screenshot of the results in WingIDE
   B) Your Script
'''
import os

SCRIPT_NAME    = "Week Two Assignment 1 - File Processing"
SCRIPT_VERSION = "Version 1.0"
SCRIPT_AUTHOR  = "Author: Brandon Holman"
SCRIPT_DATE = "January 20 2021"

print()
print(SCRIPT_NAME)
print(SCRIPT_VERSION)
print(SCRIPT_AUTHOR)
print(SCRIPT_DATE)
print()


uniqueWorms = set()

with open("redhat.txt", 'r') as logFile:
    for eachLine in logFile:
        ''' your code starts here '''
        
       line = eachLine.split():

       for field in line:
           if 'Worm' in field:
               uniqueWorms.add(field)
       for worms in sorted(uniqueWorms):
           print(worms)
     
                
        print("Script Complete")
    

仅当您处理完文件后才处理蠕虫列表

for eachLine in logFile:
    
    print(eachLine)
     
    fields = eachLine.split()
    for field in fields:
        if field.lower().find('worm')!= -1:
            uniqueWorms.append(field.lower())

# You're all done reading the file and collecting worms.
# NOW you can print the results
uniqueWorms = sorted(uniqueWorms)
for worm in uniqueWorms:
    print(worm)

阅读
嵌套循环
,并确保将
打印
放入适当的级别。使用固定缩进,它会给我一个内置错误。AttributeError:'set'对象没有属性'append',正确--您仍然需要修复该部分。要将元素添加到集合中,还是附加到列表中?您的原始代码在这种情况下也是不一致的。你成功了,因为第一次循环迭代将集合转换为一个列表。因此,我将uniqueWorms设置为一个空集,我想将一个蠕虫的每个不同名称放入集合中,然后打印这些蠕虫的名称,当我这样做时,它会像每次打印10次一样打印名称。你将其设置为一个空集,将其转换为一个列表,并排序为
,并附加到列表中。“每次10次”只不过是重复你原来的问题,上面的问题已经解决了。确定您想要的是一个集合还是一个列表,使添加/附加操作与之匹配,您应该完成。它是一个集合,我正在尝试向其中添加蠕虫名称,但我显然没有做正确的事情。