reads = { '1': 'A', '2': 'B', '3': 'C', '4': 'D', '5': 'E', '6': 'F' }`
readOrder = ['1', '2', '3', '4', '5', '6']`
如何在readOrder
中迭代字符串:
a = []
for i in readOrder():
a.append(reads[i],reads[i+1])
''.join(a)
print a
所以我可以得到:
'ABBCCDDEEF'
这种方式有效,但我必须有一个简单的方法来使用for循环:
a = [reads[0] + reads[1], reads[1] + reads[2],
reads[2] + reads[3], reads[3] + reads[4]]
print ''.join(a)
答案 0 :(得分:0)
a = []
for i,key in enumerate(readOrder):
a.append(reads[key])
if i != 0 and i != len(readOrder) - 1:
a.append(reads[key])
''.join(a)
答案 1 :(得分:-1)
也许是这样的? (修改为字符串)
#!/usr/local/cpython-2.7/bin/python
reads = { '1': 'A', '2': 'B', '3': 'C', '4': 'D', '5': 'E', '6': 'F' }
readOrder = ['1', '2', '3', '4', '5', '6']
a = []
for indexno, index_str in enumerate(readOrder[:-1]):
index_str_p1 = readOrder[indexno + 1]
a.append(reads[index_str])
a.append(reads[index_str_p1])
print a
print ''.join(a)