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

时间:2017-05-29 18:08:54

标签: python string list dictionary

我有下一个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'

我需要将string的每个元素放入list1[0][..],但当我看到一个新行'\ n'时,我必须将下一个元素放入list1[1][..] < / p>

多维列表,如下所示:

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

我是用split做的,但它让我所有人处于同一个维度。

6 个答案:

答案 0 :(得分:6)

首先按新行拆分(获取行),然后按空格拆分每个元素(以获取每列):

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

答案 1 :(得分:1)

以下功能应该为您执行此操作:

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

只需通过f(string)调用即可。

如果你不想要你的子列表中的任何空条目,你可以做

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

答案 2 :(得分:1)

输入: -

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'

代码: - 只需写

 mylist=string.split()

输出: -

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

答案 3 :(得分:0)

既然你想将字符串拆分成行,那么将这些行转换为单词,就可以使用理解

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

输出:

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

如果您不想要最后一个空白(因为上一个\n),您可以在strip之前split {。}}。

答案 4 :(得分:0)

Yo可以使用列表推导如下:

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

输出:

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

答案 5 :(得分:0)

由于你的帮助,我曾尝试用列表中的值来做字典,这很棒。

在这里它是有效的。我开始爱上列表理解了!

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

输出:

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