Python 3: Calculating percentage change with n number of values

I have a function which should calculate percentage increase / decrease between two values. The parameters in the function are original_value and new_value. Problem: I need the program to take n number of values, for example, six values, and then run the function on every pair of values. An example: The values are 1, 2, 3, 4, 5 and 6. I then need the program to calculate the avg. percentage change between 1 and 2, 2 and 3, 3 and 4, 4 and 5, and 5 and 6. I thought this would be best by making the aforementioned function, then

Split list until the next largest number with repeat

Python coding i want to split list shown below a=[5,4,2,5,7,5,4,10,2] if this list is given, i want to split it into b=[[5,4,2,5],[7,5,4],[10,2]] the algorithm is split until there is bigger number than 5 then 5,4,2,5 is in one list, next number is 7, so split the list until there is bigger then 7 which is 10. how can i do this? Answer Prints: [[5, 4, 2, 5], [7, 5, 4], [10, 2]]

Replace string in a list using python and save it with different variable

I am trying to replace a string with another string in a list, but failed and I don’t know why. For example, I have a list: i want to replace __label__1 with “OK” and __label__0 with “NOT OK” and save it in different variable using : but it failed to replace any of it Answer The predicts variable has two dimensions, try:

Compare string like ‘1,2,3’ and ‘3,1,2’ to be set as equals in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 days ago. Improve this question I have a strings like ‘1,2,3’ and ‘3,1,2’in a list, is there any way to compare these two strings and check that they are the same? I have a lis a = [‘G1G2G3,G4,G10’,’G4,G1G2G3,G10], i need a[0] == a[1] to return TRUE Answer Convert each string to a set, and compare the results.

Walrus operator for filtering regex searches in list comprehension

I have a Python list of strings. I want to do the regex search on each element, filtering only those elements where I managed to capture the regex group. I think I can do the regex search only once using the walrus operator from Python 3.8. So far I have: The logic is: I take the found group if the regex search returned anything, which means it is not None. The problem is, the bevahiour is weird – I can print() before this list comprehension, the program finishes with code 0, but there is no result and print() after the

Creating two separate “lists of lists” out of “list” & “list of lists”

I haven’t found a similar question with a solution here, so I’ll ask your help. There are 2 lists, one of which is list of lists: As a final result I would like to split it by 2 separate lists of lists: I tried to create loops but I have nothing to share. Any ideas are appreciated. Answer You can do it simply using list traversal and choosing element at index from first list and values from second list:

How do I update a variable that is pulling randomly from a list using python?

I am trying to make a small flash card program for my kids. Here is what I have come up with so far, I was hoping if I reassigned the variables in the button function it would randomly pull a new list item. But that does not seem to be the case. I know my code is not he prettiest, I’ve been teaching myself how to code for about 3 weeks now, so if there is anything else anyone can recommend to help improve this I would appreciate it. Answer There were two problems that were stopping your code from

pushing and poping into new list

I am trying to make a loop that if the program sees a letter or number it will push(x) into a new list and if it sees an asterisk * it must pop(). It is more of a stack algorithm where the first letter or number in is the last one out. FILO First in last out For example If the program is given the following array It should output or a letter input letter Output How can I implement this? Answer This should work: [‘2’, ‘4’, ‘3’, ‘1’] [‘a’, ‘t’, ‘o’, ‘e’, ‘d’, ‘f’]

Remove commas from all columns except one

Is there a way to remove commas from all columns except 1 or 2 (here, just date) in general code? (I have 20 columns in reality.) Expected output: Answer Use DataFrame.replace on columns of dataframe excluding the columns from exclude list: Result:

Why don’t Multi-dimensional (non-numpy) list slices work (anymore?)

Didn’t we used to… I remember being able to type in something like and getting back something like If I type the code above, I will get the following error (in Python 3.7): But if I call just one dimension, it works fine: What’s the Direct, Easy Slice Syntax? While I’m mildly interested if things changed or if I am just going crazy, I mostly want to know how I can do multi-dimensional slicing. What is the correct syntax instead of or ? (The second one does not raise an error, but it does not work. It returns [7, 8,