将 Python 列表转换为"lists of lists"



我想创建一个"列表列表";list对象。

这样我就可以从这个列表中初始化Pandas DataFrame。

这是我的代码:

import os
import re
result = []
final_output = []
output = os.popen('kubectl get namespaces').read()
result = output.split('n')
def remove(string):
pattern = re.compile(r's+')
return re.sub(pattern, ',', string)
for item in result:
final_output.append(remove(item))
print(final_output)

问题是,我的数据目前采用以下列表格式:

[
'[resight,True,345]', 
'[creekstuff,True,345]', 
'[digoneutism,True,567]', 
'[directorates,True,354]', 
'[bedsheet,True,499]'
]

我希望它显示为列表如下:

[
['resight','True','345'], 
['creekstuff','True','345'], 
['digoneutism','True','567'], 
['directorates','True','354'], 
['bedsheet','True','499']
]

因此,似乎需要做的是:

  1. 将单引号放在方括号外
  2. 每个单词都用一句引号括起来

但是,如何实现这一点?

由于它们遵循一个非常特定的模式,您可以通过切片操作去掉括号,然后将逗号分隔的字符串转换为具有split:的列表

>>> data = [
... '[resight,True,345]',
... '[creekstuff,True,345]',
... '[digoneutism,True,567]',
... '[directorates,True,354]',
... '[bedsheet,True,499]'
... ]
>>> [i[1:-1].split(',') for i in data]
[['resight', 'True', '345'], ['creekstuff', 'True', '345'], ['digoneutism', 'True', '567'], ['directorates', 'True', '354'], ['bedsheet', 'True', '499']]
list_of_string_list = [
'[resight,True,345]', 
'[creekstuff,True,345]', 
'[digoneutism,True,567]', 
'[directorates,True,354]', 
'[bedsheet,True,499]'
]
#for each element in list_of_string_list
#remove square brackets and split the string into a list using comma as delimeter
list_of_lists = [el.replace("[", "").replace("]", "").split(",") for el in list_of_string_list]
print(list_of_lists)
#OUTPUTS [['resight', 'True', '345'], ['creekstuff', 'True', '345'], ['digoneutism', 'True', '567'], ['directorates', 'True', '354'], ['bedsheet', 'True', '499']]

您还可以使用重模块

import re
data = [
'[resight,True,345]', 
'[creekstuff,True,345]', 
'[digoneutism,True,567]', 
'[directorates,True,354]', 
'[bedsheet,True,499]'
]
list_of_lists = [re.findall(r'b(w+)b', l) for l in data]

说明:

  • re.findall-搜索给定模式的所有匹配项
  • b(w+)b-匹配任何单词字符([a-zA-Z0-9_](,并确保在(b(之前和之后没有单词字符

最新更新