Note that there are some explanatory texts on larger screens.

plurals
  1. POpython math operations on list contents before output.write
    primarykey
    data
    text
    <p>I will put here a code with two endings, one that works, another that doesn't.</p> <p><em>I won't emphasize on what the purpose of the code is, since it is not the problem here...</em></p> <p><strong>The difference between my two endings</strong> is that in the second solution (that crashes), I wanted to apply some modification of the data.</p> <p>Here's the beginning of the code which is commons to the two <code>.py</code> files:</p> <pre><code>import tetgen, geometry from pprint import pprint import random, csv import numpy as np from pprint import pprint all_colors = [(name, float(X), float(Y), float(Z)) for name, X, Y, Z in csv.reader(open('colors.csv'))] priority_list = {name: int(i) for i, name in csv.reader(open('priority.csv'))} # background is marked SUPPORT support_i = [i for i, color in enumerate(all_colors) if color[0] == 'SUPPORT'] if len(support_i)&gt;0: support = np.array(all_colors[support_i[0]][1:]) del all_colors[support_i[0]] else: support = None tg, hull_i = geometry.tetgen_of_hull([(X,Y,Z) for name, X, Y, Z in all_colors]) colors = [all_colors[i] for i in hull_i] print ("thrown out: " + ", ".join(set(zip(*all_colors)[0]).difference(zip(*colors)[0]))) targets = [(name, float(X), float(Y), float(Z), float(BG)) for name, X, Y, Z, BG in csv.reader(open('targets.csv'))] for target in targets: name, X, Y, Z, BG = target target_point = support + (np.array([X,Y,Z]) - support)/(1-BG) tet_i, bcoords = geometry.containing_tet(tg, target_point) AT = (1-BG) </code></pre> <p>After this, the solution (let's call it (1)) works:</p> <pre><code> output = open('solution_AT.txt','a') if tet_i == None: output.write(str(target[0])) output.write('\n') else: names = [colors[i][0] for i in tg.tets[tet_i]] sorted_indices = sorted(enumerate(names), key=lambda (i, name): priority_list[name]) output.write(target[0]) counting = 0 for i, name in sorted(enumerate(names), key=lambda (i, name): priority_list[name]): output.write(',%s,%s' % (name, bcoords[i]*AT)) counting = counting + 1 if counting &gt; 3: output.write('\n') counting = 0 output.close() </code></pre> <p>but not the solution (2):</p> <pre><code> output = open('solution_AT.txt','a') if tet_i == None: output.write(str(target[0])) output.write('\n') else: names = [colors[i][0] for i in tg.tets[tet_i]] sorted_indices = sorted(enumerate(names), key=lambda (i, name): priority_list[name]) output.write(target[0]) counting = 0 for i, name in sorted(enumerate(names), key=lambda (i, name): priority_list[name]): counting = counting + 1 top = bcoords[i]*AT output.write(',%s,%s' % (name, top)) if counting &gt; 0: counting = counting + 1 cheese = bcoords[i]*AT output.write(',%s,%s' % (name, cheese/(1-top))) if counting &gt; 1: counting = counting + 1 meat = bcoords[i]*AT output.write(',%s,%s' % (name, meat/(1-top-cheese))) if counting &gt; 2: counting = counting + 1 bread = bcoords[i]*AT output.write(',%s,%s' % (name, bread/(1-top-cheese-meat)) if counting &gt; 3: output.write('\n') counting = 0 output.close() </code></pre> <p>I am getting the error: <code>Failed to run script - syntax error - invalid syntax</code> and the pointer says it's there (where I put "|") : <code>if counting &gt; |3:</code></p> <p>Do you have any idea why??</p> <p>As you can see, what I am trying to do is to apply these <strong>"top/cheese/meat"</strong> formulae to <code>bcoords[i]</code>, see below:</p> <p><strong>pseudocode:</strong></p> <pre><code>if counting = 0 // this is the initial value I want: - top = bcoords[i]*AT - counting = 1 if counting = 1 // the next value... - cheese = bcoords[i]*AT - output.write(',%s,%s' % (name, cheese/(1-top)) - counting = 2 if counting = 2 - meat = bcoords[i]*AT - output.write(',%s,%s' % (name, meat/(1-top-cheese)) - counting = 3 if counting &gt; 2: - counting = counting + 1 - bread = bcoords[i]*AT - output.write(',%s,%s' % (name, bread/(1-top-cheese-meat)) </code></pre> <p>But it doesn't works AT ALL!</p> <p>Any idea?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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