我想從Python中的列表中提取所有的字符串組合。例如,在下面的代碼中,['a','b','c']和['b','a','c']不是唯一的,而['a','b',' c']和['a','e','f']或['a','b','c']和['d','e','f']是唯一的。從Python中的列表中提取唯一的字符串組合
我試過將列表列表轉換爲元組列表並使用集合來比較元素,但所有元素仍然被返回。
combos = [['a', 'b', 'c'], ['c', 'b', 'a'], ['d', 'e', 'f'], ['c', 'a', 'b'], ['c', 'f', 'b']]
# converting list of list to list of tuples, so they can be converted into a set
combos = [tuple(item) for item in combos]
combos = set(combos)
grouping_list = set()
for combination in combos:
if combination not in grouping_list:
grouping_list.add(combination)
##
print grouping_list
>>> set([('a', 'b', 'c'), ('c', 'a', 'b'), ('d', 'e', 'f'), ('c', 'b', 'a'), ('c', 'f', 'b')])
謝謝!是的,在我進行比較之前應該考慮分類。 – Bryan 2015-03-13 14:24:08