[Solved] While loop inside of for loop [closed]

[ad_1] There’s nothing wrong with having a while loop inside a for loop. To demonstrate: i = 0 kebab = [“chicken”,”garlic”,”cheese”,”tomato”,”lettuce”,”chilli”] print “Kebabs are so good, this is what mine has:” excitement_over_kebab = 1 for ingredients in kebab: while excitement_over_kebab == 1: print kebab[i] i+=1 if i == 6: print “Enough talk, my lunch break … Read more

[Solved] Does 0 % 2 == 0?

[ad_1] Short explanation (and no other letter) 0 is less than or equal to 0 so A will be printed. Longer explanation (in case the short one was unclear to you) There are three conditions being checked: A. num <= 0 B. num >= 10 C. num % 2 == 0 (i.e. num is an … Read more

[Solved] List of lists in python [duplicate]

[ad_1] A simple way to do this is to use a dictionary. Something like this: list1 = [[‘John’,2],[‘Smith’,5],[‘Kapil’,3]] list2 = [[‘Smith’,2],[‘John’,2],[‘Stephen’,3]] dict = {} for item in list1: try: dict[item[0]] += item[1] except: dict[item[0]] = item[1] for item in list2: try: dict[item[0]] += item[1] except: dict[item[0]] = item[1] print dict This gives the output: {‘John’: … Read more

[Solved] Replace YYYY-MM-DD dates in a string with using regex [closed]

[ad_1] import re from datetime import datetime, timedelta dateString = “hello hello 2017-08-13, 2017-09-22” dates = re.findall(‘(\d+[-/]\d+[-/]\d+)’, dateString) for d in dates: originalDate = d newDate = datetime.strptime(d, “%Y-%m-%d”) newDate = newDate + timeDelta(days=5) newDate = datetime.strftime(newDate, “%Y-%m-%d”) dateString = dateString.replace(originalDate, newDate) Input: hello hello 2017-08-13, 2017-09-22 Output: hello hello 2017-08-18, 2017-09-27 6 [ad_2] solved … Read more

[Solved] Replace items in a dict of nested list and dicts

[ad_1] A recursive function that creates a new nested dictionary, replacing a key value with a different value def replace_key(elem, old_key, new_key): if isinstance(elem, dict): # nested dictionary new_dic = {} for k, v in elem.items(): if isinstance(v,dict) or isinstance(v, list): new_dic[replace_key(k, old_key, new_key)] = replace_key(v, old_key, new_key) else: new_dic[replace_key(k, old_key, new_key)] = v return … Read more

[Solved] What does x ^ 2 mean? in python

[ad_1] It doesn’t ‘mean’ anything, not to Python; it is just another character in a string literal: “Enter the value for the co-efficient of x^2. ” You could have written something else: “Enter the value for the co-efficient of x to the power 2. ” and nothing but the output shown when asking for input() … Read more

[Solved] Labeling horizontal bar using matplotlib bar_label [duplicate]

[ad_1] the issue is that the bar_label is a new feature and available in matplotlib version 3.4 or later – ref this link. Check the version of matplotlib using import matplotlib print(‘matplotlib: {}’.format(matplotlib.__version__)) Upgrade to v3.4 or latest version using pip install matplotlib –upgrade. You will need to include the code in the link you … Read more

[Solved] Access the key of a multilevel JSON file in python

[ad_1] You can use next with a generator comprehension. res = next(i[‘name’] for i in json_dict[‘ancestors’] if i[‘subcategory’][0][‘key’] == ‘province’) # ‘Lam Dong Province’ To construct the condition i[‘subcategory’][0][‘key’], you need only note: Lists are denoted by [] and the only element of a list may be retrieved via [0]. Dictionaries are denoted by {} … Read more