首页 > 解决方案 > 如何在一行中从数组中获取值(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 和一般编码的新手。

标签: pythonlist

解决方案


您可以使用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"

推荐阅读