尝试使用sort()对列表进行排序

时间:2018-08-15 11:41:02

标签: python python-3.x

在将另一个文件的内容附加到列表(水果)之后,然后将其写入txt文件“ fruit_salad.txt”之前,我试图对列表(水果)进行排序。

我遇到的问题是,一旦我写了“ fruit.sort()”,它便无法识别输出。

我通过对导致此问题的列表进行排序来做什么?

谢谢。


--- CODE ---

try:
    f = open("fruit.txt", mode='r', encoding='utf-8')
    fruit = f.readlines()
    print(fruit, "\n")


    f1 = open("more_fruit.txt", mode="r", encoding='utf-8')
    fruit.append(f1.readlines())
    print(fruit)
    # fruit.sort() # WITHOUT THIS LINE IT WORKS FINE
    output = open("fruit_salad.txt", mode='w', encoding='utf-8')

    for line in fruit:
        output.writelines(line)

except (FileNotFoundError, IOError):
    print("File Not Found!!")
finally:
    f.close()
    f1.close()
    output.close()

-输出-

 ['pear\n', 'apple\n', 'orange \n', 'mandarin\n', 'watermelon\n', 
 'pomegranate\n', 'lemon\n', 'pineapple\n'] 

  ['pear\n', 'apple\n', 'orange \n', 'mandarin\n', 'watermelon\n', 'pomegranate\n', 'lemon\n', 'pineapple\n', ['banana\n', 'raspberry\n', 'blueberry\n', 'lime\n', 'blackberry\n', 'cherry\n', 'grape']]

程序python.exe已退出,代码为0(0x0)。


错误输出

在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\ptvsd_launcher.py", line 111, in <module>
    vspd.debug(filename, port_num, debug_id, debug_options, run_as)
  File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\Packages\ptvsd\debugger.py", line 36, in debug
    run(address, filename, *args, **kwargs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\Packages\ptvsd\_main.py", line 47, in run_file
    run(argv, addr, **kwargs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\Packages\ptvsd\_main.py", line 98, in _run
    _pydevd.main()
  File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\Packages\ptvsd\pydevd\pydevd.py", line 1628, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\Packages\ptvsd\pydevd\pydevd.py", line 1035, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\Packages\ptvsd\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:\Users\micha\source\repos\PythonFileManagement\PythonFileManagement\PythonFileManagement.py", line 24, in <module>
    output.close()
NameError: name 'output' is not defined

1 个答案:

答案 0 :(得分:0)

您正在将列表追加到列表的末尾,您会看到:

[elem11, elem12, ..., elem1n, [elem21, elem22, ..., elem2m]](列表中的列表)

您需要使用extend而不是append来添加f1.readlines()的所有元素

fruit.extend(f1.readlines())

来源:https://developers.google.com/edu/python/lists