python - 如何在一行中从数组中获取值(Python 3)
问题描述
我想[a,b,c,d,e,f,g]
在一行中获取数组中的所有值并将其保存到Python 3中的另一个变量中
#This is the code I made and am getting the output "abcdefg" but am not sure
#how to store the output ,instead of printing it out.
array_value = [a,b,c,d,e,f,g]
for x in array_value:
print(x, end = '')
这可能是一个简单的问题,但我是 python 和一般编码的新手。
解决方案
您可以使用str.join(iterable)
:
返回一个字符串,它是 iterable 中字符串的串联。如果 iterable 中有任何 Unicode 对象,则返回一个 Unicode。
TypeError
如果 iterable 中有任何非字符串或非 Unicode 对象值,则将引发A。元素之间的分隔符是提供此方法的字符串。
以下应该可以解决问题:
array_value = ['a','b','c','d','e','f','g']
output_string = ''.join(array_value)
print(output_string)
>>> "abcdefg"
推荐阅读
- javascript - 等待 Ionic 中的订阅方法
- javascript - 为什么自定义函数会创建错误但console.log 没有点击事件?
- php - 雄辩的删除 - SQLSTATE [22007]:无效的日期时间格式:1292 截断不正确的 DOUBLE 值:
- ios - 如何本地化新创建的故事板?
- c++ - 按顺序将树转换为数组
- javascript - HTML 表格未显示在源文件中
- python - 为什么 Travis CI 无法构建 Python 项目?
- linux - Jenkins 服务无法在 Oracle Linux 上使用 SDKMan Java 启动
- java - ArrayList - 计算我列出的同一个对象是否准确 2 次,如果:附加到“tempFinalFilterSearchList”
- javascript - 表单提交后如何添加成功通知