我是Prolog的新手,从终端启动prolog解释器的任务,输入consult('some_prolog_program.pl'),然后测试你刚写的谓词是非常耗时的,有没有办法运行脚本测试加快发展?
例如,在CI中可以写一个main我将使用我定义的函数,然后我可以执行:
make && ./a.out
为了测试代码,我可以用Prolog做类似的事情吗?
我一直在为财务会计模型构建一个演示(可能是一个小应用程序).我正在使用VBA/Excel进行数据输入(例如表格),计算和结果表示,我也从VBA程序向MS Access数据库运行SQL问题.原则上,这些组件的功能对我来说相当不错(我意识到VBA有点过时并且不是最流畅的).我已经这样做了一段时间了(而且我是一名具有其他语言的古老经验的工程师,虽然从未作为程序员工作过)并且几乎完全处于最佳状态,并且似乎能够找到我所做的编程错误.但是,当发生故障时我倾向于对VBA发疯,这似乎不是由我造成的,而是由于VBA的不稳定性/缺点.
所以我的问题是,完全没有使用Python:我可以基本上使用上面描述的Python吗?你能不能让我知道Python是否比VBA/Excel更稳定,更不容易出错?(我正在使用Windows 7)感谢您的建议
我有一个下面给出的单词列表(示例):
['the', 'counter', 'starts', 'the', 'starts', 'for']
Run Code Online (Sandbox Code Playgroud)
我想按顺序处理这个列表并生成一个pair (x,y)x随每个单词递增而y只有在看到一个唯一的单词时才会递增.因此,对于给定的示例,我的输出应该是:
[(1,1) (2,2), (3,3) (4,3) (5,3) (6,4)]
我不确定如何在python中执行此操作.如果我能够获得有关如何做到这一点的一些见解,那将是很棒的.谢谢.
所以基本上我想计算浮点出现在给定列表中的次数.例如:用户输入等级列表(所有得分均为100),并且它们以十个为一组进行分类.从0-10,10-20,20-30等分数出现多少次?像测试分数一样.我知道我可以使用计数功能,但因为我不是在找特定的数字,所以我遇到了麻烦.有没有结合计数和范围?谢谢你的帮助.
我刚刚发现,各种itertools函数返回的类类型在Python类型系统中均不视为生成器。
首先,设置:
import collections
import glob
import itertools
import types
ig = glob.iglob('*')
iz = itertools.izip([1,2], [3,4])
Run Code Online (Sandbox Code Playgroud)
然后:
>>> isinstance(ig, types.GeneratorType)
True
>>> isinstance(iz, types.GeneratorType)
False
Run Code Online (Sandbox Code Playgroud)
的glob.iglob()结果,或任何其他典型的发电机,是类型的types.GeneratorType。但是itertools结果不是。如果我想编写一个必须急切地评估其输入序列的函数,这会引起很多混乱-我需要知道它是否是生成器。
我找到了这个替代方案:
>>> isinstance(ig, collections.Iterator)
True
>>> isinstance(iz, collections.Iterator)
True
Run Code Online (Sandbox Code Playgroud)
但是,这并不理想,因为iter(x)是Iterator不管的x是混凝土(热切评估)序列,或发电机(懒洋洋地评估)。
最终目标是这样的:
def foo(self, sequence):
"""Store the sequence, making sure it is fully
evaluated before this function returns."""
if isinstance(sequence, types.GeneratorType):
self.sequence = list(sequence)
else:
self.sequence = sequence
Run Code Online (Sandbox Code Playgroud)
为什么要执行此操作的一个示例是,如果对序列的求值可能会引发异常,并且我希望从而foo()不是随后使用时引发该异常self.sequence。
我不喜欢这种 …
对于IEEE-754算术,倒数的最后位置是否保证0或1个单位?从那开始,是否有一个保证误差约束的倒数倒数?
如何制作70000及以上范围的for循环?我正在为所得税进行循环,当收入超过70000时,税率为30%.我会做点什么for income in range(income-70000)吗?
好吧,起初我开发了一个没有使用循环的代码,它运行得很好,但后来我被告知我需要在我的代码中加入一个循环.这就是我所拥有的,但对我来说使用for循环是没有意义的.有人能帮我吗?
def tax(income):
for income in range(10001):
tax = 0
for income in range(10002,30001):
tax = income*(0.1) + tax
for income in range(30002,70001):
tax = income*(0.2) + tax
for income in range(70002,100000):
tax = income*(0.3) + tax
print (tax)
Run Code Online (Sandbox Code Playgroud)
好的,所以我现在尝试使用while循环,但它没有返回值.告诉我你的想法.我需要根据收入计算所得税.先10000美元没有税.接下来的20000有10%.接下来40000有20%.超过70000是30%.
def taxes(income):
income >= 0
while True:
if income < 10000:
tax = 0
elif income > 10000 and income <= 30000:
tax = (income-10000)*(0.1)
elif income > 30000 and income <= 70000:
tax …Run Code Online (Sandbox Code Playgroud) 我有一份命令字典。我想将所有这些组合在一起,然后按每个中的fruit属性对其进行排序。我一直在通过下面的代码使用defaultdict对它们进行组合和排序。
super_dict_apple = defaultdict(list)
super_dict_orange = defaultdict(list)
super_dict_no_fruit = defaultdict(list)
for d in dict:
if 'fruit' not in d:
for k, v in d.iteritems():
super_dict_no_fruit[k].append(v)
elif d['fruit'] == 'Apple':
for k, v in d.iteritems():
super_dict_apple[k].append(v)
elif d['fruit'] == 'orange':
for k, v in d.iteritems():
super_dict_orange[k].append(v)
Run Code Online (Sandbox Code Playgroud)
这样我得到了一个键和所有相关的值,但是我失去了原始顺序。因此,我尝试使用orderDict来执行此操作,但是我无法使其正常工作,以下是我尝试过的操作。
from collections import OrderedDict
order_dict_no_fruit = OrderedDict()
order_dict_apple = OrderedDict()
order_dict_orange = OrderedDict()
for d in dict:
if 'fruit' not in d:
for k, v in d.iteritems():
order_dict_no_fruit[k].append(v)
elif d['fruit'] == 'Apple':
for k, …Run Code Online (Sandbox Code Playgroud) python ordereddictionary data-structures defaultdict dictionary-missing
如何在子类的重写方法中访问超类方法的局部变量?
class Foo(object):
def foo_method(self):
x = 3
class Bar(Foo):
def foo_method(self):
super().foo_method()
print(x) # Is there a way to access x, besides making x an attribute of the class?
Run Code Online (Sandbox Code Playgroud)
下面的代码给出了 NameError: name 'x' is not defined
bar = Bar()
bar.foo_method()
Run Code Online (Sandbox Code Playgroud)
这不足为奇,可以通过创建x实例属性来修复它,但是可以x直接访问Bar.foo_method吗?
我有这个代码抛出math domain error异常:
v = -1.0
for i in range (201):
print acos (v)
v += 0.01
Run Code Online (Sandbox Code Playgroud)
但是,如果我将其更改为此,它的工作原理如下:
v = -100
for i in range (201):
print acos (v / 100.0)
v += 1
Run Code Online (Sandbox Code Playgroud)
这是因为四舍五入吗?
如何在Python中最好地解决这个问题?或者我应该像上一个例子那样做?