teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

9

forループを抜いたバージョンを記載

2019/01/14 14:32

投稿

Yoshitaket.
Yoshitaket.

スコア25

answer CHANGED
@@ -18,6 +18,19 @@
18
18
  print(f"0x{i} ", end="")
19
19
  ```
20
20
 
21
+ 追記:
22
+ 文字列で扱うならforループいらないですね。
23
+
24
+ ```python
25
+ print(*map(lambda x:"0x"+x.encode().hex(), text), sep=" ")
26
+ ```
27
+
28
+ ```python
29
+ In [8]: text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
30
+ ...: print(*map(lambda x:"0x"+x.encode().hex(), text), sep=" ")
31
+ 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a
32
+ ```
33
+
21
34
  以下は、Jupyter QtConsole 4.3.1の実行結果です。
22
35
 
23
36
  **結果1**

8

数値への変換(map()の中でやっているint(x,16))が不要かも知れない旨を追記

2019/01/14 14:32

投稿

Yoshitaket.
Yoshitaket.

スコア25

answer CHANGED
@@ -10,6 +10,14 @@
10
10
  print(f"0x{i:02X} ", end="")
11
11
  ```
12
12
 
13
+ .hex()をしたときに得られる値は'41'という文字列です。
14
+ print出力することだけが目的ならば数値への変換(map()の中でやっているint(x,16))が不要かもしれません。
15
+
16
+ ```python
17
+ for i in map(lambda x:x.encode().hex(), text):
18
+ print(f"0x{i} ", end="")
19
+ ```
20
+
13
21
  以下は、Jupyter QtConsole 4.3.1の実行結果です。
14
22
 
15
23
  **結果1**
@@ -23,7 +31,7 @@
23
31
 
24
32
  **結果2**
25
33
 
26
- textを"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'として実行します
34
+ textを"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'として実行。
27
35
 
28
36
  ```python
29
37
  In [48]: text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
@@ -34,11 +42,22 @@
34
42
 
35
43
  **結果3**
36
44
 
37
- textを'Ascii string ?'として実行します
45
+ textを'Ascii string ?'として実行。
38
46
 
39
47
  ```python
40
48
  In [49]: text='Ascii string ?'
41
49
  ...: for i in map(lambda x:int(x.encode().hex(),16), text):
42
50
  ...: print(f"0x{i:02X} ", end="")
43
51
  0x41 0x73 0x63 0x69 0x69 0x20 0x73 0x74 0x72 0x69 0x6E 0x67 0x20 0x3F
52
+ ```
53
+
54
+ **結果4**
55
+
56
+ 数値への変換なしのバージョンでtextを'Ascii string ?'として実行。
57
+
58
+ ```python
59
+ In [59]: text='Ascii string ?'
60
+ ...: for i in map(lambda x:x.encode().hex(), text):
61
+ ...: print(f"0x{i} ", end="")
62
+ 0x41 0x73 0x63 0x69 0x69 0x20 0x73 0x74 0x72 0x69 0x6e 0x67 0x20 0x3f
44
63
  ```

7

数値への変換(map()の中でやっているint(x,16))が不要かも知れない旨を追記

2019/01/14 07:14

投稿

Yoshitaket.
Yoshitaket.

スコア25

answer CHANGED
File without changes

6

mapに与える引数は、list(text)ではなく、textのままでOKであったので修正した

2019/01/14 07:14

投稿

Yoshitaket.
Yoshitaket.

スコア25

answer CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  ```python
8
8
  text='A'
9
- for i in map(lambda x:int(x.encode().hex(),16), list(text)):
9
+ for i in map(lambda x:int(x.encode().hex(),16), text):
10
10
  print(f"0x{i:02X} ", end="")
11
11
  ```
12
12
 
@@ -16,7 +16,7 @@
16
16
 
17
17
  ```python
18
18
  In [42]: text='A'
19
- ...: for i in map(lambda x:int(x.encode().hex(),16), list(text)):
19
+ ...: for i in map(lambda x:int(x.encode().hex(),16), text):
20
20
  ...: print(f"0x{i:02X} ", end="")
21
21
  0x41
22
22
  ```
@@ -26,8 +26,8 @@
26
26
  textを"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'として実行します。
27
27
 
28
28
  ```python
29
- In [43]: text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
29
+ In [48]: text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
30
- ...: for i in map(lambda x:int(x.encode().hex(),16), list(text)):
30
+ ...: for i in map(lambda x:int(x.encode().hex(),16), text):
31
31
  ...: print(f"0x{i:02X} ", end="")
32
32
  0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4A 0x4B 0x4C 0x4D 0x4E 0x4F 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5A
33
33
  ```
@@ -37,8 +37,8 @@
37
37
  textを'Ascii string ?'として実行します。
38
38
 
39
39
  ```python
40
- In [44]: text='Ascii string ?'
40
+ In [49]: text='Ascii string ?'
41
- ...: for i in map(lambda x:int(x.encode().hex(),16), list(text)):
41
+ ...: for i in map(lambda x:int(x.encode().hex(),16), text):
42
42
  ...: print(f"0x{i:02X} ", end="")
43
43
  0x41 0x73 0x63 0x69 0x69 0x20 0x73 0x74 0x72 0x69 0x6E 0x67 0x20 0x3F
44
44
  ```

5

実行結果の例の説明を修正

2019/01/14 06:59

投稿

Yoshitaket.
Yoshitaket.

スコア25

answer CHANGED
@@ -23,7 +23,7 @@
23
23
 
24
24
  **結果2**
25
25
 
26
- textを"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'して実行します。
26
+ textを"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'して実行します。
27
27
 
28
28
  ```python
29
29
  In [43]: text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
@@ -33,8 +33,9 @@
33
33
  ```
34
34
 
35
35
  **結果3**
36
- textを'Ascii string ?'
37
36
 
37
+ textを'Ascii string ?'として実行します。
38
+
38
39
  ```python
39
40
  In [44]: text='Ascii string ?'
40
41
  ...: for i in map(lambda x:int(x.encode().hex(),16), list(text)):

4

実行結果の例を追加

2019/01/14 06:55

投稿

Yoshitaket.
Yoshitaket.

スコア25

answer CHANGED
@@ -26,9 +26,18 @@
26
26
  textを"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'にして実行します。
27
27
 
28
28
  ```python
29
+ In [43]: text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
30
+ ...: for i in map(lambda x:int(x.encode().hex(),16), list(text)):
31
+ ...: print(f"0x{i:02X} ", end="")
32
+ 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4A 0x4B 0x4C 0x4D 0x4E 0x4F 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5A
33
+ ```
29
34
 
35
+ **結果3**
30
- text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
36
+ text'Ascii string ?'
37
+
38
+ ```python
39
+ In [44]: text='Ascii string ?'
31
- for i in map(lambda x:int(x.encode().hex(),16), list(text)):
40
+ ...: for i in map(lambda x:int(x.encode().hex(),16), list(text)):
32
- print(f"0x{i:02X} ", end="")
41
+ ...: print(f"0x{i:02X} ", end="")
33
- 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4A 0x4B 0x4C 0x4D 0x4E 0x4F 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5A
42
+ 0x41 0x73 0x63 0x69 0x69 0x20 0x73 0x74 0x72 0x69 0x6E 0x67 0x20 0x3F
34
43
  ```

3

実行結果の例を追加

2019/01/14 06:53

投稿

Yoshitaket.
Yoshitaket.

スコア25

answer CHANGED
File without changes

2

int(x,16)にしていなかったため、うまく動かない文字があったので修正

2019/01/14 06:53

投稿

Yoshitaket.
Yoshitaket.

スコア25

answer CHANGED
@@ -6,8 +6,8 @@
6
6
 
7
7
  ```python
8
8
  text='A'
9
- for i in map(lambda x:int(x.encode().hex()), list(text)):
9
+ for i in map(lambda x:int(x.encode().hex(),16), list(text)):
10
- print(f"0x{i:02} ", end="")
10
+ print(f"0x{i:02X} ", end="")
11
11
  ```
12
12
 
13
13
  以下は、Jupyter QtConsole 4.3.1の実行結果です。
@@ -15,31 +15,20 @@
15
15
  **結果1**
16
16
 
17
17
  ```python
18
- In [13]: text='A'
18
+ In [42]: text='A'
19
-
20
- In [14]: map(lambda x:int(x.encode().hex()), list(text))
21
- Out[14]: <map at 0x4479510>
22
-
23
- In [15]: list(map(lambda x:int(x.encode().hex()), list(text)))
24
- Out[15]: [41]
25
-
26
- In [16]: for i in map(lambda x:int(x.encode().hex()), list(text)):
19
+ ...: for i in map(lambda x:int(x.encode().hex(),16), list(text)):
27
- ...: print(f"0x{i:02} ", end="")
20
+ ...: print(f"0x{i:02X} ", end="")
28
21
  0x41
29
22
  ```
30
23
 
31
24
  **結果2**
32
25
 
33
- textを"abcde"にして実行します。
26
+ textを"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'にして実行します。
34
27
 
35
28
  ```python
36
- In [17]: text='abcde'
37
29
 
38
- In [19]: list(map(lambda x:int(x.encode().hex()), list(text)))
30
+ text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
39
- Out[19]: [61, 62, 63, 64, 65]
40
-
41
- In [20]: for i in map(lambda x:int(x.encode().hex()), list(text)):
31
+ for i in map(lambda x:int(x.encode().hex(),16), list(text)):
42
- ...: print(f"0x{i:02} ", end="")
32
+ print(f"0x{i:02X} ", end="")
43
- 0x61 0x62 0x63 0x64 0x65
33
+ 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4A 0x4B 0x4C 0x4D 0x4E 0x4F 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5A
44
-
45
34
  ```

1

python3.6以降のコードであることを記載

2019/01/14 06:49

投稿

Yoshitaket.
Yoshitaket.

スコア25

answer CHANGED
@@ -1,5 +1,7 @@
1
- 「なぜこうなるのか?」は、can110さん記載の通りかと思いますので、map と f-stringを使った方法を示します。
1
+ 「なぜこうなるのか?」は、can110さん記載の通りかと思いますので、map と f-stringを使った方法を示します。f-stringsはPython3.6で導入されたものですので、ご注意ください。
2
2
 
3
+ [Python3.6 から追加された文法機能 - Qiita](https://qiita.com/shirakiya/items/2767b30fd4f9c05d930b#%E6%9B%B8%E5%BC%8F%E5%8C%96%E6%B8%88%E3%81%BF%E6%96%87%E5%AD%97%E5%88%97%E3%83%AA%E3%83%86%E3%83%A9%E3%83%ABf-strings)
4
+
3
5
  **コード**
4
6
 
5
7
  ```python