LoginSignup
0
1

More than 3 years have passed since last update.

【python】sortとsortedの違い (Colaboratory)

Posted at

【python】sortとsortedの違い

pythonのsortとsortedの違いについて。
どちらも昇順ソートするアルゴリズム。

違いは破壊的かどうか。

sort:破壊的(元のデータが変わる)
sorted: 破壊しない(元のデータはそのまま)

#sortの場合
a = [2,3,4,5,1]
a.sort()
print(f'a:{a}')

#a:[1, 2, 3, 4, 5]

sortは元のデータ自体が変わる。

#sortedの場合
b = [2,3,4,5,1]
c = sorted(b)
print(f'b:{b}')
print(f'c:{c}')

#b:[2, 3, 4, 5, 1]
#c:[1, 2, 3, 4, 5]

sortedでは元のデータはそのまま。



ちなみに、sortはメソッド、sortedは組み込み関数


Colaboratoryとは?

googleの提供するオンラインのpython解析ツール。
pythonのコード実行結果を確認するときに便利。

JupyterNotebookも便利だが、インストールなど手間がかかる。Colaboratoryはページを開くだけなので簡単。

・ノートブックを新規作成
image.png

・画面上部の+でコードやテキストボックスを追加できる
image.png

コードの実行はShift+Enter

スプシなどと同様に他の人と共有できる。ボックスにコメントを記載できたりと何かと便利。

0
1
0

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
0
1