Posts: 2
Threads: 1
Joined: Feb 2021
Feb-27-2021, 11:37 AM
(This post was last modified: Feb-27-2021, 02:50 PM by Larz60+.)
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
Larz60+ write Feb-27-2021, 02:50 PM:Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags in future posts.
Posts: 371
Threads: 2
Joined: Jan 2021
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)
Posts: 6,384
Threads: 17
Joined: Feb 2020
Feb-27-2021, 06:09 PM
(This post was last modified: Feb-27-2021, 06:13 PM by deanhystad.)
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}]
Posts: 2
Threads: 1
Joined: Feb 2021
Feb-27-2021, 07:10 PM
(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
|