0

How can I convert a python list like:

[0, 1/4, 2/4, 3/4 , 4/4]

into the following:

[r'$25^{th}$', r'$50^{th}$', r'$75^{th}$', r'$100^{th}$']

Here each element of the 2nd list corresponds to a percentile value E.g. in the first list the element 0 corresponds to values below the 25th percentile. I want the solution to be easily extendible to different lengths e.g. current list has 5 elements, but we could have 2 elements or 10. Is there a pythonic way to do this?

3

2 Answers 2

1

Lists have a function called list.extend(seq) which can dynamically extend your list with a sequence. For example:

l = [1,2,3,4]
l.extend([5,6])
print(l) # [1,2,3,4,5,6]

Now, to answer your actual question:

perclist = ["%4.2f th" % (float(i)*100) for i in fraclist]
Sign up to request clarification or add additional context in comments.

1 Comment

@ofer I thought of keeping it simple without using list comprehension but I guess this is more pythonic. Thanks for typing it out.
0

Something like this I guess:

a = [0, 1/4, 2/4, 3/4 , 4/4]
b = [r'$%i^{th}$' % (i*100) for i in a[1:]] #skips first value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.