Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
search in dict inside tuple
#1
In the following functions, i want to search, for example, the string 'BAB' in hmm. The code cannot find it and i could not fix it. Thanks for your help

def TrialSystem () :
hmm = {}
hmm [0] = ({ 'A ' :0.3 , 'B ' :0.2 , 'C ' :0.5 } , 1 )
hmm [1] = ({ 'A ' :0.1 , 'B ' :0.8 , 'C ' :0.1} , 2)
hmm [2] = ({ 'A ' :0.4 , 'B ' :0.6} , -1 )
return hmm

def ERecall ( hmm , strng ) :
prb = 1
N = len ( strng )
for i in range ( N ) :
if strng [ i ] in hmm [ i ][0]:
prb *= hmm [ i ][0][ strng [ i ] ]
else :
prb = 0
break
return prb
buran write Mar-28-2023, 11:38 AM:
Please, fix the identation and use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#2
You have spaces in the key values, so when you are searching for B, the key that you're looking for is in fact B .

I think that your TrialSystem(): needs to be:
def TrialSystem():
    hmm = {}
    hmm[0] = ({'A': 0.3, 'B': 0.2, 'C': 0.5}, 1)
    hmm[1] = ({'A': 0.1, 'B': 0.8, 'C': 0.1}, 2)
    hmm[2] = ({'A': 0.4, 'B': 0.6}, -1)
    return hmm
How hope this is of help.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Partial KEY search in dict klatlap 6 1,294 Mar-28-2023, 07:24 AM
Last Post: buran
  Calculate the sum of the differences inside tuple PUP280 4 1,203 Aug-12-2022, 07:20 PM
Last Post: deanhystad
  search a list or tuple for a specific type ot class Skaperen 8 1,952 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  Optimal way to search partial correspondence in a large dict genny92c 0 1,006 Apr-22-2022, 10:20 AM
Last Post: genny92c
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,401 Jan-30-2021, 07:11 AM
Last Post: alloydog
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,847 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Sort a dict in dict cherry_cherry 4 76,036 Apr-08-2020, 12:25 PM
Last Post: perfringo
  Being explicit about the items inside a tuple argument rudihammad 3 2,472 Dec-04-2019, 08:10 AM
Last Post: perfringo
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,221 May-21-2019, 11:39 AM
Last Post: avorane
  Iterate a tuple with dict inside anna 2 2,444 Feb-13-2019, 03:44 PM
Last Post: anna

Forum Jump:

User Panel Messages

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