首页 \ 问答 \ 在Python中保存列表(Save list in Python)

在Python中保存列表(Save list in Python)

我想在python数组中保存一个字符串列表。 我的代码如下:

import os
Images_list=[os.listdir("/home/metal-machine/Pictures/new")]
print Images_list[0]

它只在[0]和我使用时打印列表

print Images_list[1]

它返回错误:IndexError:列表索引超出范围

我确信这个“索引超出范围”是因为列表的所有元素都没有用逗号分隔。

为此,我也可以在Python中使用split()方法。

images_list.split()

上面的命令拆分文件,但在所有文件之间插入逗号,但为什么Python默认情况下不在所有元素之间插入逗号? sunch类列表在Python中是否有效?


I want to save a list of strings in the python Array. My code is as below:

import os
Images_list=[os.listdir("/home/metal-machine/Pictures/new")]
print Images_list[0]

It only prints the list in the [0] and when I use

print Images_list[1]

It returns the error as: IndexError: list index out of range

I am sure this "index out of range" is because all the elements of list are not separated by comma.

For that I can also use split() method in Python.

images_list.split()

And the above command splits the files but putting commas in-between all of them, but why Python By-default not put commas in between all the elements? Is sunch kind of list is valid in Python?


原文:https://stackoverflow.com/questions/29091321
更新时间:2022-01-11 20:01

最满意答案

为什么不编写更无错误的代码

for images in Images_list:
    print images

Why not write code that is a bit more error free

for images in Images_list:
    print images

相关问答

更多

相关文章

更多

最新问答

更多
  • Google会更新重定向网址吗?(Does Google update redirect URLs? [closed])
  • 如何将js-variable从.js文件传递给Electron中的.html页面?(How to pass js-variable from .js file to .html page in Electron?)
  • matlab中的数据处理gui - 从文件和文本框中读取并传递给函数(data handling in matlab gui - reading from file and textbox and pass to function)
  • SVG矩形的笔划宽度在顶部/左侧被切除。(SVG rectangle's stroke widths are cut off on top/left. How do I fix this?)
  • BOOST_CHECK中的错误?(Bug in BOOST_CHECK?)
  • 使用ActiveDocument时记录替换量(Recording the amount of replacements when using ActiveDocument)
  • 将php数组转换为javascript数组(convert php array to a javascript array)
  • 在斯威夫特干邑国际象棋引擎与iOS项目集成(Stockfish Chess Engine Integration with iOS project in Swift)
  • C# - 增加一个双倍值(1.212E + 25)(C# - Incrementing a double value (1.212E+25))
  • 取消asynctask的最佳方法(Best way to cancel asynctask)
  • 文件转换问题的最佳方法(Best approach to File Conversion problems)
  • 从分组数据中选择第一行和最后一行(Select first and last row from grouped data)
  • 哪些元素可以禁用?(What elements can ng-disabled be applied to?)
  • ASP.NET GridView在回发时为空(ASP.NET GridView empty on postback)
  • lua环境和模块(lua environments and modules)
  • 如何将小切片复制到更大切片的末尾(How to copy a small slice to the end of a bigger slice)
  • IIS7 URL重写模块替换(IIS7 URL Rewriting Module Replace)
  • IE和Google Chrome中的多表单提交(Multi-forms submission in IE and Google chrome)
  • Symfony2:如何在控制器中检索UserChecker?(Symfony2: How to retrieve the UserChecker in a controller?)
  • 循环通过数组和删除项目,而不破坏循环(Looping through array and removing items, without breaking for loop)
  • 如何动态更改YouTube播放器的选项(How to dynamically change YouTube player's options)
  • Laravel使用以点开头的文件处理存储磁盘(Laravel working through storage disk with files starting with dot)
  • 在挂架中使用重定向时出错(Error using redirect in pylons)
  • 替换netCDF文件中的变量(Replacing a variable inside a netCDF file)
  • 让swable中的非可变数组(let Non-mutable array in swift)
  • 春季安全缺省登录页面代码位于何处?(Where is spring security default login page code located?)
  • Unix - 并排打印公共线(Unix - Print Common Lines side by side)
  • 验证TimeSpan(Validating TimeSpan)
  • 如果还有一个具有返回值的重载版本,如何正确调用非返回值Swift函数?(How do I properly call a non-return-value Swift function when it also has an overloading version that has returned value?)
  • Perl:比较两个文件并打印匹配和不匹配的数据(Perl: Comparing two files and printing data that match and don't match)