Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换“;而";加上;至于;在给定的python脚本中_Python - Fatal编程技术网

替换“;而";加上;至于;在给定的python脚本中

替换“;而";加上;至于;在给定的python脚本中,python,Python,我用python编写了以下脚本,效果很好: from sys import argv script, filename = argv def brokerage(cost): vol = 100 tamt = cost*vol br = (((cost*0.1)/100)*vol)*2 rc = (0.0074*tamt)/100 stt = (0.025*tamt)/100 std = (0.004*tamt)/100 stb = (

我用python编写了以下脚本,效果很好:

from sys import argv

script, filename = argv

def brokerage(cost):
    vol = 100
    tamt = cost*vol
    br = (((cost*0.1)/100)*vol)*2
    rc = (0.0074*tamt)/100
    stt = (0.025*tamt)/100
    std = (0.004*tamt)/100
    stb = (11*br)/100
    tbr = br+rc+stt+std+stb
    return(tbr)

cost1 = 10
cost2 = 510

while cost1 < 501 and cost2 < 1001:
    value1 = brokerage(cost1)
    value2 = brokerage(cost2)
    # Can't write 'float' to file so, changed it into string. Don't know why.
    x1 = str(value1)
    x2 = str(value2)
    line = "  |%d\t\t%s\t|\t|%d\t\t%s\t|" % (cost1, x1, cost2, x2)
    # use 'a' in 'open' function to append it, will not overwrite existing value
    # as with 'w'.
    save = open(filename, 'a')
    save.write(line)
    save.write("\n  |---------------------|\t|-----------------------|\n")
    save.close
    cost1+=10
    cost2+=10
从系统导入argv
脚本,文件名=argv
def经纪(成本):
体积=100
tamt=成本*体积
br=((成本*0.1)/100)*体积*2
rc=(0.0074*tamt)/100
stt=(0.025*tamt)/100
标准=(0.004*tamt)/100
机顶盒=(11*br)/100
tbr=br+rc+stt+std+stb
返回(待定)
成本1=10
成本2=510
当成本1<501和成本2<1001时:
价值1=经纪(成本1)
价值2=经纪(成本2)
#无法将“float”写入文件,因此,将其更改为字符串。不知道为什么。
x1=str(值1)
x2=str(值2)
line=“|%d\t\t%s\t |\t |%d\t\t%s\t |”%(成本1,x1,成本2,x2)
#使用“打开”函数中的“a”将其追加,不会覆盖现有值
#和“w”一样。
保存=打开(文件名“a”)
保存。写入(行)
save.write(“\n |----------------------|\t |--------------------------------|\n”)
保存。关闭
成本1+=10
成本2+=10
现在,我想用“for”代替“while”,代码结构变化最小

for cost1 in range(10, 501, 10):
    # Your code here...
    cost2 = cost1 + 500
(未测试)

(未测试)

或更快:

for cost1 in range(10,501,10):
    cost2 = cost1 + 500
    # Your code
或更快:

for cost1 in range(10,501,10):
    cost2 = cost1 + 500
    # Your code

你能给出想要使用for循环的原因吗?尝试遵循“艰苦学习Python”中给出的建议,即“尽可能避免while循环,而不是使用for循环”。你能给出想要使用for循环的原因吗?尝试遵循“艰苦学习Python”中给出的建议,即。尽量避免while循环,而使用for循环。“至少我认为第二个更快,因为它创建的列表少了一个,但增加了一个。它至少应该使用更少的内存。在Python3中,内存差异实际上可以忽略不计,在python2.x中,您可以通过分别将
range
zip
替换为
xrange
izip
来解决它。编辑在同样的方面,速度差异也可以忽略不计,仅仅产生一个int并不是一个真正的瓶颈。至少我认为第二个更快,因为它创建的列表少了一个,但添加了一个加法。它至少应该使用更少的内存。在Python3中,内存差异实际上可以忽略不计,在python2.x中,您可以通过分别将
range
zip
替换为
xrange
izip
来解决它。编辑在同样的方面,速度差也可以忽略不计,仅仅产生一个整数并不是一个真正的瓶颈。
for cost1 in range(10,501,10):
    cost2 = cost1 + 500
    # Your code