重新排序列表并查找所有可能的组合

时间:2017-07-22 23:48:50

标签: python list python-3.x combinations

我有一个包含可变数量元素的Python列表。

[a,b,c,d,e]

我需要在列表中重新排列这些元素,并找到所有可能的独特组合。

[a,b,c,d,e]
[d,b,a,e,c]
[e,d,a,c,b], etc.

我不能重复相同的元素,或删除任何元素:

[a,a,a,d,e]  <-- bad

2 个答案:

答案 0 :(得分:1)

您需要itertools.permutations ([a,b,c,d,e])

答案 1 :(得分:1)

您应该使用def _select_event(self, type, id): """ Selects an event for editing its properties """ if not self._can_draw(): return if type != self.event_type: #Destroy all widgets on the frame and build those neccessary for widget in self.event_edit_widgets: widget.grid_remove() self.event_edit_widgets = [] self.event_type = type if self.event_type == "Person": #Build widgets for person if len(self.map.persons) <= id: return self.event_edit_widget_id_spinbox = tkinter.Spinbox(self.event_edit_widget, from_=0, to=len(self.map.persons) - 1, command=lambda: self._select_event(self.event_type, self.event_edit_type_dropdown.get())) elif self.event_type == "Warp": #Build widgets for warp if len(self.map.warps) <= id: return self.event_edit_widget_id_spinbox = tkinter.Spinbox(self.event_edit_widget, from_=0, to=len(self.map.warps) - 1, command=lambda: self._select_event(self.event_type, self.event_edit_type_dropdown.get())) elif self.event_type == "Trigger": #Build widgets for trigger if len(self.map.triggers) <= id: return self.event_edit_widget_id_spinbox = tkinter.Spinbox(self.event_edit_widget, from_=0, to=len(self.map.triggers) - 1, command=lambda: self._select_event(self.event_type, self.event_edit_type_dropdown.get())) elif self.event_type == "Sign": #Build widgets for sign if len(self.map.signposts) <= id: return self.event_edit_widget_id_spinbox = tkinter.Spinbox(self.event_edit_widget, from_=0, to=len(self.map.signposts) - 1, command=lambda: self._select_event(self.event_type, self.event_edit_type_dropdown.get())) else: raise Exception("Unkown event type selected: " + self.event_type) #Push all common widgets to the ui self.event_edit_widget_id_spinbox.grid(row=1, column=0, sticky=tkinter.NW) self.event_edit_widget_id_spinbox.config(state="readonly") self.event_edit_widgets.append(self.event_edit_widget_id_spinbox) #Update all widgets self.event_edit_widget_id_spinbox.config(state="normal") self.event_edit_widget_id_spinbox.delete(0, tkinter.END) self.event_edit_widget_id_spinbox.insert(0, str(id)) self.event_edit_widget_id_spinbox.config(state="readonly") ,例如:

itertools.permutations