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

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 is … Read more

[Solved] Does 0 % 2 == 0?

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 even … Read more

[Solved] List of lists in python [duplicate]

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’: 4, … Read more

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

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 solved Replace YYYY-MM-DD … Read more

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

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 new_dic … Read more

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

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() would … Read more

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

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 provided … Read more

[Solved] How to fix this: ValueError: invalid literal for int() with base 10: [closed]

the default behaviour of split() is to split by whitespace. You code would not throw errors if the input only contained numbers and whitespaces, e.g. 156 178 165 171 187. If you want a different split seperator, e.g. the comma ,, you could use split(“,”) instead. Valid input would be e.g. 156,178,165,171,187. Note that [156,178,165,171,187] … Read more

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

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 {} and … Read more