在我的一个类中,我有许多方法都可以从相同的字典中获取值.但是,如果其中一个方法试图访问不存在的值,则必须调用另一个方法来使该值与该键相关联.
我目前实现如下,其中findCrackDepth(tonnage)为self.lowCrackDepth [tonnage]赋值.
if tonnage not in self.lowCrackDepth:
self.findCrackDepth(tonnage)
lcrack = self.lowCrackDepth[tonnage]
Run Code Online (Sandbox Code Playgroud)
但是,我也可以这样做
try:
lcrack = self.lowCrackDepth[tonnage]
except KeyError:
self.findCrackDepth(tonnage)
lcrack = self.lowCrackDepth[tonnage]
Run Code Online (Sandbox Code Playgroud)
我假设两者之间的性能差异与值在字典中的频率有关.这个差异有多大?我正在生成几百万个这样的值(在该类的许多实例中分布在许多字典中),并且每次该值不存在时,它可能有两次.
我主要是出于好奇而提出这个问题.我写了一些代码,这些代码正在做一些非常耗时的工作.所以,在执行我的workhorse函数之前,我把它包装成了对time.clock()的几次调用.它看起来像这样:
t1 = time.clock()
print this_function_takes_forever(how_long_parameter = 20)
t2 = time.clock()
print t2 - t1
Run Code Online (Sandbox Code Playgroud)
这很好.我的功能正确返回并t2 - t1给了我一个972.29或大约16分钟的结果.
但是,当我将代码更改为此时
t1 = time.clock()
print this_function_takes_forever(how_long_parameter = 80)
t2 = time.clock()
print t2 - t1
Run Code Online (Sandbox Code Playgroud)
我的功能仍然很好,但结果t2 - t1是:
None
-1741
Run Code Online (Sandbox Code Playgroud)
我很好奇实施细节导致了什么.None和负数都让我感到困惑.它与签名类型有关吗?这是None怎么解释的?
我正在尝试创建一个类似于shell的环境,其中向用户显示">>>"并且可以键入任意多个预定义的命令.但是,我能想到实现这一点的唯一方法是使用字典映射命令 - > code和python的"exec".
有没有更正确的方法呢?
我正在尝试制作一个python程序来返回具有双连续字母的单词(例如,门,球,地板).我的代码到现在为止如下所示,但它将文件中的所有单词分成两个字母的部分:
def text_processing( file_location ):
import re
file_variable = open( file_location )
lines = file_variable.read()
print lines
double_letter = re.compile('[A-Z]{2,2}', re.IGNORECASE)
double_letter_list = double_letter.findall(lines)
print double_letter_list
Run Code Online (Sandbox Code Playgroud) 我一直收到错误'IndexError:list index out of range'
不知道我做错什么它适用于一对夫妇运行错误我唯一能想到的是我得到的名字太长了变量
mylist = [x +'.mp3' for x in re.findall(r'file=(.*?).mp3',the_webpage)]
#Remove Duplicates
if mylist:
mylist.sort()
last = mylist[-1]
for i in range(len(mylist)-3, -1, -1):
if last == mylist[i]:
del mylist[i]
else:
last = mylist[i]
print " "
#takes the quotes out of the string
#ti = titl1[0]
n1 = titl1[0]
n2 = song1[0]
#sg = song1
sname = "".join(tuple(n1 + "-" + n2 + ".mp3"))
print sname
url = mylist[0]
Run Code Online (Sandbox Code Playgroud)
结果
Traceback (most recent call last): …Run Code Online (Sandbox Code Playgroud) 到目前为止我所拥有的:
def balance_equation(species,coeff):
data=zip(coeff,species)
positive=[]
negative=[]
for (mul,el) in data:
if int(mul)<0:
negative.append((el,mul))
if int(mul)>0:
positive.append((el,mul))
Run Code Online (Sandbox Code Playgroud)
我知道这不会打印任何东西.我所拥有的是一个函数,它在两个列表species=['H2O','O2','CO2']和coeff=['1','3','-4'].我需要它像这样打印:
1H20+3O2=4CO2
我首先将负系数和物种放在一个列表中,而将积极放在另一个列表中.我似乎能够让两个打印正确.
我有大型csv文件,216961行:
9808,54,43,59,999,17,10,59,999,-1,0,0
9809,54,43,59,999,17,12,0,-1,0,0
9810,54,43,59,999,17,13,0,001,-1,0,0
9811,54,43,59,999,17,13,59,999,-1,0,0
9812,54,43,59,999,17,15,0,-1,0,0
9813,54,43,59,999,17,16,0,001,0,0,0
9814,54,43,59,999,17,16,59,999,0,0,0
9815,54,43,59,999,17,18,0,0,0,0
9816,54,43,59,999,17,19,0,001,0,0,0
9817,54,43,59,999,17,19,59,999,0,0,0
9818,54,43,59,999,17,21,0,0,0,0
9819,54,43,59,999,17,22,0,001,0,0,0
9820,54,43,59,999,17,22,59,999,0,0,0
Run Code Online (Sandbox Code Playgroud)
我需要筛选文件并删除所有存在-1的行,大约6900行,所以我想请求你的帮助
我的尝试:
import csv
reader = csv.reader(open("file.csv", "rb"), delimiter=',')
for line in reader:
match = line[7]
if match not in '-1':
print line
Run Code Online (Sandbox Code Playgroud)
但-1并不总是在线[7]
就像标题所说的那样,我正在尝试编写一个带有(x,y)坐标列表的程序,并确定是否有3个点共线(位于具有相同斜率的直线上)
我收到一些错误消息.就目前而言,我得到一个"TypeError:'int'对象不可订阅"消息.如果我取出collinearityTest调用areCollinear函数的部分,我会得到一个"索引超出范围"错误.我是python的新手,只是想学习.
def areCollinear(p1, p2, p3):
slope1 = (p2[1] - p1[1]) / (p2[0] - p1[0])
slope2 = (p3[1] - p2[1]) / (p3[0] - p2[0])
if slope1 == slope2:
print "Points are colinear"
else:
print "Points are NOT colinear, what's the matter with you?"
def collinearityTest(pointList):
position = 0
while position >=0 and position < len(pointList):
for p1 in pointList[position]:
position = position + 1
for p2 in pointList[position]:
position = position + 1
for p3 in pointList[position]:
position = position …Run Code Online (Sandbox Code Playgroud) 我正试图解决一个问题,但是我觉得我得到的循环在某个地方有一个缺失的链接......
我已经给出了简报 - "现在编写一个程序,计算所需的最低固定月付款,以便在12个月内偿还信用卡余额."
从本质上讲,我所做的远远不是代码,它会取一个基本价值(例如10),从信用卡余额(考虑到利息)中拿走它,如果是它的总月数将余额变为负数(例如已付清)高于12,它会增加'minmonth'(每月还清金额),直到月数等于或低于12.
提前道歉,我实际上只学习了2天的Python!
我哪里错了?
balance = float(raw_input('Enter the outstanding balance on your creditcard: '))
interest = float(raw_input('Enter the annual credit card interest rate as a decimal: '))
minmonth = 10
months = 0
monthlyinterest = interest / 12
while(balance > 0):
balance = balance * (1 + monthlyinterest) - minmonth
months = months + 1
if(months > 12):
months = 0
minmonth = minmonth + 10
else:
print 'RESULT!'
print 'Total amount to pay per month would …Run Code Online (Sandbox Code Playgroud) 如何在直线上找到距离给定点特定距离的点.我用C编写这段代码,但我得不到正确的答案.难道你们有人指导我做错了什么.
我得到x1,y1,x2,y2值和剩下的距离很好.使用这些我可以找到斜率m和y截距也很好.现在,我需要在连接这两个点的直线上找到点,该点距点x1,y1 10个单位.我好像在这里出错了.这是我写的代码.
int x1 = node[n].currentCoordinates.xCoordinate;
int y1 = node[n].currentCoordinates.yCoordinate;
int x2 = node[n].destinationLocationCoordinates.xCoordinate;
int y2 = node[n].destinationLocationCoordinates.yCoordinate;
int distanceleft = (y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1);
distanceleft = sqrt(distanceleft);
printf("Distance left to cover is %d\n",distanceleft);
int m = (y2 - y1)/(x2 - x1); // slope.
int b = y1 - m * x1; //y-intercept
//find point on the line that is 10 units away from
//current coordinates on equation …Run Code Online (Sandbox Code Playgroud) 所以,我正在使用一个文本文件,我在其上对字符串进行以下操作
def string_operations(string):
1) lowercase
2) remove integers from string
3) remove symbols
4) stemming
Run Code Online (Sandbox Code Playgroud)
在此之后,我仍然留下如下字符串:
durham 28x23
Run Code Online (Sandbox Code Playgroud)
我看到了我的方法中的缺陷,但想知道是否有一种好的,快速的方法来识别字符串是否附加了数值.
所以在上面的例子中,我想要输出
durham
Run Code Online (Sandbox Code Playgroud)
另一个例子:
21st ammendment
Run Code Online (Sandbox Code Playgroud)
应该给:
ammendment
Run Code Online (Sandbox Code Playgroud)
那么我该如何处理这些东西呢?
在所有示例中,a是:
>>> def a():
... print "aaaaaaa"
Run Code Online (Sandbox Code Playgroud)
我从另一段代码传递了一个函数对象,我需要执行该函数.
当我说"功能对象"时,我的意思是这样一个对象:
>>> type(a)
<type 'function'>
Run Code Online (Sandbox Code Playgroud)
看看这个:
>>> def function(f):
... print "start"
... f
... print "end"
>>> function(a)
start
end
Run Code Online (Sandbox Code Playgroud)
如果function()正在执行它传递的函数,则输出将aaaaaaa在其中间(从中返回a(),这是传递给它的函数)
那么如何在传递函数对象时执行函数呢?
(对不起,如果不是很清楚,我也会混淆自己......)