【发布时间】:2015-05-08 01:49:03
【问题描述】:
我想知道是否有更有效的方法来引用列表/字典中每个项目/键的每个索引。 Here's a bigger sample dictionary.
raw_dict = {'atgc': 1, 't': 0, 'gcccctttc': 1, 'cttc': 1}
sorted_list = sorted(list(raw_dict))
translation = dict()
i_all = 0
for i_list, item in enumerate(sorted_list):
for i_item in range(len(item)):
translation[i_all] = ([i_list, i_item])
i_all += 1
print sorted_list
# output ['atgc', 'cttc', 'gcccctttc', 't']
print translation
# output {0: [0, 0], 1: [0, 1], 2: [0, 2], 3: [0, 3], 4: [1, 0], 5: [1, 1], 6: [1, 2], 7: [1, 3], 8: [2, 0], 9: [2, 1], 10: [2, 2], 11: [2, 3], 12: [2, 4], 13: [2, 5], 14: [2, 6], 15: [2, 7], 16: [2, 8], 17: [3, 0]}
索引 'i_all' 类似于假设的字符串 'atgccttcgcccctttct',加入排序后的 'raw_dict' 键
我想使用所有字符的索引来为“raw_dict”中的键创建具有可变长度和可变起始索引的子字符串。但是,我实际上不能将所有键作为字符串连接,因为这可能会生成不存在的子字符串。
【问题讨论】:
-
这对我来说没有任何意义。您提供的“产生类似”的输出似乎与您的示例字典无关。我不知道你想做什么。 “索引所有字符”或“引用每个索引”是什么意思?我理解每个单词,但句子没有任何意义。用奇怪的短语“列表/字典的项目/键”澄清你的意思。至少尝试在不使用 / 的情况下重写它。
-
我明白了。我现在正在更新问题。
-
我没有完全理解这个问题。但我相信收藏模块可以在这里为您提供帮助。
标签: python list python-2.7 dictionary indexing