Python Forum
best way to implement algorithm
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
best way to implement algorithm
#1
Question 
Hey,
Can you please help me implement this program using python.Huh

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.
Reply
#2
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)
hamidze likes this post
Reply
#3
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}]
hamidze likes this post
Reply
#4
Thumbs Up 
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Right way to implement interfaces yossiy123 1 1,282 May-12-2022, 10:31 AM
Last Post: Gribouillis
  Implement distancematrix to Held karp algorithm Robinjacobsson 1 1,861 Nov-01-2019, 09:24 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020