0

I'm trying to write a sine function that outputs answers going only to two decimal places. This is my code so far:

x=np.arange(0,190,10)
x_values=np.deg2rad(x)
y=np.sin(x_values)
y_values={":.2f"}.format(y)
print(y_values)

All the code through y works for me, when I do

print(y)

it outputs the correct values, but to like 10 decimal places, and I'd like to limit it to 2. I would like to do it using the .format notation, which I suck at. I'm getting an error that says "'set' object has no attribute 'format'," so there's some kind of fundamental misunderstanding I have about exactly how .format works. Is it only meant for strings? I would greatly appreciate any help I can get.

I did have a successful result using this:

y_values=[ '%.2f' % elem for elem in y ]

However, I'd like to see if it's possible to do it with .format. Thank you!

energy314
  • 27
  • 3
  • "I'm getting an error that says "'set' object has no attribute 'format'," so there's some kind of fundamental misunderstanding I have about exactly how .format works. Is it only meant for strings?" Yes; this is a simple typo - you **don't want** a set; you get one because you put the `{}` - which are supposed to be part of the string - outside the string. Before posting, [please try](https://meta.stackoverflow.com/questions/261592) to look for existing solutions and to [carefully study the code](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) to look for simple problems. – Karl Knechtel Sep 22 '22 at 19:05
  • Put another way: it is the same problem as if you had written `%'.2f' % elem` and wondered why it causes a syntax error. – Karl Knechtel Sep 22 '22 at 19:07

2 Answers2

1

This should work for you:

y_values=[ '{:.2f}'.format(elem) for elem in y ]

I prefer f strings:

y_values=[ f'{elem:.2f}' for elem in y ]

The problem with your code was that the curly braces were outside the quotes and that y is a list. Moving the braces and using a list comprehension (as you showed in your last bit of code) are the two changes that fixed the issues you had.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • Please read [answer] and do not answer questions that are common duplicates and/or boil down to simple typos. The only thing that was wrong in OP's code was the placement of the `{}`. I **really** should not have to explain something like this to someone with your level of experience. – Karl Knechtel Sep 22 '22 at 19:08
  • @KarlKnechtel: That wasn't the only problem. The variable `y` contains a list. The solution was to correct the typo and combine the techniques the OP tried. I shouldn't be chastised for completely solving a poster's actual problem. – Dennis Williamson Sep 22 '22 at 19:48
  • OP *already demonstrated* the technique for handling the list. But even if that were missing information, it is another common duplicate. Stack Overflow is **not there** to "completely solve posters' actual problems"; it is **not a discussion forum**. – Karl Knechtel Sep 23 '22 at 02:52
  • 1
    Thank you so much for your help! I am quite new to Python, I only started two weeks ago, so these kinds of things are sometimes hard for me to catch. – energy314 Sep 24 '22 at 14:02
-1

You can create a universal vectorized function for this:

v_format = np.vectorize(lambda x, y: f'{x:.{y}f}')

# v_format(1, 2) returns
# array('1.00', dtype='<U4')

# v_format([15.2445, 154.5647, 35.6785], 2) returns
# array(['15.24', '154.56', '35.68'], dtype='<U6')

# v_format([15.2445, 154.5647, 35.6785], 4) returns
# array(['15.2445', '154.5647', '35.6785'], dtype='<U8')

# v_format([1.23, 3.456, 7.8910], [1, 2, 3]) returns
# array(['1.2', '3.46', '7.891'], dtype='<U5')

Work for every array with any numbers in it (even with complex ones).

* (If you need you can convert an array into a list or a tuple)

Nazar0360
  • 1
  • 2
  • Please read [answer] and do not answer questions that are common duplicates and/or boil down to simple typos. The only thing that was wrong in OP's code was the placement of the `{}`. Also, it makes no sense to bust out Numpy for this, or to generalize in this way. Please do not invent questions to answer that OP did not ask. – Karl Knechtel Sep 22 '22 at 19:08
  • @KarlKnechtel: The OP is already using Numpy as evidenced in the code posted in the question. – Dennis Williamson Sep 22 '22 at 22:04