koteo
koteo

Reputation: 3

How to convert this string to a multi-dimensional list in Python?

I have the next string:

string = 'tuned     1372                root    6u      REG                8,3      4096  102029349 /tmp/ffiabNswC (deleted)\ngmain     1372 2614           root    6u      REG                8,3      4096  102029349 /tmp/ffiabNswC (deleted)\n'

I need to put every element of the string into the list1[0][..], but when I see a new line '\n', I have to put the next elements into list1[1][..]

A multi-dimensional list, like this:

list1 = [["tuned", "1372", "root", "6u", "REG", "8,3", "4096", "102029349", "/tmp/ffiabNswC", "(deleted)"], 
         ["gmain", "1372", "2614", "root", "6u", "REG", "8,3", "4096", "102029349", "/tmp/ffiabNswC", "(deleted)"]]

I do it with split, but it put me all in the same dimension.

Upvotes: 0

Views: 2579

Answers (6)

koteo
koteo

Reputation: 3

Due to your help, I´ve tried to do a dictionary with the values inside a list and it´s great.

Here it is and it works. I´m starting to fall in love with list comprehension!

dict1 = { i.split()[0]: (i.split()[1:]) for i in data.strip().split('\n') }

Output:

dict1
{'gmain': ['1372',
 '2614',
 'root',
 '6u',
 'REG',
 '8,3',
 '4096',
 '102029349',
 '/tmp/ffiabNswC',
 '(deleted)'],
'tuned': ['1372',
 'root',
 '6u',
 'REG',
 '8,3',
 '4096',
 '102029349',
 '/tmp/ffiabNswC',
 '(deleted)']}

Upvotes: 0

Vic13
Vic13

Reputation: 561

Input:-

string = 'tuned 1372 root 6u REG 8,3 4096 102029349 /tmp/ffiabNswC 
(deleted)\ngmain 1372 2614 root 6u REG 8,3 4096 102029349 /tmp/ffiabNswC 
(deleted)\n'

Code: - Just write

 mylist=string.split()

Output:-

[tuned
 1372
 root
6u
REG
8,3
4096
102029349
/tmp/ffiabNswC
(deleted)
gmain
1372
2614
root
6u
REG
8,3
4096
102029349
/tmp/ffiabNswC
(deleted)]

Upvotes: 1

Mohd
Mohd

Reputation: 5613

Yo can use list comprehensions as the following:

string = 'tuned     1372                root    6u      REG                8,3      4096  102029349 /tmp/ffiabNswC (deleted)\ngmain     1372 2614           root    6u      REG                8,3      4096  102029349 /tmp/ffiabNswC (deleted)\n'
print [x.split() for x in string.strip().split('\n')]

output:

[['tuned', '1372', 'root', '6u', 'REG', '8,3', '4096', '102029349', '/tmp/ffiabNswC', '(deleted)'], ['gmain', '1372', '2614', 'root', '6u', 'REG', '8,3', '4096', '102029349', '/tmp/ffiabNswC', '(deleted)']]

Upvotes: 0

chthonicdaemon
chthonicdaemon

Reputation: 19820

Since you want to split the string into lines, then the lines into words, you can use a comprehension

result = [line.split() for line in string.split('\n')]

Output:

[['tuned', '1372', 'root', '6u', 'REG', '8,3', '4096', '102029349', '/tmp/ffiabNswC', '(deleted)'],
 ['gmain', '1372', '2614', 'root', '6u', 'REG', '8,3', '4096', '102029349', '/tmp/ffiabNswC', '(deleted)'],
 []]

If you don't want that final blank (because of the last \n) you can just strip it before splitting.

Upvotes: 0

xjcl
xjcl

Reputation: 15329

The following function should do this for you:

f = lambda list: [sublist.split(' ') for sublist in list.split('\n')]

Just call it via f(string).

If additionally you don't want any empty entries in your sub-lists you could do

f = lambda list: [sublist.split(' ') for sublist in list.split('\n') if sublist]

Upvotes: 1

zwer
zwer

Reputation: 25809

Split first by a new line (to get the row), then split each element by space (to get each column):

data = "tuned 1372 root 6u REG 8,3 4096 102029349 /tmp/ffiabNswC (deleted)\ngmain 1372 2614 root 6u REG 8,3 4096 102029349 /tmp/ffiabNswC (deleted)\n"

parsed = [elements.split() for elements in data.strip().split("\n")]  # `strip()` removes the last whitespace so we don't get blank elements

print(parsed)

# [['tuned', '1372', 'root', '6u', 'REG', '8,3', '4096', '102029349', '/tmp/ffiabNswC', '(deleted)'], ['gmain', '1372', '2614', 'root', '6u', 'REG', '8,3', '4096', '102029349', '/tmp/ffiabNswC', '(deleted)']]

Upvotes: 6

Related Questions