1

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?

1
  • 1
    But importantly, where did such a list come from? Commented Dec 9, 2021 at 16:33

3 Answers 3

2

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']]
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.