使用以下代码,我得到:
>>> for leaf in tree:
... print(leaf.name)
...
IDBA_scaffold_Genus1_species1
IDBA_scaffold_Genus1_species1
IDBA_scaffold_Genus1_species2
IDBA_scaffold_Genus3_species1
IDBA_scaffold_Genus3_species1
,我想做些类似的事情:
for leaf in tree:
if duplicate leaf.name:
then add an index to this leaf.name
leaf.name = leaf.name+index
然后以我为例:
>>> for leaf in tree:
... print(leaf.name)
...
IDBA_scaffold_Genus1_species1_index1
IDBA_scaffold_Genus1_species1_index2
IDBA_scaffold_Genus1_species2
IDBA_scaffold_Genus3_species1_index1
IDBA_scaffold_Genus3_species1_index2
leaf中的leaf.name也可以在列表或列中。
有人有主意吗?
答案 0 :(得分:0)
# When comparing, record the previous name
prev_name = None
index = 0
for leaf in tree:
# Skip the first one, starting with the second one, comparing whether the first two names are consistent
if prev_name is not None:
if prev_name == leaf.name:
index += 1
print(prev_name + '_index' + str(index))
else:
# If the two are not equal, and the index is greater than 0, the previous name is duplicated before, but it is not duplicated with the current name.
if index > 0:
print(prev_name + '_index' + str(index + 1))
index = 0
else:
print(prev_name)
prev_name = leaf.name
# Finally, the last data in the traversal is processed.
print(prev_name + '_index' + str(index + 1) if index > 0 else prev_name)
编辑:添加注释