[Solved] Appeding different list values to dictionary in python

[ad_1] Maybe use zip: for a,b,c in zip(scores,boxes,class_list): if a >= 0.98: data[k+1] = {“score”:a,”box”:b, “class”: c } ; k=k+1; print(“Final_result”,data) Output: Final_result {2: {‘score’: 0.98, ‘box’: [0.1, 0.2, 0.3, 0.4], ‘class’: 1}} Edit: for a,b,c in zip(scores,boxes,class_list): if a >= 0.98: data[k+1] = {“score”:a,”box”:b, “class”: int(c) } ; k=k+1; print(“Final_result”,data) 9 [ad_2] solved Appeding … Read more

[Solved] calculate days between several dates in python

[ad_1] One way to solve it would be the one below (Keep in my that this is a minimum example, describing the logic): from datetime import datetime line = “2023-01-01, 2023-01-02, 2024-01-01, 2019-01-01” # Split and convert strings to datetime objects dates = list(datetime.strptime(elem, ‘%Y-%m-%d’) for elem in line.split(‘, ‘)) total = 0 # Read … Read more

[Solved] splitting a list into two lists based on a unique value

[ad_1] Use a dict and group by the first column: from csv import reader from collections import defaultdict with open(“in.txt”) as f: d = defaultdict(list) for k, v in reader(f,delimiter=” “): d[k].append(v) print(d.values()) Which will give you all the values in two separate lists: [[’25’, ’26’], [’12’, ’56’] If the data is always in two … Read more

[Solved] Customize axes in Matplotlib

[ad_1] You can display subscripts by writing your column names using LaTex: import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame( { 0: { “Method 1”: 31.7, “Method 2”: 44.2, “Method 3”: 75.6, “Method 4”: 87.5, “Method 5”: 88.6, “Method 6”: 100.0, }, 1: { “Method 1”: 32.9, “Method 2”: 45.4, “Method 3”: … Read more

[Solved] Python: Find a Sentence between some website-tags using regex

[ad_1] If you must do it with regular expressions, try something like this: a = re.finditer(‘<a.+?question-hyperlink”>(.+?)</a>’, html) for m in a: print m.group(1) Just for the reference, this code does the same, but in a far more robust way: doc = BeautifulSoup(html) for a in doc.findAll(‘a’, ‘question-hyperlink’): print a.text [ad_2] solved Python: Find a Sentence … Read more

[Solved] Code a small Python dictionary

[ad_1] Read user input. Repeat for that many number of times – get items from dictionary using get attribute which handles KeyError itself: dic = {‘Hello’: ‘Salam’, ‘Goodbye’: ‘Khodafez’, ‘Say’: ‘Goftan’, ‘We’: ‘Ma’, ‘You’: ‘Shoma’} n = int(input()) for _ in range(n): print(dic.get(input(), ‘Wrong Input’)) EDIT: dic = {‘Hello’: ‘Salam’, ‘Goodbye’: ‘Khodafez’, ‘Say’: ‘Goftan’, ‘We’: … Read more

[Solved] Python error – list index out of range?

[ad_1] You hard coded the range of your loop and it is probably greater than the length of the list A quick fix is def printInfo(average): average.sort() # sorts the list of tuples average.reverse() # reverses the list of tuples print(‘\tDate\t\tAverage Price’) for i in range(len(average)): #Change was here print(“\t{:.2f}”.format(average[i][2], average[i][1], average[i][0])) however, a better … Read more

[Solved] Python Syntax error (“”) [closed]

[ad_1] As pointed out in this answer, print is a function in Python 3, which means you need to wrap the arguments to print within parentheses. So this should fix the error in your case: print(“employee / supervisor id :” ,k.get_id(),” Date joined :” ,k.get_date()) 2 [ad_2] solved Python Syntax error (“”) [closed]

[Solved] Python arrays in numpy

[ad_1] Perhaps the closest thing in Python to this Javascript array behavior is a dictionary. It’s a hashed mapping. defaultdict is a dictionary that implements a default value. In [1]: from collections import defaultdict In [2]: arr = defaultdict(bool) Insert a couple of True elements: In [3]: arr[10] = True In [4]: arr Out[4]: defaultdict(bool, … Read more

[Solved] How to use python-request to post a file on a Rails App

[ad_1] I figured it out: def add_photo(entry_id, image_path): return requests.post( url = URL, headers = HEADER, files = { ‘entry[entry_id]’: (None, entry_id, ‘text’), ‘entry[photo]’: (os.path.basename(image_path), open(image_path, ‘rb’), ‘image/jpg’, {‘Expires’: ‘0’}) } ) [ad_2] solved How to use python-request to post a file on a Rails App

[Solved] Pandas: get json from data frame

[ad_1] You can use: #convert int xolum to string df[‘member_id’] = df.member_id.astype(str) #reshaping and convert to months period df.set_index(‘member_id’, inplace=True) df = df.unstack().reset_index(name=”val”).rename(columns={‘level_0′:’date’}) df[‘date’] = pd.to_datetime(df.date).dt.to_period(‘m’).dt.strftime(‘%Y-%m’) #groupby by date and member_id and aggregate sum df = df.groupby([‘date’,’member_id’])[‘val’].sum() #convert all values !=0 to 1 df = (df != 0).astype(int).reset_index() #working in pandas 0.18.1 d = df.groupby(‘member_id’)[‘date’, … Read more