回答編集履歴

1

内包表記をリストからタプルに、各種リンク追加

2021/04/11 10:50

投稿

actorbug
actorbug

スコア2224

test CHANGED
@@ -1,8 +1,8 @@
1
- 内包表記を使えば、こんな感じ。
1
+ [all](https://docs.python.org/ja/3/library/functions.html#all)と[ジェネレータ式](https://docs.python.org/ja/3/reference/expressions.html#generator-expressions)を使えば、こんな感じ。
2
2
 
3
3
  ```python
4
4
 
5
- if all(c in list for c in ['a', 'b', 'c', 'd', 'e', 'f', 'g']):
5
+ if all(c in list for c in ('a', 'b', 'c', 'd', 'e', 'f', 'g')):
6
6
 
7
7
  print('Hello World!')
8
8
 
@@ -10,7 +10,7 @@
10
10
 
11
11
 
12
12
 
13
- setに変換してよければ、こんな感じ。
13
+ [set型](https://docs.python.org/ja/3/library/stdtypes.html#set-types-set-frozenset)に変換してよければ(=要素がすべて[ハッシュ可能](https://docs.python.org/ja/3/glossary.html#term-hashable)なら)、こんな感じ。
14
14
 
15
15
  ```python
16
16
 
@@ -19,3 +19,15 @@
19
19
  print('Hello World!')
20
20
 
21
21
  ```
22
+
23
+
24
+
25
+ こちらでも同じ意味。
26
+
27
+ ```python
28
+
29
+ if {'a', 'b', 'c', 'd', 'e', 'f', 'g'}.issubset(list):
30
+
31
+ print('Hello World!')
32
+
33
+ ```