I have a list like the following one:
group = [
#Group 1 ('aaa bbbb' the most common words = two words)
['aaaa bbbb nnnn', #<-- row 1
'aaaa bbbb oooo', #<-- row 2
'aaaa bbbb pppp'], #<-- row 3
#Group 2 ('hello' the most common word = one word)
['hello Jack T.', #<-- row 1
'hello Ramona D.', #<-- row 2
'hello Robert G.'], #<-- row 3
#Group 3 ('yes! go go' the most common words = the whole string)
['yes! go go', #<-- row 1
'yes! go go', #<-- row 2
'yes! go go', #<-- row 3
'yes! go go'], #<-- row 4
#Group 4 (only one word = it's an invalid group)
['python'], #<-- row 1
#Group 5 (only one word = it's an invalid group)
['java'] #<-- row 1
]
I need to find the most common words for each group and save them to new list :
like this:
OUT : ['aaaa','hello','yes! go go']
but the third group has duplicate words -> 'go go' and I need both, so the real out is :
OUT : ['aaaa','hello','yes! go']
this is the working code
#Try to count words for each group
for groups in group:
#how many groups ?
nGroup = len(groups)
#join lists
words = " ".join(groups).split()
I get:
WORDS ['aaaa', 'bbbb', 'nnnn', 'aaaa', 'bbbb', 'oooo', 'aaaa', 'bbbb', 'pppp'] WORDS ['hello', 'Jack', 'T.', 'hello', 'Ramona', 'D.', 'hello', 'Robert', 'G.'] WORDS ['java'] WORDS ['python'] WORDS ['yes!', 'go', 'go', 'yes!', 'go', 'go', 'yes!', 'go', 'go', 'yes!', 'go', 'go']
#how many identical rows ?
rows = collections.Counter(words)
#what's the common words for each row ?
wCommon = rows.most_common()
#how often that's?
mCommon = rows.most_common(1)[0][1]
print (f"wCommon :{wCommon} rows :{rows} mCommon :{mCommon}")
I get:
#Group 1
wCommon :[('aaaa', 3), ('bbbb', 3),
('nnnn', 1), ('oooo', 1),
('pppp', 1)]
rows :Counter({'aaaa': 3, 'bbbb': 3,
'nnnn': 1, 'oooo': 1,
'pppp': 1})
mCommon :3
#Group 2
wCommon :[('hello', 3), ('Jack', 1), ('T.', 1),
('Ramona', 1), ('D.', 1),
('Robert', 1), ('G.', 1)]
rows:Counter({'hello': 3, 'Jack': 1, 'T.': 1,
'Ramona': 1, 'D.': 1,
'Robert': 1, 'G.': 1})
mCommon:3
#Group 3
wCommon :[('java', 1)] rows:Counter({'java': 1}) mCommon:1
#Group 4
wCommon :[('python', 1)] rows:Counter({'python': 1}) mCommon:1
#Group 5
wCommon :[('go', 8), ('yes!', 4)] rows:Counter({'go': 8, 'yes!': 4})
mCommon:8
the following one is the original list but it can change. I tried to split it into groups and count the common words for each row... for example:
aaaa, hello , yes! go go
but sometimes there are one or more common words like 'aaaa bbbb' How to get it ? or duplicates like 'go' and in that case it doesn't work
list_1 = [
"aaaa bbbb nnnn",
"aaaa bbbb oooo",
"aaaa bbbb pppp",
"hello Ramona D.",
"hello Jack T.",
"hello Robert G.",
"yes! go go",
"yes! go go",
"yes! go go",
"yes! go go",
"python",
"java"
]
Edit: Thank you all