0

我将在这里放一个有两个结尾的代码,一个有效,另一个无效。

我不会强调代码的目的是什么,因为这不是这里的问题......

我的两个结局之间的区别在于,在第二个解决方案(崩溃)中,我想对数据进行一些修改。

这是两个.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:

你知道为什么吗?

如您所见,我要做的是将这些“顶级/奶酪/肉类”公式应用于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))

但它根本不起作用!

任何想法?

谢谢

4

2 回答 2

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
于 2013-08-07T02:02:19.787 回答
1

你之前有 3 个(,只有 2 个)在行

所以 Python 正在像这样有效地解析它

output.write(',%s,%s' % (name, cheese/(1-top)) if counting > 1:...

if显然是那里的语法错误

于 2013-08-07T02:04:31.523 回答