0
点赞
收藏
分享

微信扫一扫

检测相邻相同元素的索引位置 以供合并单元格

书坊尚 2023-09-06 阅读 52

# for i, value_i in enumerate(list_a):
#     list_intercept = list_a[i: ]
#     j = [k for (k, value_k) in enumerate(list_intercept) if (value_k == value_i)][-1] + i
#     print(i, value_i, list_intercept, j, f'{i}: {j}', list_a[i: j+1], sep='\t')
#     print(sep='\n')

list_example = ['A', 'A', 'B', 'B', 'C', 'C']
# 取唯一值
list_sorted = sorted(set(list_example), key=list_example.index) # set后需排序  # list(set(list_example)) == ['B', 'C', 'A']  # 不能用中括号转化[set]

dict_example = {key: value for key, value in enumerate(list_example)}
for i in list_sorted:
    dict_i = {key: value for key, value in dict_example.items() if (value == i)}
    print(dict_i, f"{list(dict_i.keys())[0]}: {list(dict_i.keys())[-1]}")

举报

相关推荐

0 条评论