回答編集履歴

9

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

2019/01/14 14:32

投稿

Yoshitaket.
Yoshitaket.

スコア25

test CHANGED
@@ -33,6 +33,32 @@
33
33
  for i in map(lambda x:x.encode().hex(), text):
34
34
 
35
35
  print(f"0x{i} ", end="")
36
+
37
+ ```
38
+
39
+
40
+
41
+ 追記:
42
+
43
+ 文字列で扱うならforループいらないですね。
44
+
45
+
46
+
47
+ ```python
48
+
49
+ print(*map(lambda x:"0x"+x.encode().hex(), text), sep=" ")
50
+
51
+ ```
52
+
53
+
54
+
55
+ ```python
56
+
57
+ In [8]: text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
58
+
59
+ ...: print(*map(lambda x:"0x"+x.encode().hex(), text), sep=" ")
60
+
61
+ 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
36
62
 
37
63
  ```
38
64
 

8

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

2019/01/14 14:32

投稿

Yoshitaket.
Yoshitaket.

スコア25

test CHANGED
@@ -17,6 +17,22 @@
17
17
  for i in map(lambda x:int(x.encode().hex(),16), text):
18
18
 
19
19
  print(f"0x{i:02X} ", end="")
20
+
21
+ ```
22
+
23
+
24
+
25
+ .hex()をしたときに得られる値は'41'という文字列です。
26
+
27
+ print出力することだけが目的ならば数値への変換(map()の中でやっているint(x,16))が不要かもしれません。
28
+
29
+
30
+
31
+ ```python
32
+
33
+ for i in map(lambda x:x.encode().hex(), text):
34
+
35
+ print(f"0x{i} ", end="")
20
36
 
21
37
  ```
22
38
 
@@ -48,7 +64,7 @@
48
64
 
49
65
 
50
66
 
51
- textを"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'として実行します
67
+ textを"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'として実行。
52
68
 
53
69
 
54
70
 
@@ -70,7 +86,7 @@
70
86
 
71
87
 
72
88
 
73
- textを'Ascii string ?'として実行します
89
+ textを'Ascii string ?'として実行。
74
90
 
75
91
 
76
92
 
@@ -85,3 +101,25 @@
85
101
  0x41 0x73 0x63 0x69 0x69 0x20 0x73 0x74 0x72 0x69 0x6E 0x67 0x20 0x3F
86
102
 
87
103
  ```
104
+
105
+
106
+
107
+ **結果4**
108
+
109
+
110
+
111
+ 数値への変換なしのバージョンでtextを'Ascii string ?'として実行。
112
+
113
+
114
+
115
+ ```python
116
+
117
+ In [59]: text='Ascii string ?'
118
+
119
+ ...: for i in map(lambda x:x.encode().hex(), text):
120
+
121
+ ...: print(f"0x{i} ", end="")
122
+
123
+ 0x41 0x73 0x63 0x69 0x69 0x20 0x73 0x74 0x72 0x69 0x6e 0x67 0x20 0x3f
124
+
125
+ ```

7

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

2019/01/14 07:14

投稿

Yoshitaket.
Yoshitaket.

スコア25

test CHANGED
File without changes

6

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

2019/01/14 07:14

投稿

Yoshitaket.
Yoshitaket.

スコア25

test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  text='A'
16
16
 
17
- for i in map(lambda x:int(x.encode().hex(),16), list(text)):
17
+ for i in map(lambda x:int(x.encode().hex(),16), text):
18
18
 
19
19
  print(f"0x{i:02X} ", end="")
20
20
 
@@ -34,7 +34,7 @@
34
34
 
35
35
  In [42]: text='A'
36
36
 
37
- ...: for i in map(lambda x:int(x.encode().hex(),16), list(text)):
37
+ ...: for i in map(lambda x:int(x.encode().hex(),16), text):
38
38
 
39
39
  ...: print(f"0x{i:02X} ", end="")
40
40
 
@@ -54,9 +54,9 @@
54
54
 
55
55
  ```python
56
56
 
57
- In [43]: text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
57
+ In [48]: text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
58
58
 
59
- ...: for i in map(lambda x:int(x.encode().hex(),16), list(text)):
59
+ ...: for i in map(lambda x:int(x.encode().hex(),16), text):
60
60
 
61
61
  ...: print(f"0x{i:02X} ", end="")
62
62
 
@@ -76,9 +76,9 @@
76
76
 
77
77
  ```python
78
78
 
79
- In [44]: text='Ascii string ?'
79
+ In [49]: text='Ascii string ?'
80
80
 
81
- ...: for i in map(lambda x:int(x.encode().hex(),16), list(text)):
81
+ ...: for i in map(lambda x:int(x.encode().hex(),16), text):
82
82
 
83
83
  ...: print(f"0x{i:02X} ", end="")
84
84
 

5

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

2019/01/14 06:59

投稿

Yoshitaket.
Yoshitaket.

スコア25

test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
 
50
50
 
51
- textを"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'して実行します。
51
+ textを"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'して実行します。
52
52
 
53
53
 
54
54
 
@@ -68,7 +68,9 @@
68
68
 
69
69
  **結果3**
70
70
 
71
+
72
+
71
- textを'Ascii string ?'
73
+ textを'Ascii string ?'として実行します。
72
74
 
73
75
 
74
76
 

4

実行結果の例を追加

2019/01/14 06:55

投稿

Yoshitaket.
Yoshitaket.

スコア25

test CHANGED
@@ -54,14 +54,32 @@
54
54
 
55
55
  ```python
56
56
 
57
+ In [43]: text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
57
58
 
59
+ ...: for i in map(lambda x:int(x.encode().hex(),16), list(text)):
58
60
 
59
- text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
60
-
61
- for i in map(lambda x:int(x.encode().hex(),16), list(text)):
62
-
63
- print(f"0x{i:02X} ", end="")
61
+ ...: print(f"0x{i:02X} ", end="")
64
62
 
65
63
  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
66
64
 
67
65
  ```
66
+
67
+
68
+
69
+ **結果3**
70
+
71
+ textを'Ascii string ?'
72
+
73
+
74
+
75
+ ```python
76
+
77
+ In [44]: text='Ascii string ?'
78
+
79
+ ...: for i in map(lambda x:int(x.encode().hex(),16), list(text)):
80
+
81
+ ...: print(f"0x{i:02X} ", end="")
82
+
83
+ 0x41 0x73 0x63 0x69 0x69 0x20 0x73 0x74 0x72 0x69 0x6E 0x67 0x20 0x3F
84
+
85
+ ```

3

実行結果の例を追加

2019/01/14 06:53

投稿

Yoshitaket.
Yoshitaket.

スコア25

test CHANGED
File without changes

2

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

2019/01/14 06:53

投稿

Yoshitaket.
Yoshitaket.

スコア25

test CHANGED
@@ -14,9 +14,9 @@
14
14
 
15
15
  text='A'
16
16
 
17
- for i in map(lambda x:int(x.encode().hex()), list(text)):
17
+ for i in map(lambda x:int(x.encode().hex(),16), list(text)):
18
18
 
19
- print(f"0x{i:02} ", end="")
19
+ print(f"0x{i:02X} ", end="")
20
20
 
21
21
  ```
22
22
 
@@ -32,25 +32,11 @@
32
32
 
33
33
  ```python
34
34
 
35
- In [13]: text='A'
35
+ In [42]: text='A'
36
36
 
37
+ ...: for i in map(lambda x:int(x.encode().hex(),16), list(text)):
37
38
 
38
-
39
- In [14]: map(lambda x:int(x.encode().hex()), list(text))
40
-
41
- Out[14]: <map at 0x4479510>
42
-
43
-
44
-
45
- In [15]: list(map(lambda x:int(x.encode().hex()), list(text)))
46
-
47
- Out[15]: [41]
48
-
49
-
50
-
51
- In [16]: for i in map(lambda x:int(x.encode().hex()), list(text)):
52
-
53
- ...: print(f"0x{i:02} ", end="")
39
+ ...: print(f"0x{i:02X} ", end="")
54
40
 
55
41
  0x41
56
42
 
@@ -62,28 +48,20 @@
62
48
 
63
49
 
64
50
 
65
- textを"abcde"にして実行します。
51
+ textを"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'にして実行します。
66
52
 
67
53
 
68
54
 
69
55
  ```python
70
56
 
71
- In [17]: text='abcde'
72
57
 
73
58
 
59
+ text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
74
60
 
75
- In [19]: list(map(lambda x:int(x.encode().hex()), list(text)))
61
+ for i in map(lambda x:int(x.encode().hex(),16), list(text)):
76
62
 
77
- Out[19]: [61, 62, 63, 64, 65]
63
+ print(f"0x{i:02X} ", end="")
78
64
 
79
-
80
-
81
- In [20]: for i in map(lambda x:int(x.encode().hex()), list(text)):
65
+ 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
82
-
83
- ...: print(f"0x{i:02} ", end="")
84
-
85
- 0x61 0x62 0x63 0x64 0x65
86
-
87
-
88
66
 
89
67
  ```

1

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

2019/01/14 06:49

投稿

Yoshitaket.
Yoshitaket.

スコア25

test CHANGED
@@ -1,4 +1,8 @@
1
- 「なぜこうなるのか?」は、can110さん記載の通りかと思いますので、map と f-stringを使った方法を示します。
1
+ 「なぜこうなるのか?」は、can110さん記載の通りかと思いますので、map と f-stringを使った方法を示します。f-stringsはPython3.6で導入されたものですので、ご注意ください。
2
+
3
+
4
+
5
+ [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)
2
6
 
3
7
 
4
8