我将在这里放置一个带有两个结尾的代码,一个有用,另一个没有。
我不会强调代码的目的是什么,因为这不是问题......
我的两个结局之间的区别是在第二个解决方案(崩溃)中,我想要 应用一些数据修改。
这是代码的开头,它是两个.py
文件的共同点:
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)>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)
在此之后,解决方案(让我们称之为(1))起作用:
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 > 3:
output.write('\n')
counting = 0
output.close()
但不是解决方案(2):
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 > 0:
counting = counting + 1
cheese = bcoords[i]*AT
output.write(',%s,%s' % (name, cheese/(1-top)))
if counting > 1:
counting = counting + 1
meat = bcoords[i]*AT
output.write(',%s,%s' % (name, meat/(1-top-cheese)))
if counting > 2:
counting = counting + 1
bread = bcoords[i]*AT
output.write(',%s,%s' % (name, bread/(1-top-cheese-meat))
if counting > 3:
output.write('\n')
counting = 0
output.close()
我收到错误:Failed to run script - syntax error - invalid syntax
并且指针显示它在那里(我放“|”):if counting > |3:
你知道为什么吗?
正如您所看到的,我要做的是将这些“top / cheese / meat”公式应用于bcoords[i]
,见下文:
伪代码:
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 > 2:
- counting = counting + 1
- bread = bcoords[i]*AT
- output.write(',%s,%s' % (name, bread/(1-top-cheese-meat))
但它并不适用于所有人!
有什么想法吗?
由于
答案 0 :(得分:1)
您似乎有缩进问题。我在代码中观察到的是,以下代码行(if
块)尝试访问在name
循环中定义的变量top
和for
,即使它们实际上是在for
循环之外。
缩进这些语句,使它们属于for
循环。如果有效,请告诉我们
if counting > 0:
counting = counting + 1
cheese = bcoords[i]*AT
output.write(',%s,%s' % (name, cheese/(1-top))
if counting > 1:
counting = counting + 1
meat = bcoords[i]*AT
output.write(',%s,%s' % (name, meat/(1-top-cheese))
if counting > 2:
counting = counting + 1
bread = bcoords[i]*AT
output.write(',%s,%s' % (name, bread/(1-top-cheese-meat))
if counting > 3:
output.write('\n')
counting = 0
答案 1 :(得分:1)
之前,您有3
(
,只有2 )
因此Python正在有效地解析它
output.write(',%s,%s' % (name, cheese/(1-top)) if counting > 1:...
和if
显然是语法错误