回答編集履歴

2

追記

2019/05/09 07:52

投稿

can110
can110

スコア38266

test CHANGED
@@ -17,3 +17,57 @@
17
17
  print('index=',i,'x=',x,'is str!') # たとえば「index= 1 x= 8 is str!」など
18
18
 
19
19
  ```
20
+
21
+
22
+
23
+ コメントを受けて
24
+
25
+ --
26
+
27
+ 変数`cd`はカンマ区切りの文字列のようなので、まずは数値リストに変換してやる必要があります。
28
+
29
+ その後やりたい処理としては以下のような感じでしょうか。
30
+
31
+ ```Python
32
+
33
+ import matplotlib.pyplot as plt
34
+
35
+ import numpy as np
36
+
37
+
38
+
39
+ mydict={0:(255,0,0),1:(0,255,0),2:(0,0,255),3:(255,255,0),4:(255,0,255),5:(0,255,255),6:(100,100,100),7:(100,100,0),8:(100,0,100),9:(0,100,100)}
40
+
41
+
42
+
43
+ cd = '8,5,3,7,4,7,8,5,4,3,1,4' # とりあえず3x4=12ピクセル分
44
+
45
+ cd = list(map(int,cd.split(','))) # カンマ区切り文字列→数値のリストに変換
46
+
47
+ print(cd) # [8, 5, 3, 7, 4, 7, 8, 5, 4, 3, 1, 4]
48
+
49
+
50
+
51
+ # 色番号から対応するRGB値を取得
52
+
53
+ conv = []
54
+
55
+ for x in cd:
56
+
57
+ conv.append(mydict[x])
58
+
59
+
60
+
61
+ # 画像に変換
62
+
63
+ pixels = np.array(conv).reshape(3,4,3)
64
+
65
+
66
+
67
+ plt.imshow(pixels)
68
+
69
+ plt.show()
70
+
71
+ ```
72
+
73
+ ![イメージ説明](775c2a032a97689108beb0ee38f62f08.png)

1

回答修正

2019/05/09 07:52

投稿

can110
can110

スコア38266

test CHANGED
@@ -4,16 +4,16 @@
4
4
 
5
5
  cdの中にstr型の`'8'`が含まれていませんか?
6
6
 
7
- 以下のようなコードで確認できます。
7
+ 以下のようなコードで、どの位置にstr型の要素があるか確認できます。
8
8
 
9
9
  ```Python
10
10
 
11
- cd=[8,'8']
11
+ cd=8,5,3,7,4,7,8,5,4,3,1,4,7........8,6,9 #60000個の0~9の数値が並んでます。
12
12
 
13
13
  for i,x in enumerate(cd):
14
14
 
15
15
  if type(x) == str:
16
16
 
17
- print('index=',i,'x=',x,'is str!') # index= 1 x= 8 is str!
17
+ print('index=',i,'x=',x,'is str!') # たとえば「index= 1 x= 8 is str!」など
18
18
 
19
19
  ```