【问题标题】:python math operations on list contents before output.write在 output.write 之前对列表内容进行 python 数学运算
【发布时间】:2013-08-07 01:18:12
【问题描述】:

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

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

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

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

但它根本不起作用!

有什么想法吗?

谢谢

【问题讨论】:

  • 是否有回溯/错误信息?
  • 脚本运行失败:Failed to run script: syntax error -invalid syntax 并停在那里(我放的地方:“|”):if counting > 1|:
  • 会不会只是缩进问题??我尝试了许多变体,但没有解决任何问题

标签: python math csv output


【解决方案1】:

您似乎遇到了缩进问题。我在您的代码中观察到的是以下代码行(if 块)尝试访问在for 循环中定义的变量nametop,即使它们实际上不在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

【讨论】:

  • mmm....我试着按照你说的改进(见上面(2)的编辑),但还是不行,还是有语法错误,但不一样地方...我不明白
  • 您好,现在可以了,但是还有一个错误,请看下面
【解决方案2】:

您有 3 个(,而之前只有 2 个)

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

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

if 显然是那里的语法错误

【讨论】:

  • 嗯,你有一堆问题要解决。那么错误信息就会不同,所以你需要告诉我们下一个问题是什么。
  • 天哪,我忘了一个……这么简单的问题!谢谢,还是有问题,请看我下面的帖子
猜你喜欢
  • 1970-01-01
  • 2017-08-07
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多