in Education by
I have the list of sets like so. I basically want to convert this to the dictionary and to address duplicate keys, I want to take a text value which is longer in length: [('hong kong', 'state'), ('hong kong', 'city'), ('hong', 'country'), ('kong', 'city'), ('hong kong', 'country')] I wanted the below-desired result: {'state': 'hong kong', 'city': 'hong kong', 'country': 'hong kong'} I have the function that does this but I am sure that there will be an efficient & pythonic way to do this. So I have tried this: def create_dict(l): d=defaultdict(list) for s in l: key = s[1] val = s[0] if d[key]: if len(val) > len(d[key]): d[key] = val else: d[key] = val return d Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
You can have a look at it, here is how you can use the sorted method with custom key: lst = [('hong kong', 'state'), ('hong kong', 'city'), ('hong', 'country'), ('kong', 'city'), ('hong kong', 'country')] def create_dict(l): sorted_lst = sorted(l, key=lambda x: len(x[0])) return {k: v for v, k in sorted_lst} print(create_dict(lst)) Output: {'country': 'hong kong', 'city': 'hong kong', 'state': 'hong kong'} If you are a beginner and want to know more about Python the do check out the python for data science

Related questions

0 votes
    I have an array of the shape (100000, 1) with each element in an array of type positive integer and not ... help would be appreciated. Select the correct answer from above options...
asked Jan 8, 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 have a 20 x 4000 dataframe in python using pandas. Two of these columns are named Year and quarter. I'd ... anyone help with that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I am trying to write my below pinging script results into the Text file, but I am getting an error message. ... object is not iterable Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have the dict as below, if same value is found more than once then my dict key must be created with incremental ... : ['COMPILE'], } Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have a set of oil wells compiled in the panda's data frame. It looks like this: wells = pd.DataFrame({'date ... -01-01 FIELDZ 10.8 Select the correct answer from above options...
asked Jan 19, 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 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'm looking for a decent implementation of the OPTICS algorithm in Python. I will use it to form density-based ... to that cluster. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I want to save files for each result from the loop. For example, I wrote the code but it only saves one file ... do I modify my code? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Here is my sample string: [true, {"name": "NameofItem", "amount": "1", "price": 100, "sellerName": " ... seem to make that work. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Hi, I am a relatively new programmer and my teacher gave us this problem to fix. Thing is, I have no idea ... wrong with this problem? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a big data frame. I want to replace float 1.0 and 0.0 with the true and false. My code: import pandas ... to do it in one line. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me why Python is better than R for Data Science? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
...