mar*_*eau 12 python set literals abstract-syntax-tree
在Python 2.7中的新功能文档中,它表示对集合文字的支持是从Python 3.1反向移植的.但是,似乎这种支持没有扩展到ast模块的literal_eval()功能,如下所示.
这是故意的,疏忽的还是其他的 - 从字符串表示创建文字集的最简洁的解决方法是什么?(我假设以下工作在Python 3.1+中,对吧?)
import ast
a_set = {1,2,3,4,5}
print a_set
print ast.literal_eval('{1,2,3,4,5}')
Run Code Online (Sandbox Code Playgroud)
输出显示错误消息:
set([1, 2, 3, 4, 5])
Traceback (most recent call last):
File "...\setliterals.py", line 4, in <module>
print ast.literal_eval('{1,2,3,4,5}')
File "...\Python\lib\ast.py", line 80, in literal_eval
return _convert(node_or_string)
File "...\Python\lib\ast.py", line 79, in _convert
raise ValueError('malformed string')
ValueError: malformed string
Run Code Online (Sandbox Code Playgroud)
PS我能想到的唯一解决方法就是使用eval().
我一直在使用它来转换 pandas DataFrame 中的列(df[col] = df[col].apply(to_set)。可能对发现这个问题的任何人有用。它可能没有那么快,但它避免使用eval.
def to_set(set_str):
"""
Required to get around the lack of support for sets in ast.literal_eval.
It works by converting the string to a list and then to a set.
Parameters
----------
set_str : str
A string representation of a set.
Returns
-------
set
Raises
------
ValueError
"malformed string" if the string does not start with '{' and and end
with '}'.
"""
set_str = set_str.strip()
if not (set_str.startswith('{') and set_str.endswith('}')):
raise ValueError("malformed string")
olds, news = ['{', '}'] , ['[',']']
for old, new in izip(olds, news):
set_str = set_str.replace(old, new)
return set(literal_eval(set_str))
Run Code Online (Sandbox Code Playgroud)