将颜色的字符串表示形式转换回列表

Convert string representation of colors back to list

我的数据中有一列是数字列表,但在保存为 CSV 时,我猜这是作为字符串存储的。 我想将这个字符串列表转换回列表列表。

我的数据现在是这样的:

import pandas as pd 
from ast import literal_eval

colors = ["(120, 120, 80)", "(90, 10, 100)"]
names = ["name1", "name2"]
data = {
    "colors":colors,
    "names":names
}
df = pd.DataFrame(data)

通过阅读 Whosebug,我尝试了文字 eval 方法,但它没有用:

try:
  df['colors'] = literal_eval( df['colors'].tolist() )
except ValueError as e:
  print(e)

我收到格式错误的字符串错误。

您可以对每一列执行类似的操作:

col = [int(val) for val in colors.replace("(","").replace(")","").split(",")]

使用literal_eval() 是一个很好的方法。问题是它需要单独应用于每个 sub-list (字符串)。 Pythonic 方法是使用如下列表推导式:

>>> from ast import literal_eval
>>> colors = ["(120, 120, 80)", "(90, 10, 100)"]
>>> colors = [literal_eval(x) for x in colors]
>>> colors
[(120, 120, 80), (90, 10, 100)]

要获得 listlist 而不是 listtuple,您可以使用:

>>> from ast import literal_eval
>>> colors = ["(120, 120, 80)", "(90, 10, 100)"]
>>> colors = [list(literal_eval(x)) for x in colors]
>>> colors
[[120, 120, 80], [90, 10, 100]]

ast.literal_eval(node_or_string) 的 Python 文档指出:

Safely evaluate an expression node or a string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None.

This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.

使用 re.findall 提取数字并 apply 遍历系列:

import re
df['colors'].apply(lambda str : [int(s) for s in re.findall(r'\d+',str) ]).tolist()

#  [[120, 120, 80], [90, 10, 100]]