0

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

现在每个字符串的第一个字符是Aor M。除了这个第一个字母之外,一些字符串是相同的:例如以 结尾的两个字符串create_skyline_installer.sh,从顶部算起第 5 个,从底部算起第 4 个。

现在我想要实现的是找到字符串之后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']

但我无法找到解决方案。

请帮帮我。

4

1 回答 1

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']
于 2013-10-17T15:26:59.790 回答