gpt4 book ai didi

python - 如何在一行中从数组中获取值(Python 3)

转载 作者:行者123 更新时间:2023-12-02 03:08:53 24 4
gpt4 key购买 nike

我想从一行中获取数组[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) :

Return a string which is the concatenation of the strings in iterable. If there is any Unicode object in iterable, return a Unicode instead. A TypeError will be raised if there are any non-string or non Unicode object values in iterable. The separator between elements is the string providing this method.

以下应该可以解决问题:

array_value = ['a','b','c','d','e','f','g']
output_string = ''.join(array_value)
print(output_string)

>>> "abcdefg"

关于python - 如何在一行中从数组中获取值(Python 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58247507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com