我想从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