1

I have a list like this:

  hello = [[<User: mr--adhikari>], [<User: mr--adhikari>], [<User: mr--adhikari>, <User: awesome>]]

I want to convert the list to look like this:

[['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari', 'awesome']]

Tried with:

re = [i.split(':')[1] for i in hello]
ref = [i.split('>')[0] for i in ref]

This works if I have only don't have two elements in a same list. I may or may not have two elements in a same list.

Edit: My dynamic list:

   shared_username = ['[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>, <User: awesome>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]']

shared_username = [[y.lstrip('<User: ').rstrip('>') for y in x] for x in shared_username]

Result:

[['[', '', '', '', '', '', '', '', 'm', '', '-', '..so on
4

2 回答 2

1

使用sts.lstrip和的组合str.rstrip

演示:

>>> strs = '[<User: mr--adhikari>]'
>>> strs = strs.lstrip('[<User: ')
>>> strs
'mr--adhikari>]'
>>> strs = strs.rstrip('>]')
>>> strs
'mr--adhikari'

代码:

>>> hello = [['<User: mr--adhikari>'], ['<User: mr--adhikari>'], ['<User: mr--adhikari>', '<User: awesome>']]
>>> [ [y.lstrip('<User: ').rstrip('>') for y in x] for x in hello ]
[['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari', 'awesome']]

更新:

>>> shared_username = ['[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>, <User: awesome>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]']

>>> [[y.lstrip('[<User: ').rstrip('>]') for y in x.split(', ')] for x in shared_username]
[['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari', 'awesome'], ['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari']]

演示:

strs = '[<User: mr--adhikari>, <User: awesome>]'

首先拆分字符串', '以获取字符串列表,现在循环此列表并应用str.lstripstr.rstrip如上所述。

>>> strs.split(', ')
['[<User: mr--adhikari>', '<User: awesome>]']

更新:

使用regex

演示:

>>> import re
>>> r = re.compile(r'User:\s([A-Za-z\-._/@]+)')
>>> strs = '[<User: sachitad>, <User: awesome>]'
>>> r.findall(strs)
['sachitad', 'awesome']
>>> strs1 = '[<User: mr--adhikari>, <User: awesome>]'
>>> r.findall(strs1)
['mr--adhikari', 'awesome']
>>> strs2 = '[<User: respect>]'
>>> r.findall(strs2)
['respect']
>>> strs = '[<User: sa@chi.tad>, <User: awe_so/me>]'
>>> r.findall(strs)
['sa@chi.tad', 'awe_so/me']

代码:

>>> shared_username = ['[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>, <User: awesome>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]']
>>> [r.findall(item) for item in shared_username]
[['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari', 'awesome'], ['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari']]
于 2013-06-24T07:02:46.280 回答
0

只是为了确认,嵌套列表中的元素是字符串还是字典?如果您不确定运行以下命令来检查第一个: print type(hello[0][0])

更新:

这是另一种选择,它以您最初请求的方式格式化输出(没有“用户:”)。不是最简洁的语法,但它似乎有效:

def get_user_data(lst, strip_str):
    new_list = []
    for i in range(len(lst)):
        tmp = lst[i]
        for j in strip_str:
            if j in tmp:
                tmp = tmp.replace(j,'')
        tmp = tmp.split(',')
        new_list.append(tmp)
    return new_list

shared_username = ['[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>, <User: awesome>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]', '[<User: mr--adhikari>]']
new_list = get_user_data(shared_username,['<','>',' ','[',']','User:'])
print new_list

输出:[['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari', 'awesome' ], ['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari'], ['mr--adhikari']]

于 2013-06-24T07:10:23.570 回答