gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:57:26 25 4
gpt4 key购买 nike

我的数据中有一列是数字列表,但在保存为 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)

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

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

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

最佳答案

使用 literal_eval() 是一个很好的方法。问题是它需要单独应用于每个子列表(字符串)。 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 而不是 tuplelist,您可以使用:

>>> 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.

关于python - 将颜色的字符串表示形式转换回列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57031596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com