胡说叔叔
我认为有一种更优雅的方式来写这个。我还没有找到时间来获取这个。所以这里有一个快速但肮脏的版本,但仍然对你有用。x = [['black', 'sugar', 'second', 'dog'], ['red', 'cane', 'five', 'pig'], ['black', 'sugar', 'first', 'cat'], ['pink', 'cute', 'one', 'snake'], ['pink', 'angry', 'one', 'donkey'], ['black', 'sugar', 'third', 'bird'], ['red', 'cane', 'five', 'monkey'], ['pink', 'angry', 'one', 'spider']]x.sort()print (x[0][0])print (' ' + x[0][1])print (' 1 ' + x[0][2])print (' 1a '+x[0][3])level_2 = 1level_3 = 'a'for i,a in enumerate(x[1:]): if a[0] == x[i][0]: if a[1] == x[i][1]: if a[2] == x[i][2]: level_3 = chr(ord(level_3) + 1) else: level_2 +=1 level_3 = 'a' print (' ' + str(level_2) + ' ' + a[2]) else: level_2 +=1 level_3 = 'a' print (' ' + a[1]) print (' ' + str(level_2) + ' ' + a[2]) else: level_2 +=1 level_3 = 'a' print (a[0]) print (' ' + a[1]) print (' ' + str(level_2) + ' ' + a[2]) print (' ' + str(level_2) + level_3 + ' ' + a[3])其输出是:black sugar 1 first 1a cat 2 second 2a dog 3 third 3a birdpink angry 4 one 4a donkey 4b spider cute 5 one 5a snakered cane 6 five 6a monkey 6b pig