[Solved] How can we test if a list contains n consecutive numbers with a difference of 1? [closed]

Try reducing your list. from functools import reduce def get_all_consecutives(iterable, consecutive_limit): consecutives = [] def consecutive_reducer(accumulator, next_item): if not accumulator: accumulator.append(next_item) else: if next_item – accumulator[-1] == 1: accumulator.append(next_item) else: accumulator = [next_item] if len(accumulator) == consecutive_limit: consecutives.append(accumulator) return accumulator reduce(consecutive_reducer, iterable, []) if not consecutives: return False elif len(consecutives) == 1: return consecutives[0] else: … Read more

[Solved] Python replace line by index number

If you wish to count iterations, you should use enumerate() with open(‘fin.txt’) as fin, open(‘fout.txt’, ‘w’) as fout: for i, item in enumerate(fin, 1): if i == 7: item = “string\n” fout.write(item) 2 solved Python replace line by index number

[Solved] Using yield in nested loop

As you’ve been told in the comments, I also don’t think you can save memory using yield in this case. However, if you only want to know how to use yield, this is one of the options: import pandas as pd data = [{ “id”: 123, “sports”: { “football”: { “amount”: 3, “count”: 54 }, … Read more

[Solved] Disable button in Tkinter (Python)

You need to unbind the event. state=”disabled”/state=DISABLED makes button disabled but it doesn’t unbind the event. You need to unbind the corresponding events to achieve this goal. If you want to enable the button again then you need to bind the event again. Like: from Tkinter import * def printSomething(event): print(“Print”) #Start GUI gui = … Read more

[Solved] recursion code should go infinitely?

This code will terminate as the function is calld with k-1, meaning that the condition k>1 will eventually evaluate to False. Imagine recursion is like a continually growing tree and a squirrel is the interpreter. When the isort() function is called, the tree branches off and the squirrel runs to the end of that branch. … Read more

[Solved] Embedded function returns None

monthly_payment_function does not return anything. Replace monthly_payment= with return (that’s ‘return’ followed by a space). Also you have an unconditional return before def monthly_payment_function, meaning it never gets called (strictly speaking, it never even gets defined). Also you are pretty randomly mixing units, and your variable names could use some help: from __future__ import division … Read more

[Solved] How to calculate precision and recall for two nested arrays [closed]

You have to flatten your lists as shown here, and then use classification_report from scikit-learn: correct = [[‘*’,’*’],[‘*’,’PER’,’*’,’GPE’,’ORG’],[‘GPE’,’*’,’*’,’*’,’ORG’]] predicted = [[‘PER’,’*’],[‘*’,’ORG’,’*’,’GPE’,’ORG’],[‘PER’,’*’,’*’,’*’,’MISC’]] target_names = [‘PER’,’ORG’,’MISC’,’LOC’,’GPE’] # leave out ‘*’ correct_flat = [item for sublist in correct for item in sublist] predicted_flat = [item for sublist in predicted for item in sublist] from sklearn.metrics import classification_report print(classification_report(correct_flat, … Read more

[Solved] how to create a list of elements from an XML file in python

1) Try this: import xml.etree.ElementTree as ET Books = ET.parse(‘4.xml’) #parse the xml file into an elementtre root = Books.getroot() for child in root: BookInfo = [ child.find(‘title’).text, child.find(‘author’).text, child.find(‘year’).text, child.find(‘price’).text ] print (BookInfo) 2)if you can receive the specific element from the list use BookInfo[0] – this is title, BookInfo[1] – author… solved how … Read more

[Solved] How to separate the contents of parentheses and make a new dataframe column? [closed]

Seems like str.extract would work assuming the seat number is the numeric characters before 席 and the seat arrangement is the values inside the parenthesis: import numpy as np import pandas as pd df = pd.DataFrame({ ‘seat’: [’45席(1階カウンター4席、6〜8人テーブル1席2階地下それぞれ最大20人)’, np.nan, np.nan, np.nan, ‘9席(カウンター9席、個室4席)’] }) new_df = df[‘seat’].str.extract(r'(\d+)席((.*))’, expand=True) new_df.columns = [‘seat number’, ‘seat arrangement’] new_df: seat … Read more