简体   繁体   English

如何在Python中将此字符串转换为多维列表?

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

I have the next string : 我有下一个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][..] 我需要将string每个元素放入list1[0][..] ,但是当我看到新行'\\ n'时,我必须将下一个元素放入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 ,但是这使我所有人处于同一维度。

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) . 只需通过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. 如果您不希望最后一个空格(由于最后一个\\n ),则可以在split之前strip它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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