-1

我正在尝试删除列表中的重复项my_list

In [13]: my_list
Out[13]: [[(-7.7725138974675732, -79.168560840024711),
  (-7.7725138974675732, -79.168560840024711),
  (-5.5, -59.0),
  (-4.4, -48.3),
  (-3.2, -37.5),
  (-2.1, -26.9),
  (-0.9, -16.2),
  (2.7974572775821454, 4.4251549647685868),
  (2.7974572775821454, 4.4251549647685868)],
 [(2.7974572775821454, 4.4251549647685868),
  (2.7974572775821454, 4.4251549647685868),
  (29.8, 16.4),
  (41.8, 16.9),
  (53.1, 17.2),
  (74.767340748230566, 15.947522750252775),
  (74.767340748230566, 15.947522750252775),
  (74.767340748230566, 15.947522750252775),
  (97.8, 13.4),
  (109.2, 14.9),
  (125.57323627494615, 19.53393479479609)],
 [(68.773124224303274, -79.616585806160444),
  (68.773124224303274, -79.616585806160444),
  (71.141449110088374, -58.675607868692225),
  (73.874255065554237, -27.94398092967819),
  (73.874255065554237, -27.94398092967819),
  (73.874255065554237, -27.94398092967819),
  (74.5, -2.9),
  (74.767340748230566, 15.947522750252775)],
 [(-109.09700131981562, -32.09602243732722),
  (-108.3, -32.7),
  (-81.9, -52.7),
  (-64.94343179027949, -62.943395681375023),
  (-64.94343179027949, -62.943395681375023),
  (-44.8, -70.5),
  (-32.2, -74.1),
  (-7.7725138974675732, -79.168560840024711),
  (-7.7725138974675732, -79.168560840024711),
  (10.6, -80.1),
  (21.6, -80.4),
  (32.8, -80.6),
  (43.9, -80.6),
  (68.773124224303274, -79.616585806160444),
  (68.773124224303274, -79.616585806160444),
  (68.773124224303274, -79.616585806160444),
  (94.59403067734813, -75.99089907199837)],
 [(-57.743037814594651, 0.48281269760971313),
  (-57.743037814594651, 0.48281269760971313),
  (-61.5, -30.9),
  (-62.9, -42.2),
  (-64.94343179027949, -62.943395681375023),
  (-64.94343179027949, -62.943395681375023)],
 [(73.874255065554237, -27.94398092967819),
  (73.874255065554237, -27.94398092967819),
  (113.3911596081064, -33.232674908171724)],
 [(71.141449110088374, -58.675607868692225),
  (71.141449110088374, -58.675607868692225)],
 [(2.7974572775821454, 4.4251549647685868),
  (-17.6, 3.5),
  (-57.743037814594651, 0.48281269760971313),
  (-57.743037814594651, 0.48281269760971313)]]

然后,我做my_list = list(set(my_list))

In [20]: my_list
Out[20]: [[(-7.7725138974675732, -79.168560840024711),
  (-3.2, -37.5),
  (-2.1, -26.9),
  (-5.5, -59.0),
  (-4.4, -48.3),
  (-0.9, -16.2),
  (2.7974572775821454, 4.4251549647685868)],
 [(109.2, 14.9),
  (41.8, 16.9),
  (97.8, 13.4),
  (29.8, 16.4),
  (53.1, 17.2),
  (125.57323627494615, 19.53393479479609),
  (74.767340748230566, 15.947522750252775),
  (2.7974572775821454, 4.4251549647685868)],
 [(73.874255065554237, -27.94398092967819),
  (74.5, -2.9),
  (74.767340748230566, 15.947522750252775),
  (68.773124224303274, -79.616585806160444),
  (71.141449110088374, -58.675607868692225)],
 [(-108.3, -32.7),
  (-7.7725138974675732, -79.168560840024711),
  (-44.8, -70.5),
  (-81.9, -52.7),
  (10.6, -80.1),
  (-64.94343179027949, -62.943395681375023),
  (94.59403067734813, -75.99089907199837),
  (-109.09700131981562, -32.09602243732722),
  (68.773124224303274, -79.616585806160444),
  (32.8, -80.6),
  (-32.2, -74.1),
  (21.6, -80.4),
  (43.9, -80.6)],
 [(-62.9, -42.2),
  (-64.94343179027949, -62.943395681375023),
  (-57.743037814594651, 0.48281269760971313),
  (-61.5, -30.9)],
 [(73.874255065554237, -27.94398092967819),
  (113.3911596081064, -33.232674908171724)],
 [(71.141449110088374, -58.675607868692225)],
 [(-17.6, 3.5),
  (-57.743037814594651, 0.48281269760971313),
  (2.7974572775821454, 4.4251549647685868)]]

可以看出,数据的顺序发生了变化。顺序对我很重要。所以我必须在手术后维持秩序。

为什么会这样?如何维持秩序?

4

2 回答 2

3

正如@Cameronsparr 所说,您之所以订购是因为集合本质上是 unordered

为了维持秩序,我会做这样的事情:

old_list = [...]
new_list = []
tmp_set = set()
for el in old_list:
     if el not in tmp_set:
          tmp_set.add(el)
          new_list.append(el)

如何在保留顺序的同时从列表中删除重复项?有一种更巧妙的方式来做到这一点。

于 2013-10-19T02:10:28.620 回答
3

订单被破坏是因为set,本质上是一个无序的集合(编辑,谢谢)

关于维护顺序:如何在保持顺序的同时从列表中删除重复项?

于 2013-10-19T02:05:47.413 回答