[Solved] Python – selectively add up values while iterating through dictionary of dictionaries [closed]

my_dict = {key1:{value1:value2}, key2:{value3:value4}, key3{value5:value6}, key4:{value7:value8}…} result = [] for key1 in my_dict: if sum(my_dict[key1].keys() + my_dict[key1].values())==3: result[key1] = my_dict[key1].keys()[0] Hope this helps solved Python – selectively add up values while iterating through dictionary of dictionaries [closed]

[Solved] how to export selected dictionary data into a file in python?

First, you start your question with expenses but ends with incomes and the code also has incomes in the parameters so I’ll go with the income. Second, the error says the answer. “expense_types(income_types)” is a list and “expenses(incomes)” is a dictionary. You’re trying to find a list (not hashable) in a dictionary. So to make … Read more

[Solved] How to convert txt to dictionary in python [closed]

I’ve added comments to the answer to explain the flow, please add more details in your question if this is not clear enough 🙂 # Open file for reading file_house_price = open(“house_price.txt”, “r”) # read all lines from the file into a variable data_house_price = file_house_price.readlines() # as we’re done with the file, we can … Read more

[Solved] Continuously adding to a dictionary [closed]

Right now, the else clause fires if there is a at least one key that does not match vehicle. Fix it like this: def add_to_dict(product_type, brand, product_dict): if product_type in product_dict: product_dict[product_type].append(brand) else: product_dict[product_type] = [brand] product_dict = {} add_to_dict(“car”, “audi”, product_dict) add_to_dict(“car”, “mercedes”, product_dict) add_to_dict(“phone”, “apple”, product_dict) print(product_dict) # Output: # {“car”: [“audi”,”mercedes”], “phone”: … Read more

[Solved] How to get number of occurences of items after certain item in list

Try this one-liner: reduce(lambda acc, cur: acc + ([] if cur[1] == ‘you’ else [next((i[0] – cur[0] – 1 for i in list(enumerate(my_list))[cur[0]+1:] if i[1] == ‘hello’), len(my_list) – cur[0] – 1)]), enumerate(my_list), []) It will give an array where the nth element is the number of ‘you’s following the nth occurrence of ‘hello’. e.g. … Read more

[Solved] how can I square the numbers between 0 and any input with extremely large numbers in python [closed]

If I am interpreting your question correctly, you simply need to devise a very simple list comprehension program. Getting the square root of everything between zero and n is not necessary. Did you mean square? Here’s my solution: def nbDig(n,d): int_list = str([str(x*x) for x in range(0,n+1)]).count(str(d)) return int_list I can test it like so: … Read more

[Solved] C++ to Python Code (Arduino to Raspberry Pi) – Using Ultrasonic [closed]

You don’t need this void setup() Linux is take a care of this. Here is Python code: import RPi.GPIO as GPIO import time #GPIO Mode (BOARD / BCM) GPIO.setmode(GPIO.BCM) #set GPIO Pins GPIO_TRIGGER = 18 GPIO_ECHO = 24 #set GPIO direction (IN / OUT) GPIO.setup(GPIO_TRIGGER, GPIO.OUT) GPIO.setup(GPIO_ECHO, GPIO.IN) def distance(): # set Trigger to HIGH … Read more

[Solved] “The cheater’s coin” python riddle [closed]

If I read your problem correctly, you need to provide a method to compensate for the broken coin producing a relatively fair amount of results. With that instead of calling the broken_coin() function every time directly, one could call a function that calls the function and every other time returns the reverse result to the … Read more

[Solved] Finding the modules where changes were checked with svn

Three separate tasks: call svn properly to create the log parse the log Write the parsed values somewhere 1. import subprocess as sp svn_url = “svn://repo-path.com/project” revisions = [12345, 12346] revision_clargs = [“-r%i” % revision for revision in revisions] popen = sp.Popen([“svn”, “log”, “-v”] + revision_clargs + [svn_url],stdout=sp.PIPE,stderr=sp.PIPE) out,err = popen.communicate() 2. input_ = “”” … Read more

[Solved] how to find the black region in near the edge

color_img = imread(‘0k4Kh.jpg’); img = rgb2gray(color_img); [x, y] = size(img); for i = 1:x if length(find(img(i, :))) ~= 0 lastmarginalrow = i-1; break; end end for ii = y:-1:1 if length(find(img(:, ii))) ~= 0 lastmarginalcol = ii-1; break; end end figure; fig = imshow(color_img); h = impoly(gca, [0,x; lastmarginalcol,x; lastmarginalcol,lastmarginalrow; 0,lastmarginalrow]); api = iptgetapi(h); api.setColor(‘red’); … Read more

[Solved] Creating list with dates

Keeping as much as possible your code structure, this should be the solution you are looking for. It’s meant to be easy to read and understand. However it is not the best solution, for that we need to know what selection looks like, or even better what your input file looks like. def main(): with … Read more

[Solved] Python:how to get keys with same values? [closed]

If you want this to work for any arbitrary key(s) you can use a defaultdict of OrderedDicts.. from collections import defaultdict, OrderedDict result_dict = defaultdict(OrderedDict) data = [(‘Han Decane’,’12333′),(‘Can Decane’,’12333′),(‘AlRight’,’10110′)] for (v,k) in data: result_dict[k][v]=True >>> list(result_dict[‘12333’].keys()) [‘Han Decane’, ‘Can Decane’] And if you want all the results that had multiple values >>> [k for … Read more