简体   繁体   English

Python 将列表转换为字符串的程序

[英]Python program to convert a list to string

I have a list list = ["Num1","Num2","Num3"] how can I convert list to string like: Num1 => avg(Num1) as ABC I found function join but I can't convert我有一个列表list = ["Num1","Num2","Num3"]如何将列表转换为字符串,例如: Num1 => avg(Num1) as ABC我发现 function join 但我无法转换

The join() method takes in an iterable as an argument (in you case, the list), and joins it with the string that you called it on (ie, an empty string or a space). join()方法接受一个可迭代对象作为参数(在您的例子中是列表),并将它与您调用它的字符串(即空字符串或空格)连接起来。 So in your case, you can use:所以在你的情况下,你可以使用:

' '.join(list) # 'Num1 Num2 Num3'

Also, list is a bad variable name since it eclipses the built in function list() .此外, list是一个错误的变量名,因为它使内置的 function list()黯然失色。

list_sum = 0
for entry in list:
      list_sum += int(entry)
list_average = list_sum / len(list)

Something like that should work类似的东西应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM