Hey,
Can you please help me implement this program using python.
I have a list:
The input
list=[["word1", "word2"],["word1", "word2","word1"], ["word4", "word5","word4", "word5", "word2", "word3"]]
The output:
Output:
out=[
{"word1", "word2",2}, {"word2", "word1",1},{"word4", "word5",2},{"word5", "word4",1},
{"word5", "word2",1},{"word2", "word3",1}
]
how it works:
it calculates the successive words in a sub-list, and it updates if it finds a new occurrence in a sub-list
I had a tough time with this because a list of sets reverses the order of combinations like
{word2, word1}
and makes them
{word1, word2}
. The best I can do is a list of lists.
word_list = [
["word1", "word2"],
["word1", "word2","word1"],
["word4", "word5","word4", "word5", "word2", "word3"]]
out = []
count = []
for array in word_list :
for marker in range (len (array)) :
try :
tester = [array [marker], array [marker + 1]]
if tester in out :
count [out.index (tester)] += 1
else :
count.append (1)
out.append (tester)
except IndexError :
pass
marker = 0
for array in out :
array.append (count [marker])
marker += 1
print (out)
Cheating with formatting. Sets are awkward to use in something like this. How do you get the count out of the set? A dictionary is a much better fit, and a counting dictionary does all the work. Just pretty up the output if required.
from collections import Counter
groups=[
["word1", "word2"],
["word1", "word2","word1"],
["word4", "word5","word4", "word5", "word2", "word3"]]
out = Counter([(a,b) for group in groups for a, b in zip(group, group[1:])])
pairs = ', '.join(f'{{"{key[0]}", "{key[1]}", {value}}}' for key, value in out.items())
print(f'out = [{pairs}]')
Output:
out = [{"word1", "word2", 2}, {"word2", "word1", 1}, {"word4", "word5", 2}, {"word5", "word4", 1}, {"word5", "word2", 1}, {"word2", "word3", 1}]
(Feb-27-2021, 06:09 PM)deanhystad Wrote: [ -> ]Cheating with formatting. Sets are awkward to use in something like this. How do you get the count out of the set? A dictionary is a much better fit, and a counting dictionary does all the work. Just pretty up the output if required.
from collections import Counter
groups=[
["word1", "word2"],
["word1", "word2","word1"],
["word4", "word5","word4", "word5", "word2", "word3"]]
out = Counter([(a,b) for group in groups for a, b in zip(group, group[1:])])
pairs = ', '.join(f'{{"{key[0]}", "{key[1]}", {value}}}' for key, value in out.items())
print(f'out = [{pairs}]')
Output:
out = [{"word1", "word2", 2}, {"word2", "word1", 1}, {"word4", "word5", 2}, {"word5", "word4", 1}, {"word5", "word2", 1}, {"word2", "word3", 1}]
Exactly what I want thanks