列表元素操纵



假设我有一个列表,

['1-2', '1-3', '2-1', '3-2', '3-3', '6-1', '5-1', '4-1', '8-3', '8-2', '7-1', '9-1']

我需要输出唯一的元素,假设如果那里有1-2和1-3,我只需要输出1个实例,并且它们应以排序顺序,而无需A-。

样本输出:

1 2 
2 1
3 2
4 1
5 1
6 1
7 1
8 2
9 1
l = ['1-2', '1-3', '2-1', '3-2', '3-3', '6-1', '5-1', '4-1', '8-3', '8-2', '7-1', '9-1']
l.sort()
added = []
for line in l:
    itms = line.split("-")
    if itms[0] in added:
        continue
    added.append(itms[0])
    print("%s %s" % (itms[0], itms[1]))

最新更新