Home > Software design >  Convert Python List into a "lists of lists"
Convert Python List into a "lists of lists"

Time:12-10

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?

CodePudding user response:

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']]

CodePudding user response:

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']]

CodePudding user response:

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)
  • Related