Python str list to list

Posted bug_x

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python str list to list相关的知识,希望对你有一定的参考价值。

eg1:

My csv data like this below,i wt count  list nums
2018上,"[‘C‘, ‘B‘, ‘B‘, ‘B‘, ‘D‘, ‘A‘, ‘B‘, ‘C‘, ‘A‘, ‘D‘, ‘B‘, ‘D‘, ‘A‘, ‘A‘, ‘A‘, ‘C‘, ‘D‘, ‘B‘, ‘C‘, ‘B‘, ‘A‘, ‘C‘, ‘A‘, ‘D‘, ‘A‘, ‘D‘, ‘B‘, ‘D‘, ‘B‘, ‘C‘, ‘B‘, ‘A‘, ‘D‘, ‘B‘, ‘C‘, ‘B、C‘, ‘B‘, ‘B‘, ‘B‘, ‘D‘, ‘B‘, ‘C‘, ‘C‘, ‘B‘, ‘B‘, ‘D‘, ‘A‘, ‘D‘, ‘D‘, ‘C‘, ‘B‘, ‘B‘, ‘A‘, ‘B‘, ‘C‘, ‘B‘, ‘B‘, ‘B‘, ‘A‘, ‘A‘, ‘A‘, ‘C‘, ‘A‘, ‘B‘, ‘A‘, ‘A、A‘, ‘B、C‘, ‘C‘, ‘D‘, ‘B‘, ‘C‘, ‘C‘, ‘A‘]"


eg:

df_data = np.array(pd.read_csv(answer_file, header=None))
    new_data = []
    for d in df_data:
        y = d[0]
        ya = d[1]      #  [\C\,\A\]
#(1)check one y2 = np.array(ya) # [‘C‘,‘A‘] #this is what i not want
y2_test = y2.shape # 0-d
  
y2_test2 = y2.tolist() # dtype shows not list

#the right methon
    #(2)check one
    ls = ya.strip(‘][‘).split(‘,‘)  # dtype show list 
#(3) right methon
import ast
ls = ast.iteral_eval(ya)
ls = [i.strip() for i in ls]


 














以上是关于Python str list to list的主要内容,如果未能解决你的问题,请参考以下文章

Python list 和 str 互转

Python3 字符串str和列表list转换

Python3基础 list str转成list

测试python类是不是为str、list等

python 数据迭代添加小技巧

python基础之数据类型转换