2021年4月9日星期五

Unique combinations of (permuted) list

Consider the following list:

data["categories"].value_counts() I get

c          110  b           81  d           80  a           70  c,b         43            ...   d,a,b,c      2  a,b,c,d      2  a,b,d        1  a,c,d,b      1  d,c,a,b      1  Name: categories, Length: 62, dtype: int64  

I want to count the unique instance of each combination but due to different ordering, it counts a,b,c versus c,b,a as two separate elements where as I want to count count them as the same.

so I attempted to first list them first them sort them

list = data["categories"]  L = []  # getting length of list  length = len(list)    for i in range(len(data["categories"])):      L.append(sorted(list[i]))    for j in range(len(L)):      M.append(L[i].remove(','))  

The problem with this archaic method is that it leaves the ',' from the list and those then need to be manually removed.

and then manually remove the ','

[',', 'a', 'd'] --> ['a','d']

Is there a more intelligent way of doing this?

https://stackoverflow.com/questions/67030177/unique-combinations-of-permuted-list April 10, 2021 at 10:05AM

没有评论:

发表评论