Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列表转换为字符串的简单方法,格式仍为列表_Python_String_List_Type Conversion - Fatal编程技术网

Python 将列表转换为字符串的简单方法,格式仍为列表

Python 将列表转换为字符串的简单方法,格式仍为列表,python,string,list,type-conversion,Python,String,List,Type Conversion,这应该很简单,但我找不到简单的解决方案。坦率地说,这应该是一条单行线 在Python3中,如何转换列表,例如 ['hello','world'] 格式完全相同的字符串,如 "['hello','world']" 我不想把它分成两个字 // This is bad. "hello world" 我想保留传统的列表格式。我最初认为可以将列表绑定为类似字符串(str)的列表名称,但显然不行。这不是一个简单的函数吗 编辑: 多亏了AChampion,我发现这是一个简单的语法错误。我试图将列表绑定到

这应该很简单,但我找不到简单的解决方案。坦率地说,这应该是一条单行线

在Python3中,如何转换列表,例如

['hello','world']
格式完全相同的字符串,如

"['hello','world']"
我不想把它分成两个字

// This is bad.
"hello world"
我想保留传统的列表格式。我最初认为可以将列表绑定为类似字符串(str)的列表名称,但显然不行。这不是一个简单的函数吗

编辑:

多亏了AChampion,我发现这是一个简单的语法错误。我试图将列表绑定到如下字符串:

//this is bad
(str)list_name
当我应该这样写的时候

//this is good
str(list_name)
这正是我想要的。谢谢你的帮助,很抱歉我带了这么一个初学者的问题到这里。还在习惯Python。

只需简单地说:

lst = ['hello', 'world']
new_str = str(lst)
print(new_str) # prints out ["hello", "world"]
Python不会将字符串转换为字符串格式,除非您明确地告诉它。像这样:

lst = ['hello', 'world']
str1 = str(''.join(lst))
print(str1)# prints 'helloworld'
或者,如果您想要一个函数:

def listToString(lst):
    new_lst = str(lst)
    return new_lst
虽然函数实际上更像一个包装器。我会直接使用python bulitin
str()
转换列表,而不是尝试将其放入包装函数中。

只需简单地说:

lst = ['hello', 'world']
new_str = str(lst)
print(new_str) # prints out ["hello", "world"]
Python不会将字符串转换为字符串格式,除非您明确地告诉它。像这样:

lst = ['hello', 'world']
str1 = str(''.join(lst))
print(str1)# prints 'helloworld'
或者,如果您想要一个函数:

def listToString(lst):
    new_lst = str(lst)
    return new_lst

虽然函数实际上更像一个包装器。我会直接使用python bulitin
str()
转换列表,而不是尝试将其放入包装函数中。

简短的回答是:你不能完全按照你的要求做

您不能获取在Python中键入的任何列表,并获取具有完全相同原始格式的字符串。Python对列表中的间距非常宽松,因此您可以这样构造列表:

  • ['hello','world']
  • ['hello','world']
  • [“你好”,“世界”]
  • [“你好”,“世界”]
  • 当Python将这些列表创建为列表时,它不会保留有关如何编写它的信息。所以,没有办法让它回来


    如果你只是想要一个字符串化的列表,你可能应该使用。

    简短的回答是:你不能完全按照你的要求去做

    您不能获取在Python中键入的任何列表,并获取具有完全相同原始格式的字符串。Python对列表中的间距非常宽松,因此您可以这样构造列表:

  • ['hello','world']
  • ['hello','world']
  • [“你好”,“世界”]
  • [“你好”,“世界”]
  • 当Python将这些列表创建为列表时,它不会保留有关如何编写它的信息。所以,没有办法让它回来

    如果您只是想要一个字符串化的列表,您可能应该使用。

    假设它是一个简单字符串列表,那么您需要的是列表
    str()
    repr()
    表单。
    如果您有一个更复杂的结构,并且希望将其转换(序列化)为字符串形式并返回到对象形式,那么您可能需要查看
    json
    模块:

    >>> import json
    >>> json.dumps(['hello','world'])
    '["hello", "world"]'
    
    假设它是一个简单字符串列表,那么a列表
    str()
    repr()
    表单都是您需要的表单。
    如果您有一个更复杂的结构,并且希望将其转换(序列化)为字符串形式并返回到对象形式,那么您可能需要查看
    json
    模块:

    >>> import json
    >>> json.dumps(['hello','world'])
    '["hello", "world"]'