Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>All the values that are being put into the 'val' list are negative decimals.</p> </blockquote> <p>No, they aren't; they're <em>lists of</em> strings that represent negative decimals, since the <code>.split()</code> call produces a list. <code>maxv</code> and <code>minv</code> are lists of strings, which can't be fed to <code>float()</code>.</p> <blockquote> <p>What is the error referring to, and how do I fix it? </p> </blockquote> <p>It's referring to the fact that the contents of <code>val</code> aren't what you think they are. The first step in debugging is to verify your assumptions. If you try this code out at the REPL, then you could inspect the contents of <code>maxv</code> and <code>minv</code> and notice that you have lists of strings rather than the expected strings.</p> <p>I assume you want to put all the lists of strings (from each line of the file) together into a single list of strings. Use <code>val.extend(lineval)</code> rather than <code>val.append(lineval)</code>.</p> <p>That said, you'll still want to <code>map</code> the strings into floats before calling <code>max</code> or <code>min</code> because otherwise you will be comparing the strings as strings rather than floats. (It might well work, but explicit is better than implicit.)</p> <p>Simpler yet, just read the entire file at once and split it; <code>.split()</code> without arguments splits on whitespace, and a newline is whitespace. You can also do the <code>map</code>ping at the same point as the reading, with careful application of a list comprehension. I would write:</p> <pre><code>with open('clusters_scores.out') as f: val = [float(x) for x in f.read().split()] result = max(val) - min(val) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload