简体   繁体   中英

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.

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

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]

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

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 split ting.

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

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

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