Counting the number of triplets forming GP series in python

The problem statement is: You are given an array and you need to find number of triplets of indices(i,j,k) such that the elements at those indices are in geometric progression for a given common ratio, r and i<j<k. For example, arr=[1,4,16,64]. If r=4, we have [1,4,16] and [4,16,64] at indices (0,1,2) and (1,2,3). I have a solution but I am unable to understand it. Can anyone help me out? Answer The way this problem works is that it uses “dict” to keep a running count of each number in the array. Then, it uses “dictPairs” to keep a running count

Group dates into list based on value

I have a json object that I’m trying to group item together in. this code returns values grouped by date as the key and then a list of teams and dates like this However, I need it to return and key value pair like this where all the dates for a specific team are in a list as the value for the key Date. Answer This is one approach using dict.setdefault Ex:

Fastest method: for value in DictA, find value in DictB and retrieve other DictB value?

For each item in dictA, I want to search for it in dictB, if dictB has it then I want to pull some other values from dictB and add it to dictA. An example that is working is here, however it is rather slow as I have 50,000+ items to search through and it will perform this similar function on multiple dicts. Is there a fast method of performing this search? The dictA output after this would be: Answer The given is list of dict. You can create dict from that assuming id is uninque. Converting from list of dict

Sort lists of dictionaries within dictionary

EDIT: I’m editing the whole post as it wasn’t as clear as I thougt. I have this dictionary (list of dictionaries within a dictionary) and I need to sort it by the values of “Position” and “Team”, meaning that I need to get all the values from those two keys and print them as I show below. I know how to get each value, but I don’t know how to get them and sort them. I tried this piece of code, but I still can’t get it to work. My actual dictionary is much bigger than this one, and I

how to access a dictionary from another function in the current function

I am trying to refer to the dictionary in function_one. I have tried to return the dictionary variables, and use the dictionary names as parameters and arguments. However, I am getting an error message saying that the dictionaries I am trying to access in function_two is not defined. Here is my simplified code: Basically, I am asking if the element chosen for input1 is in the dictionary in the previous function I want the program to change the total_cost value etc. Answer When you are calling the function_one(), you are not using the dictionaries returned by it. You can use

Map pandas dataframe columns to an array

I have a dataframe like this: And an array like: The first element will be for family_id=0 and column “choice_0” = 52 The second element will be for family_id=1 and column “choice_2” = 82 The third element will be for family_id=2 and column “choice_4” = 27 And I will like to get: The logic will be: For family_id =0 The initial array has a a 52. And I will like to receive a 0 because it belongs to the “choice_0” column. For family_id = 1 The initial array has a 82. And I will like to receive a 2 because

Sort based Dictionary of Dictionary of Dictionary Values?

I have a dictionary like this I am trying to sort dictionary based on KEY OF LAST VALUES(LIST). FOR EXAMPLE I am looking for get dictionary value like ‘C’ IN first and ‘D’ in second I’m trying to get correct order. Here is code: Unfortunately didn’t get correct result This is my expected output I am using python 3.5 . I am trying to get order like this Answer The following code will sort any dictionary by its key and recursively sort any dictionary value that is also a dictionary by its key and makes no assumption as to the

Calculate how many times the values are repeated inside a dictionary key

I have a dictionary that is created from a select in the database, what I needed was to generate a metric from that dictionary Dictionary Exit of the Dict: Example of metric: code_client: BRA appears 1x within dictionary Summing up: I need to calculate how many times the values are repeated within the KEY = *code_client* I tried to do it as follows: Answer From what I can tell, you need to get a count of each code client in the dictionary. This code will populate the dictionary ct with each code_client as a key, and the number of occurrences

Specify ordering for columns when creating a DataFrame from list of dictionaries

I have a method that creates a list, results, and then appends to it a dict row which has keys (column names in my eventual dataframe) and values (row values in my eventual dataframe). Then I append the row and convert the results collection to a df at the end. For example: The issue I have is that the column names are alphbetized so the df looks like this: Is there a way to specify the column names to be in the order I want (“Name”, “Col”)? In reality I have many more columns so I’d prefer not to have

Create pandas series using a dictionary as mapper

Is there a built-in function to create a pandas.Series column using a dictionary as mapper and index levels in the data frame ? The idea is to create a new column based on values in index levels and a dictionary. For instance: Let’s suppose the following data frame, where id, name and code and different levels in indexes and the following dictionary d = {‘a’: {‘y’, ‘z’}, ‘b’: {‘x’}} The output of the new column should look like: As a result of mapping in which new = 1 if code index value was in the dictionary list of values with