Python loop through list, enumerate, and start and end at specific index, but do not modify the index

Advertisements I have a text file as a list, each line is an item in the list. This list contains multiple start tag and end tag (but otherwise not structured) and I must iterate through the file processing data between the start and end tags. Due to potential errors in the file, I must ignore… Read More Python loop through list, enumerate, and start and end at specific index, but do not modify the index

Python – Why does enumerate() cause a later zip() to only pull from the last item in list?

Advertisements Code: boylist = [‘Jim’, ‘James’, ‘Jack’, ‘John’, ‘Jason’] for i, boylist in enumerate(boylist): print(f’Index {i} is {boylist} in my list’) #boylist = [‘Jim’, ‘James’, ‘Jack’, ‘John’, ‘Jason’] girllist = [‘Emma’, ‘Clara’, ‘Susan’, ‘Jill’, ‘Lisa’] for boylist, girllist in zip(boylist, girllist): print(f'{boylist} and {girllist} form a nice couple’)\ Output: Index 0 is Jim in my… Read More Python – Why does enumerate() cause a later zip() to only pull from the last item in list?

Creating a ranking in Python from a given value by id

Advertisements I have this dataset: dic = {‘id’:[1,1,1,1,1,2,2,2,2], ‘sales’: [100.00, 200.00, 300.00, 400.00, 500.00, 100.00, 200.00, 300.00, 400.00], ‘year_month’: [202201, 202202, 0, 202204, 202205, 202201, 202202, 202203, 0]} df = pd.DataFrame(dic) Output: id sales year_month 0 1 100.0 202201 1 1 200.0 202202 2 1 300.0 0 3 1 400.0 202204 4 1 500.0 202205… Read More Creating a ranking in Python from a given value by id

Why am I getting IndexError: list index out of range

Advertisements from openpyxl import load_workbook book = load_workbook(‘Line_Machines_Data.xlsx’) sheet = book[‘Line 7 machines’] line_7data = [15, 20, 30, 40] # this is the new data for row in sheet[‘A1’ : ‘F1’]: #iterates through each row for count, cell in enumerate(row): cell.value = line_7data[count] book.save(‘Line_Machines_Data.xlsx’) I need the cell value to be updated using an index.… Read More Why am I getting IndexError: list index out of range

Python – naming parameters from enumerate

Advertisements Question: Take the following: def a(the_enumerate: enumerate): print("index: " + str(the_enumerate[0]), character: " + the_enumerate[1]) map(a, enumerate("test")) Is there a way of naming the index and the current element (character) in a better way than: index: int = the_enumerate[0] character: int = the_enumerate[1] Perhaps it would be something like this: def a(the_enumerate: enumerate(index: int,… Read More Python – naming parameters from enumerate

Need to check all my variables for an ampersand, I put variables in a list and check using a for loop, but Python only changes value inside list

Advertisements I’ve got the following code, but unfortunately it only changes the value inside the list. Is there any way I can change the value outside the list, so it can be used later in the script? street_number = "100 & 102" street_name = "Fake Street" suburb = "Faketown" allvariables = [street_number, street_name, suburb] ampersand… Read More Need to check all my variables for an ampersand, I put variables in a list and check using a for loop, but Python only changes value inside list