简体   繁体   中英

Convert Python List into a "lists of lists"

I want to create a "list of lists" list object in Python.

So that I can initialize a Pandas DataFrame from this list of lists.

Here's my code:

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)

The problem is, my data is currently in the following list format:

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

I want it to appear as list of lists follows:

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

So, it seems what needs to be done is:

  1. Drop the single quote outside the square bracket
  2. Have a single quote wrap each word.

But, how to achieve this?

Since these follow a very specific pattern, you can get rid of the brackets with a slice operation, and then turn the comma-separated string into a list with 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']]

Also you can use re module

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]

Explanation:

  • re.findall - searches for all matches for the given pattern;
  • \b(\w+)\b - matches any word character ([a-zA-Z0-9_]) and ensures that there is no word character before and after ( \b )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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