搜索python列表中的子字符串,然后从列表中删除它

时间:2013-10-17 15:15:43

标签: python list search

我有一个python列表,如:

['A /cloudos-ui/branches/bare_metal', 
'M /cloudos-ui/branches/bare_metal/packaging/DEBIAN/control', 
'M /cloudos-ui/branches/bare_metal/create_cosmos_installer.sh', 
'M /cloudos-ui/branches/bare_metal/packaging/DEBIAN/control', 
'M /cloudos-ui/branches/bare_metal/scripts/create_skyline_installer.sh', 
'M /cloudos-ui/branches/bare_metal/forms.py', 
'M /cloudos-ui/branches/bare_metal/tables.py', 
'M /cloudos-ui/branches/bare_metal/templates/flavors/_create.html',
'M /cloudos-ui/branches/bare_metal/templates/flavors/_edit.html',
'A /cloudos-ui/branches/bare_metal/scripts/create_skyline_installer.sh',
'A /cloudos-ui/branches/bare_metal/forms.py', 
'A /cloudos-ui/branches/bare_metal/tables.py', 
'A /cloudos-ui/branches/bare_metal/templates/flavors/_create.html']

现在每个字符串的第一个字符是AM。除了第一个字母,一些字符串是相同的:例如两个字符串以create_skyline_installer.sh结尾,第五个字符串从顶部开始,第四个字符串从底部开始。

现在我想要实现的是找到字符串,其中AM之后字符串的其余部分是相同的。然后我想删除以M开头的列表元素。

我的意思是最终列表应如下所示:

['A /cloudos-ui/branches/bare_metal', 
'M /cloudos-ui/branches/bare_metal/packaging/DEBIAN/control', 
'M /cloudos-ui/branches/bare_metal/create_cosmos_installer.sh', 
'M /cloudos-ui/branches/bare_metal/packaging/DEBIAN/control', 
'M /cloudos-ui/branches/bare_metal/scripts/create_skyline_installer.sh',
'M /cloudos-ui/branches/bare_metal/forms.py', 
'M /cloudos-ui/branches/bare_metal/tables.py', 
'M /cloudos-ui/branches/bare_metal/templates/flavors/_create.html',
'M /cloudos-ui/branches/bare_metal/templates/flavors/_edit.html']

但我无法找到解决方案。

请帮帮我。

1 个答案:

答案 0 :(得分:1)

尝试对列表进行排序,其中A元素将在M元素之前处理,并使用字典保存它们,因此第一个条目将被覆盖。使用reverse=True保留A条目。

lst = ['A /cloudos-ui/branches/bare_metal (from /cloudos-ui/branches/rc4g1build:9930)',                            
'M /cloudos-ui/branches/bare_metal/misc/cosmos/packaging/DEBIAN/control', 
'M /cloudos-ui/branches/bare_metal/misc/cosmos/scripts/create_cosmos_installer.sh', 
'M /cloudos-ui/branches/bare_metal/misc/skyline/packaging/DEBIAN/control', 
'M /cloudos-ui/branches/bare_metal/misc/skyline/scripts/create_skyline_installer.sh',
'M /cloudos-ui/branches/bare_metal/src/horizon/openstack_dashboard/dashboards/admin/flavors/forms.py', 
'M /cloudos-ui/branches/bare_metal/src/horizon/openstack_dashboard/dashboards/admin/flavors/tables.py', 
'M /cloudos-ui/branches/bare_metal/src/horizon/openstack_dashboard/dashboards/admin/flavors/templates/flavors/_create.html',
'M /cloudos-ui/branches/bare_metal/src/horizon/openstack_dashboard/dashboards/admin/flavors/templates/flavors/_edit.html',
'A /cloudos-ui/branches/bare_metal/misc/skyline/scripts/create_skyline_installer.sh',
'A /cloudos-ui/branches/bare_metal/src/horizon/openstack_dashboard/dashboards/admin/flavors/forms.py', 
'A /cloudos-ui/branches/bare_metal/src/horizon/openstack_dashboard/dashboards/admin/flavors/tables.py', 
'A /cloudos-ui/branches/bare_metal/src/horizon/openstack_dashboard/dashboards/admin/flavors/templates/flavors/_create.html']

d = {}

for elem in sorted(lst):
    l = elem.split(None, 1)
    d[l[1]] = elem

print(list(d.values()))

它产生:

['A /cloudos-ui/branches/bare_metal (from /cloudos-ui/branches/rc4g1build:9930)',
 'M /cloudos-ui/branches/bare_metal/src/horizon/openstack_dashboard/dashboards/admin/flavors/templates/flavors/_edit.html',
 'M /cloudos-ui/branches/bare_metal/src/horizon/openstack_dashboard/dashboards/admin/flavors/forms.py',
 'M /cloudos-ui/branches/bare_metal/src/horizon/openstack_dashboard/dashboards/admin/flavors/tables.py',
 'M /cloudos-ui/branches/bare_metal/misc/cosmos/packaging/DEBIAN/control',
 'M /cloudos-ui/branches/bare_metal/src/horizon/openstack_dashboard/dashboards/admin/flavors/templates/flavors/_create.html',
 'M /cloudos-ui/branches/bare_metal/misc/skyline/packaging/DEBIAN/control',
 'M /cloudos-ui/branches/bare_metal/misc/cosmos/scripts/create_cosmos_installer.sh',
 'M /cloudos-ui/branches/bare_metal/misc/skyline/scripts/create_skyline_installer.sh']