reverse

Reverse digits of a list of numbers in python

Reverse digits of a list of numbers in python Question: for i in range(len(primo)): reversed_num = 0 while i > 0: digit = i % 10 reversed_num = reversed_num * 10 + digit i //= 10 reversed.append (reversed_num) for r in range(len(reversed)): print (r) I’m writing a program to reverse a series of numbers from …

Total answers: 5

Why does Print() in combination with reverse in Python produce None?

Why does Print() in combination with reverse in Python produce None? Question: I have just come across an interesting case, I think. I was trying to reverse my list and then print output using the print() function. Here are the two ways I have tried: 1. Printing directly: x = [2.0, 9.1,12.5] print(x.reverse()) output: None …

Total answers: 4

Check if a number can be formed by sum of a number and its reverse

Check if a number can be formed by sum of a number and its reverse Question: I want to check if a given number can be formed by another number say b and reverse(b). For example 12 == 6+6, 22 == 11 + 11 and 121 == 29+92. One thing I have figured out is …

Total answers: 7

Get some elements from numpy array in reverse order

Get some elements from numpy array in reverse order Question: I need to generate numpy arrays getting elements in reverse order from another array. Toy example code Lets say I use following toy example code: import numpy as np a = np.array([1,2,3,5,8,13]) n = len(a) for i in range(n): print a[n:n-i-2:-1] I would expect that …

Total answers: 3

Reversing a list with single element gives None

Reversing a list with single element gives None Question: I noticed odd behaviour when returning a list (with a single element) from a function and then trying to reverse it, using reverse() I’ve distilled it here: def myFunction(): return [“The Smiths”] nums = [5,4,3,2,1] nums.reverse() print nums # 1,2,3,4,5 – fine! # lets use one …

Total answers: 1

Reverse index in a list

Reverse index in a list Question: For the list k1=[31.0, 72, 105.0, 581.5, 0, 0, 0], I would like to add a constant for example 100 to the first non-zero element in the reverse list. this is what I want: newk1=[0, 0, 0, 681.5, 105, 72, 31] As a beginner in Python I could not …

Total answers: 6

Why does sorting a list return None?

Why does sorting a list return None? Question: My code is written in python 3 and it is meant to print out palindromes. It should iterate through all the palindromic products of 2 3-digit numbers as shown below: mylist=[] for i in range(999,99,-1): for x in range(999, i-1, -1,): number=i*x number=str(number) if number==number[::-1]: #print(number) mylist.append(number) …

Total answers: 1

Reverse certain elements in a 2d array to produce a matrix in the specified format, Python 3

Reverse certain elements in a 2d array to produce a matrix in the specified format, Python 3 Question: I have the following code for a list of lists with the intention of creating a matrix of numbers: grid=[[1,2,3,4,5,6,7],[8,9,10,11,12],[13,14,15,16,17],[18,19,20,21,22]] On using the following code which i figured out would reverse the list, it produces a matrix …

Total answers: 3

Reversed cumulative sum of a column in pandas.DataFrame

Reversed cumulative sum of a column in pandas.DataFrame Question: I’ve got a pandas DataFrame with a boolean column sorted by another column and need to calculate reverse cumulative sum of the boolean column, that is, amount of true values from current row to bottom. Example In [13]: df = pd.DataFrame({‘A’: [True] * 3 + [False] …

Total answers: 4

Reverse the order of a legend

Reverse the order of a legend Question: I use the following code to plot the bar graph and need to present a legend in reverse order. How can I do it? colorsArr = plt.cm.BuPu(np.linspace(0, 0.5, len(C2))) p = numpy.empty(len(C2), dtype=object) plt.figure(figsize=(11, 11)) prevBar = 0 for index in range(len(C2)): plt.bar(ind, C2[index], width, bottom=prevBar, color=colorsArr[index], label=C0[index]) …

Total answers: 5