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?

Pythonで「タプルの要素を昇順または降順に並び替える」の動作を確認してみた

Posted at

概要

Pythonで「タプルの要素を昇順または降順に並び替える」の動作を確認してみました。以下のページを参考にしました。

実装

以下のファイルを作成しました。

sample.py
mytuple = ("C", "A", "B")
# 並べ替えを行う
newlist = sorted(mytuple)
# リストからタプルを作成する
newtuple = tuple(newlist)
print(mytuple)
print(newtuple)

numtuple = (55, 78, 92, 82, 65)
newnumlist = sorted(numtuple)
newnumtuple = tuple(newnumlist)
print("Before:", numtuple)
print("After: ", newnumtuple)

animaltuple = ("Dog", "Cat", "Bear", "Deer")
newanimallist = sorted(animaltuple)
newanimaltuple = tuple(newanimallist)
print("Before:", animaltuple)
print("After: ", newanimaltuple)

numtuple = (5, 3.14, 4.78, 4)
newlist = sorted(numtuple)
print(tuple(newlist))

mytuple = ("80", 75, 45, "68")
#newlist = sorted(mytuple)

colortuple = ("Blue", "Red", "Green", "White", "Black")
upcolortuple = tuple(sorted(colortuple))
downcolortuple = tuple(sorted(colortuple, reverse=True))
print("Orig:", colortuple)
print("ASC: ", upcolortuple)
print("DESC:", downcolortuple)

以下のコマンドを実行しました。

$ python3 sample.py 
('C', 'A', 'B')
('A', 'B', 'C')
Before: (55, 78, 92, 82, 65)
After:  (55, 65, 78, 82, 92)
Before: ('Dog', 'Cat', 'Bear', 'Deer')
After:  ('Bear', 'Cat', 'Deer', 'Dog')
(3.14, 4, 4.78, 5)
Orig: ('Blue', 'Red', 'Green', 'White', 'Black')
ASC:  ('Black', 'Blue', 'Green', 'Red', 'White')
DESC: ('White', 'Red', 'Green', 'Blue', 'Black')
('C', 'b', 'A', 'E', 'd')
('A', 'C', 'E', 'b', 'd')
('A', 'b', 'C', 'd', 'E')

まとめ

何かの役に立てばと。

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