0
1

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] listを逆順にソート かつ 0除去する

Last updated at Posted at 2022-10-30

目的

数値の配列listから下記を行う。

  • 0除去
  • 逆順にソート

コード

testListExcluded = list()
testList = [0,1,0,2,0,0,3]
print(testList)

# 逆順にする
# reverse()が返すのはNoneなので注意。
testList.reverse()
print(testList)

# 0除去する
for value in testList:
    if value != 0:
        testListExcluded.append(value)
        
print(testListExcluded)

出力結果

[0, 1, 0, 2, 0, 0, 3]
[3, 0, 0, 2, 0, 1, 0]
[3, 2, 1]

0
1
5

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?