[英]How to convert string of list of list to list?
I have this file, it is the result of the MapReduce job so it has key-value
format:我有这个文件,它是 MapReduce 作业的结果,因此它具有
key-value
格式:
'null\t[0, [[0, 21], [1, 4], [2, 5]]]\n'
'null\t[1, [[0, 3], [1, 1], [2, 2]]]\n'
I want to remove all the character except the second element of this value list:我想删除除此值列表的第二个元素之外的所有字符:
[[0, 21], [1, 4], [2, 5]]
[[0, 3], [1, 1], [2, 2]]
And finally, add each to a single list:最后,将每个添加到一个列表中:
[[[0, 21], [1, 4], [2, 5]], [[0, 3], [1, 1], [2, 2]]]
This is my attempt so far:这是我迄今为止的尝试:
with open(FILENAME) as f:
content = f.readlines()
for line in content:
# Just match all the chars upto "[[" then replace the matched chars with "["
clean_line = re.sub(r'^.*?\[\[', '[', line)
# And remove "\n" and the last 2 "]]" of the string
clean_line = re.sub('[\n]', '', clean_line)[:-2]
corpus.append(clean_line)
Output: Output:
['[0, 21], [1, 4], [2, 5]', '[0, 3], [1, 1], [2, 2]']
You can see it is still str
type, how can I make it to list
type?你可以看到它仍然是
str
类型,我怎样才能使它成为list
类型?
Treat it as a line of json and just replace parts of your lines with json documents as needed将其视为 json 行,并根据需要将部分行替换为 json 文档
import json
corpus = [json.loads(line.replace('null\t', '{"a":').replace("\n", "}"))["a"][1] for line in content]
At the end, you can convert representations of list to List object by using ast
like this:最后,您可以使用
ast
将列表的表示形式转换为列表 object,如下所示:
import ast
sample = ['[0, 21], [1, 4], [2, 5]', '[0, 3], [1, 1], [2, 2]']
result = []
for item in sample:
result.append(list(ast.literal_eval(item)))
And this is the result
containing the desired elements:这是包含所需元素的
result
:
[[[0, 21], [1, 4], [2, 5]], [[0, 3], [1, 1], [2, 2]]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.