0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Python】文字整数 昇順ソートリスト作成 レシピ

Last updated at Posted at 2021-11-05

1.【やりたいこと】

array_1 = '10 6 8 1 5 9 3 4 7 2'

type(array_1)
> `class 'str'`

# やりたいこと
array_2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

2.【作成レシピ】

使用メソッド

  • 内包表記:array_1の文字型の整数をint型へ変換 (str型→int型)
  • 組み込み関数:sorted()
array_1 = '10 6 8 1 5 9 3 4 7 2'

# レシピ
array_2 = sorted([int(i) for i in array_1.split()])

# 完成
array_2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?