简体   繁体   English

在python中将一个列表或列表列表进行字符串化并将其转换回原始形式的问题

[英]issue in stringifying a list or list of list in python and converting it back to original form

I have been trying to stringify a list and list of list and then convert it back to it's original form. 我一直在尝试对listlist of list进行stringify ,然后将其转换回其原始形式。 Following are my 2 sample lists : 以下是我的两个示例lists

option_list = ['1309', '1P09', 'Bdg 1', '1226', 'Bdg 1']
option_listoflist = [['1309', 'Blg 1', 500], ['1P09', 'Bdg 1', 4501], ['1226', 'Bdg 1', 600], ['1302', 'Bdg 1', 1432]]

I wrote this python code, based on some SO posts, where I am trying to stringify the above 2 lists and then try to convert them back but it is throwing error: 我根据一些SO帖子编写了此python代码,在这里我试图对上述2个lists进行stringify ,然后尝试将它们转换回去,但是会引发错误:

str1 = ''.join(option_list)
print(str1+'\n')
str_2_list = ast.literal_eval(str1)
print(str_2_list)

str2 = ''.join(option_listoflist)
print(str2+'\n')
str_2_listoflist = ast.literal_eval(str2)
print(str_2_listoflist)

When I execute this I get invalid syntax error at str_2_list = ast.literal_eval(str1) . 执行此操作时,我在str_2_list = ast.literal_eval(str1)处收到invalid syntax错误。

How can I stringify a list and list of list and then convert it back to it's original form? 如何对listlist of list stringify ,然后将其转换回原始格式?

NOTE: What I want to do is to convert ['1309', '1P09', 'Bdg 1', '1226', 'Bdg 1'] to string version like this "['1309', '1P09', 'Bdg 1', '1226', 'Bdg 1']" and then convert it back to original list. 注意:我想做的就是将['1309', '1P09', 'Bdg 1', '1226', 'Bdg 1']为类似"['1309', '1P09', 'Bdg 1', '1226', 'Bdg 1']" ,然后将其转换回原始列表。 Similarly for a list of list 列表的列表类似

You are doing it wrong - conversion to string. 您做错了-转换为字符串。 Try str1 = str(options_list) 尝试str1 = str(options_list)

Since you seem to be trying to replicate JavaScript's JSON.stringify , just use the json module to do exactly that: 由于您似乎正在尝试复制JavaScript的JSON.stringify ,因此只需使用json模块即可:

In [1]: import json

In [2]: option_list = ['1309', '1P09', 'Bdg 1', '1226', 'Bdg 1']

In [3]: option_listoflist = [['1309', 'Blg 1', 500], ['1P09', 'Bdg 1', 4501], ['1226', 'Bdg 1', 600], ['1302', 'Bdg 1', 1432]]

In [4]: json.dumps(option_list)
Out[4]: '["1309", "1P09", "Bdg 1", "1226", "Bdg 1"]'

In [5]: json.dumps(option_listoflist)
Out[5]: '[["1309", "Blg 1", 500], ["1P09", "Bdg 1", 4501], ["1226", "Bdg 1", 600], ["1302", "Bdg 1", 1432]]'

In [6]: json.loads(json.dumps(option_list)) == option_list
Out[6]: True

In [7]: json.loads(json.dumps(option_listoflist)) == option_listoflist
Out[7]: True

Your ''.join(option_list) outputs '13091P09Bdg 11226Bdg 1' which can't be converted to list because the output string is not following list syntax. 您的''.join(option_list)输出'13091P09Bdg 11226Bdg 1' ,由于输出字符串未遵循列表语法,因此无法将其转换为list。

Crrected Code 改写代码

option_list = ['1309', '1P09', 'Bdg 1', '1226', 'Bdg 1']
option_listoflist = [['1309', 'Blg 1', 500], ['1P09', 'Bdg 1', 4501], ['1226', 'Bdg 1', 600],['1302', 'Bdg 1', 1432]]
str1 = ','.join(option_list)
print(str1+'\n')
str_2_list = str1.split(',')
print(str_2_list)

str2 = ','.join(str(item) for innerlist in option_listoflist for item in innerlist)
print(str2+'\n')
str_2_listoflist=[str2.split(',')[i:i+3] for i in range(0, len(str2.split(',')), 3)]
for i in str_2_listoflist:
  i[2]=int(i[2])
print(str_2_listoflist)

Output: 输出:

1309,1P09,Bdg 1,1226,Bdg 1 1309,1P09,建筑1,1226,建筑1

['1309', '1P09', 'Bdg 1', '1226', 'Bdg 1'] ['1309','1P09','Bdg 1','1226','Bdg 1']

1309,Blg 1,500,1P09,Bdg 1,4501,1226,Bdg 1,600,1302,Bdg 1,1432 1309,Blg 1,500,1P09,Bdg 1,4501,1226,Bdg 1,600,1302,Bdg 1,1432

[['1309', 'Blg 1', 500], ['1P09', 'Bdg 1', 4501], ['1226', 'Bdg 1', 600], ['1302', 'Bdg 1', 1432]] [['1309','Blg 1',500],['1P09','Bdg 1',4501],['1226','Bdg 1',600],['1302','Bdg 1', 1432]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM