<p>首先,完全公开:这将是一个uni分配,所以我不想收到代码。:). 我更多的是寻找方法;我对python很陌生,读过一本书,但还没有写过任何代码。</p>
<p>整个任务是导入CSV文件的内容,从CSV文件的内容创建一个决策树(使用<a href="http://en.wikipedia.org/wiki/ID3_algorithm" rel="nofollow noreferrer">ID3 algorithm</a>),然后解析第二个CSV文件以在树上运行。有一个很大的(可以理解的)偏好是让它能够处理不同的CSV文件(我问过我们是否允许硬编码列名,主要是为了排除这种可能性,而答案是否定的)。</p>
<p>CSV文件采用相当标准的格式;标题行用#标记,然后显示列名,之后的每一行都是一系列简单的值。示例:</p>
<pre><code># Column1, Column2, Column3, Column4
Value01, Value02, Value03, Value04
Value11, Value12, Value13, Value14
</code></pre>
<p>目前,我正在努力解决第一部分:解析CSV。为了为决策树做出决策,字典结构似乎是最符合逻辑的;所以我想按照以下思路做些事情:</p>
<pre><code>Read in each line, character by character
If the character is not a comma or a space
<a href="https://www.cnpython.com/list/append" class="inner-link">append</a> character to temporary string
If the character is a comma
Append the temporary string to a list
Empty string
Once a line has been read
Create a dictionary using the header row as the key (somehow!)
Append that dictionary to a list
</code></pre>
<p>但是,如果我这样做,我不知道如何在键和值之间建立映射。我还想知道是否有办法对列表中的每一个字典执行一个操作,因为我需要做的事情是“每个人都返回第1列和第4列的值,这样我就可以计算出谁拥有什么!”-我认为有某种机制,但我不知道该怎么做。</p>
<p>字典是最好的方法吗?使用其他数据结构做事情会更好吗?如果是,怎么办?</p>