乔恩·克莱门斯的解决方案是正确的,但这里有另一个解决方案。在
为了保持秩序,我不得不把钥匙单列出来。如果我用vdCacheType.keys()列出它们,它们是按如下顺序排列的:['R', 'C', 'WT', 'WB', 'NR', 'AWB', 'D', 'RA']这行不通。在
str.strip()在这种情况下不起作用,因为字符串之间没有空格。在vdCacheType = {'AWB' : 'Always WriteBack', 'WB': 'Write Back',
'NR': 'No Read Ahead', 'RA': 'Read Ahead Adaptive',
'WT': 'Write Through', 'R' : 'Read Ahead Always',
'D': 'Direct IO', 'C': 'Cached' }
vdCacheKeys = ['AWB','WB','NR','RA','WT','R','D','C']
mystr = 'NRWTD'
my2str = 'RAWBC'
listAbbr = []
result = ''
index = 0
print vdCacheType.keys()
for x in vdCacheKeys:
if x in mystr:
listAbbr.append(x)
index = mystr.find(x)
mystr = mystr[:index]+' ' + mystr[index +len(x):]
print mystr
result+=vdCacheType[x] + ', '
# print x # Here i would save the found abbr. in a list for later use
print result
输出No Read Ahead, Write Through, Direct IO,