LoginSignup
0
0

More than 1 year has passed since last update.

Python アルファベット順に並べる

Posted at

Pythonでアルファベット順に並べる方法

説明

公式ドキュメント
list.sort()メソッド、またはsorted()関数を使います。

sample.py
sorted([5, 2, 3, 1, 4])
#[1, 2, 3, 4, 5]

a = [5, 2, 3, 1, 4]
a.sort()
#[1, 2, 3, 4, 5]

The Zen of Pythonを並べ替え

The Zen of Pythonを使って試してみましょう。

The Zen of Python
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those! 
abc_order.py
with open("zen-of-python.txt") as f:
    data = f.read().split()
    new_data = sorted(data)
    print(new_data)

#実行結果
['*right*', '--', '--obvious', 'Although', 'Although', 'Although', 'Beautiful', 'Complex', 'Dutch.', 'Errors', 'Explicit', 'Flat', 'If', 'If', 'In', 'Namespaces', 'Now', 'Peters', 'Python,', 'Readability', 'Simple', 'Sparse', 'Special', 'The', 'There', 'Tim', 'Unless', 'Zen', 'a', 'a', 'ambiguity,', 'and', 'are', "aren't", 'at', 'bad', 'be', 'be', 'be', 'beats', 'better', 'better', 'better', 'better', 'better', 'better', 'better', 'better', 'break', 'by', 'cases', 'complex.', 'complicated.', 'counts.', 'dense.', 'do', 'do', 'easy', 'enough', 'explain,', 'explain,', 'explicitly', 'face', 'first', 'good', 'great', 'guess.', 'hard', 'honking', 'idea', 'idea.', 'idea.', 'implementation', 'implementation', 'implicit.', 'is', 'is', 'is', 'is', 'is', 'is', 'is', 'is', 'is', 'is', 'it', "it's", 'it.', "let's", 'may', 'may', 'more', 'nested.', 'never', 'never', 'never.', 'not', 'now.', 'obvious', 'of', 'of', 'of', 'often', 'one', 'one', 'one--', 'only', 'pass', 'practicality', 'preferably', 'purity.', 'refuse', 'rules.', 'should', 'should', 'silenced.', 'silently.', 'special', 'temptation', 'than', 'than', 'than', 'than', 'than', 'than', 'than', 'than', 'that', 'the', 'the', 'the', 'the', 'the', 'those!', 'to', 'to', 'to', 'to', 'to', 'ugly.', 'unless', 'way', 'way', "you're"]

まとめ

アルファベット文字列が要素のリストをソートすると、アルファベット順になって返ってきます。

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