Python: Collectionsモジュール
このドキュメントはPythonを学ぶガイドの一部です。完全なガイドはこちらからご覧いただけます:包括的なPythonガイド。
👋 Stackheroのドキュメントへようこそ!
Stackheroは、数多くの利点を提供するPythonクラウドソリューションを提供しています。主な利点は以下の通りです:
- シンプルな
git pushでアプリケーションを数秒でデプロイ。- 独自のドメイン名を使用し、HTTPS証明書の自動設定による強化されたセキュリティを享受。
- 自動バックアップ、ワンクリックアップデート、そしてシンプルで透明性のある予測可能な価格設定で安心を提供。
- プライベートで専用のVMによる最適なパフォーマンスと強固なセキュリティを実現。
時間を節約し、生活を簡素化: StackheroのPythonクラウドホスティングソリューションを試すのに5分しかかかりません!
collectionsモジュールは、プログラムを改善するための特殊なコンテナデータ型を提供します。
from collections import namedtuple, defaultdict, Counter, deque
例:
from collections import namedtuple, deque, Counter, OrderedDict, defaultdict
# namedtuple
Person = namedtuple('Person', 'name age')
person1 = Person("Alice", 30)
print(person1.name, person1.age) # 出力: Alice 30
# deque
dq = deque([1, 2, 3, 4, 5])
dq.append(6)
dq.appendleft(0)
print(dq) # 出力: deque([0, 1, 2, 3, 4, 5, 6])
# Counter
count = Counter("hello world")
print(count) # 出力: Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
# OrderedDict
od = OrderedDict()
od['a'] = 1
od['b'] = 2
od['c'] = 3
print(od) # 出力: OrderedDict([('a', 1), ('b', 2), ('c', 3)])
# defaultdict
dd = defaultdict(int)
dd['a'] += 1
dd['b'] += 2
print(dd) # 出力: defaultdict(<class 'int'>, {'a': 1, 'b': 2})