[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
[[ノート/Python]]~
訪問者数 &counter(); 最終更新 &lastmod();
**Pythonのitertoolsパッケージ [#t7bbf398]
itertoolsパッケージは組合せとかを生成してくれる。
参考
-[[組み合わせと順列:http://tanopy.blog79.fc2.com/blog-entry-48.html]]
***使い方 [#wf908584]
import itertools
for c in itertools.combinations([1,2,3,4,5],2):
print(c)
実際使ってみた
dict = {"yamada":75, "endou":82}
print dict.keys()
> ['yamada', 'endou']
print dict.values()
> [75, 82]
print dict.items()
> [('yamada', 75), ('endou', 82)]
list = dict.items()
list
> [('yamada', 75), ('endou', 82)]
list.append(('tanaka', 65))
list
> [('yamada', 75), ('endou', 82), ('tanaka', 65)]
import itertools
for c in itertools.combinations(list,2):
print(c)
> (('yamada', 75), ('endou', 82))
> (('yamada', 75), ('tanaka', 65))
> (('endou', 82), ('tanaka', 65))
というわけで、何も考えずに組合せができてしまう。
終了行:
[[ノート/Python]]~
訪問者数 &counter(); 最終更新 &lastmod();
**Pythonのitertoolsパッケージ [#t7bbf398]
itertoolsパッケージは組合せとかを生成してくれる。
参考
-[[組み合わせと順列:http://tanopy.blog79.fc2.com/blog-entry-48.html]]
***使い方 [#wf908584]
import itertools
for c in itertools.combinations([1,2,3,4,5],2):
print(c)
実際使ってみた
dict = {"yamada":75, "endou":82}
print dict.keys()
> ['yamada', 'endou']
print dict.values()
> [75, 82]
print dict.items()
> [('yamada', 75), ('endou', 82)]
list = dict.items()
list
> [('yamada', 75), ('endou', 82)]
list.append(('tanaka', 65))
list
> [('yamada', 75), ('endou', 82), ('tanaka', 65)]
import itertools
for c in itertools.combinations(list,2):
print(c)
> (('yamada', 75), ('endou', 82))
> (('yamada', 75), ('tanaka', 65))
> (('endou', 82), ('tanaka', 65))
というわけで、何も考えずに組合せができてしまう。
ページ名: