in Education by
I have the dict as below, if same value is found more than once then my dict key must be created with incremental numbering. TT = { "TYPE_1" : ['ERROR'], "TYPE_2": ['FATAL'], "TYPE_3" : ["TIME_OUT"], "TYPE_4" : ['SYNTAX'], "TYPE_5" : ['COMPILE'], } m1 = "ERROR the input is not proper" m2 = "This should have not occured FATAL" m3 = "Sorry TIME_OUT" m4 = "SYNTAX not proper" m5 = "u r late its TIME_OUT" The value "TIME_OUT" occur twice in the search. count = 0 for key in TT.keys(): print(key) Key_1 = key while key_1 in TT: count = count+1 key_1 = key + "_{}".format(count) The above code gives error Key_1 not defined. Expected OUTPUT: if same value is occuring more than once then the dict key should be created with incremental numbering "TYPE_3_1" : ["TIME_OUT"], TT = { "TYPE_1" : ['ERROR'], "TYPE_2": ['FATAL'], "TYPE_3" : ["TIME_OUT"], "TYPE_3_1" : ["TIME_OUT"], "TYPE_4" : ['SYNTAX'], "TYPE_5" : ['COMPILE'], } Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
There can be an efficient way of solving this if you can rethink some of a data structure but if that is not an option you may be able to try this. inputs = ["ERROR the input is not proper", "This should have not occurred FATAL", "Sorry TIME_OUT", "SYNTAX not proper", "u r late its TIME_OUT"] basic_types = { "TYPE_1" : ['ERROR'], "TYPE_2": ['FATAL'], "TYPE_3" : ["TIME_OUT"], "TYPE_4" : ['SYNTAX'], "TYPE_5" : ['COMPILE'], } type_counts = {} results = {} for sentence in inputs: for basic_type in basic_types: if basic_types.get(basic_type)[0] in sentence: type_counts[basic_type] = type_counts.get(basic_type, 0) + 1 if type_counts[basic_type] == 1: results[basic_type] = [basic_types.get(basic_type)[0]] else: results[basic_type+"_{}".format(type_counts[basic_type] - 1)] = [basic_types.get(basic_type)[0]] print(results) Improve your knowledge in data science from scratch using Data science online courses

Related questions

0 votes
    I have the list of sets like so. I basically want to convert this to the dictionary and to address duplicate keys, ... ] = val return d Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have a Python dictionary like the following: {u'2012-06-08': 388, u'2012-06-09': 388, u'2012-06-10 ... (my_dict,index=my_dict.keys()) Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I have the dataset that has the no_employees column that is the str object. whats is a best way to create the new ... 1-5 |Very Small Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with 2 columns and I want to add the new column; This new column should be updated based on ... +=1 is not working. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have this dataframe and trying to select the last n (=2) rows if the present value is True so I code as ... , I should select 50,40 Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    I'm learning the difference between the various machine learning algorithms. I understand that the implementations of ... for that? Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    In the python documentation, an instance of a named_tuple is created by using the below code: Point = named_tuple(' ... of doing it? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    I have data like this: id a b c d 1 y y z z 2 y z y y 3 y y y y I want to count the value of "y" ... 1 2 2 1 3 4 Can anyone help me? Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have an assignment to make an AI Agent that will learn play a video game using ML. I want to create a new ... the help of OpenAI Gym? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Write pseudo code to count occurrence of a given number in the list. For example, consider the following list, if ... in the list. Select the correct answer from above options...
asked Nov 28, 2021 in Education by JackTerrance
0 votes
    I have the below data frame lst = [['A',1],['B',0],['C',1],['D',0],['E',1],['F',1],['G',1]] ... give all val with 1. df[df.val == 1] Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the df df = pd.DataFrame({'col1': [1,2,3,4,5,6]}) and I would like to use df.isin() and ... help me find the solution to it? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    I have the dataframe as below: df ID val 1 0.0 2 yes 3 1.0 4 0.0 5 yes How do I match my ... use np.where along with the bfill? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    I am receiving the error: ValueError: Wrong number of items passed 3, placement implies 1, and I am struggling to ... 'sigma'] = sigma Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I am training on 970 samples and validating on 243 samples. How big should batch size and number of epochs be ... on data input size? Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
...