如何以给定格式转换此列表?

时间:2013-06-24 06:59:22

标签: python python-2.x

我有一个这样的清单:

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

我想将列表转换为如下所示:

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

试过:

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

如果我在同一个列表中只有两个元素,则此方法有效。我可能有也可能没有同一个列表中的两个元素。

修改: 我的动态清单:

   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]

结果:

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

2 个答案:

答案 0 :(得分:1)

使用sts.lstripstr.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']]

答案 1 :(得分:0)

要确认,嵌套列表中的元素是字符串还是字典?如果您不确定运行以下内容来检查第一个:打印类型(hello [0] [0])

更新:

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

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