将this文本文件转换为矩阵的最简单方法是什么?每行有一行,O
表示0
,X
表示1
答案 0 :(得分:10)
$url = "http://hyperpublic.com/challenge2input.txt";
StringCases[Import[$url, "Lines"], {"O" -> 0, "X" -> 1}]
答案 1 :(得分:9)
我首先将该文本保存在文件tmp.txt中。
In[180]:= words = ReadList["~danl/tmp.txt", Word];
vals = Map[Characters, words] /. {"O" -> 0, "X" -> 1};
In[182]:= vals[[1]]
Out[182]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Daniel Lichtblau Wolfram Research