LoginSignup
2
2

More than 5 years have passed since last update.

python > 3つの小数を".3f, .3f, .3f"形式で表示 / [-1:1]の範囲の座標値を3つ取得

Last updated at Posted at 2016-05-31

3つの乱数を取得して、C言語でいうところの"%.3f, %.3f, %.3f"のような形式で表示を試みた。

import numpy as np
from numpy.random import *

pos = tuple( rand(3) )

frm = '%.3f, %.3f, %.3f'
print pos

print frm % tuple(pos)
結果
(0.076222111784701618, 0.76602747950227201, 0.13864621611927097)
0.076, 0.766, 0.139

参考 http://stackoverflow.com/questions/7568627/using-python-string-formatting-with-lists

v0.2

座標値を[-1,1]の範囲に変更。

import numpy as np
from numpy.random import *


for loop in range(15):
    pos = tuple( rand(3) * 2 - 1 )

    frm = '[ %.3f, %.3f, %.3f ],'
    # print pos

    print frm % tuple(pos)

(追記)
上記よりも @shiracamus さんのコメントの方が良いです。

2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2