标题说全部.
我的代码:
try:
os.remove("NumPyResults.txt")
except IOError:
with open("NumPyResults.txt", 'a') as results_file:
outfile = csv.writer(results_file)
outfile.writerow(.......)
Run Code Online (Sandbox Code Playgroud)
它附加的原因是因为它处于一个函数中并被多次调用.因此,每次运行程序时,我都想要一个新文件,删除旧文件并编写新文件.
但是,这不会创建新文件.我还在我运行的目录中创建了该文件,它也没有删除它.
我明白了
WindowsError: [Error 2] The system cannot find the file specified: 'NumPyResults.txt'
Run Code Online (Sandbox Code Playgroud) 面试官问我这个问题.如何为特定值搜索大型数组(数千或数百万个值).
我建议二进制搜索对项目进行排序并且数组大小较小的情况.如果你想要一个大数组中的最大值(值是1次传递后最右边的值),我还建议对冒泡排序算法进行1次迭代.
但我不确定哪些算法可以在固有的随机分类数组索引中提取值.
我正在尝试阅读此文本文件。
A B C D
1 5 6 7
2 8 9 10
3 .......
4 .......
Run Code Online (Sandbox Code Playgroud)
该字母作为一行引入,然后我将所有值作为浮点数引入
with open('file.txt', 'r') as f:
headings = f.readline()
numbers = [float(n) for n in f.read().split()] #read values to the 'numbers' as a list
print numbers
Run Code Online (Sandbox Code Playgroud)
所以我有一长串所有整数。
但我想要这种格式的字典:
my_dict( { 1: [5,6,7], 2:[8,9,10] } )
Run Code Online (Sandbox Code Playgroud)
所以文件编号的第一列是键,其余的是与它们各自的键相关的列表。
我将每 4 个值设置为带有循环的键,但是如何轻松地将其余值作为列表放入相应的键中。
我有一个项目需要运行,不知道如何实现自定义异常.它主要做复杂的科学功能,含糊不清.
如果没有设置某些内容,通常会引发异常.我已经把它作为runnables的一个开始例子.
# Define a class inherit from an exception type
class CustomError(Exception):
def __init__(self, arg):
# Set some exception infomation
self.msg = arg
try:
# Raise an exception with argument
raise CustomError('This is a CustomError')
except CustomError, arg:
# Catch the custom exception
print 'Error: ', arg.msg
Run Code Online (Sandbox Code Playgroud)
我不知道这是如何工作的,或者我是如何实现我的代码的.它不是很明确.
了解需要创建的基本异常.
在一个功能:
if self.humidities is None:
print "ERROR: Humidities have not been set..."
return
Run Code Online (Sandbox Code Playgroud)
显然,这需要引发/抛出异常.
我有一个数据结构
my_list = [ [a, b], [c,d], [e,f], [g,h], [i, j], [k, l] ....]
Run Code Online (Sandbox Code Playgroud)
字母是浮点数.
我需要找到c,e和a之间的比率>>>> c/a ... e/a然后找到d,f和b之间的比率>>>> d/b,f/b并继续列表中的所有元素12个元素.所以计算了8个比率.
是否有一个函数可以有效地执行此操作,因为我们在列表元素之间进行操作?无需先单独提取数组中的数据,然后进行数学计算.