从python字符串集中删除引号

tau*_*rus 1 python set python-3.x

我正在创建一个空集并使用该.add()函数向其添加多个字符串.当我打印出我的设置时,它具有以下格式:

{'abc', 'def', 'ghi'}
Run Code Online (Sandbox Code Playgroud)

Python 3中是否有一种方法可以删除每个字符串周围的引号,以便返回该集合

{abc, def, ghi}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 6

所有Python容器(包括集合)都repr()用于显示其内容; 这是一个调试辅助工具,一个开发人员代表,而不是给最终用户的东西.

因此,如果要在不使用的情况下显示内容,则需要手动设置字符串格式repr().例如:

def set_representation(s):
    return '{{{}}}'.format(', '.join(map(str, s)))
Run Code Online (Sandbox Code Playgroud)

将值映射到str()使用逗号连接它们并使用{...}大括号包围结果之前.

这会产生:

>>> s = {'abc', 'def', 'ghi'}
>>> print(set_representation(s))
{def, ghi, abc}
Run Code Online (Sandbox Code Playgroud)