小编mar*_*ion的帖子

从另一个子调用类型变量

嗨,我有一系列子程序如下:

  1. DataCollection():从电子表格中收集数据并将其写入自定义类型变量.

  2. NewSub():做其他事情,但与问题无关.

我想保留先前声明的相同变量,并在第二个子中分配值.我想我必须以某种方式使它们成为全局变量,但到目前为止无法解决它,无论我做什么,我都得到变量未定义的错误.我的代码如下:

Option Explicit

Public Type Trucks
    NumberOfAxles As Integer
    AxleWeights(15) As Double
End Type

Public Sub DataCollection()

Dim NumberOfTrucks As Integer
Truck(10) As Trucks
Dim i, j, k As Integer

'Determine Number of Trucks
NumberOfTrucks = Cells(6, 8)

'Populate Truck Arrays (Trucks 1 to 5)

k = 0
For i = 1 To 5
    Truck(i).NumberOfAxles = Cells(9, 4 + 4 * k)
    k = k + 1
Next i

k = 0
For i = 1 To …
Run Code Online (Sandbox Code Playgroud)

excel vba global-variables excel-vba subroutine

3
推荐指数
1
解决办法
4万
查看次数

python中的列表赋值行为

来自其他语言,我对列表中Python变量赋值的方式有点困惑.举个例子,让我们说:

x = [4, 5, 2, 70, 1]
y = x
y.sort()
Run Code Online (Sandbox Code Playgroud)

如果打印x和y,则两个变量的结果相同:

x = [1, 2, 4, 5, 70]
y = [1, 2, 4, 5, 70]
Run Code Online (Sandbox Code Playgroud)

我没想到这种行为.我认为x的序列不会改变,因为我只在列表y上应用了sort方法.

另一方面,如果我使用切片运算符将列表x的内容分配给列表y,那么我将达到预期的(至少在我的情况下)行为:

x = [4, 5, 2, 70, 1]
y = x[:]
y.sort()
Run Code Online (Sandbox Code Playgroud)

如果打印x和y,我看到列表x保持不变.

x = [4, 5, 2, 70, 1]
y = [1, 2, 4, 5, 70]
Run Code Online (Sandbox Code Playgroud)

有人可以解释背后的逻辑吗?

谢谢!

python sorting list variable-assignment

3
推荐指数
1
解决办法
163
查看次数

函数作为函数的参数

我在Python书中看到了这个例子,它展示了如何使用函数作为另一个函数的参数:

def diff2(f, x, h=1E-6):
    r = (f(x-h) - 2*f(x) + f(x+h))/float(h*h)
    return r

def g(t):
    return t**(-6)

t = 1.2
d2g = diff2(g, t)
print d2g
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果没有为函数g提供参数,这个脚本如何工作?有问题的一行是:

d2g = diff2(g,t)
Run Code Online (Sandbox Code Playgroud)

不应该这样做:

d2g = diff2(g(t), t)
Run Code Online (Sandbox Code Playgroud)

python arguments function

3
推荐指数
1
解决办法
111
查看次数

Python中具有不同长度的列表列表中的元素总和

我试图计算列表列表中的元素总和.如果主列表中的列表都具有相同的大小,我可以毫不费力地计算总和,如下所示:

a = [[4], [8], [15]]
total = [sum(i) for i in zip(*a)]
Run Code Online (Sandbox Code Playgroud)

结果:

total = [27]     #(4 + 8 + 15) = 27, GOOD!!!
Run Code Online (Sandbox Code Playgroud)

但是我可能在主列表中有一个不同大小的列表,例如:

a = [[3], [4, 6], [10]]
Run Code Online (Sandbox Code Playgroud)

预期结果:

total = [17, 19]     #(3 + 4 + 10) = 17, (3 + 6 + 10) = 19
Run Code Online (Sandbox Code Playgroud)

我被困在这里,显然我的相同大小的列表的解决方案不起作用.以我定义的方式获得结果的最佳方法是什么?我的直觉是找出具有最大长度的列表,然后通过添加零将其他列表扩展到该长度,最后分别计算总和.这听起来像一个丑陋的解决方案,我想知道是否有一个快速,更优雅的方式来做到这一点.

谢谢!

编辑:应该更好地解释它.我也有点困惑......以下是更好的例子:

列表中列表中的元素数量a永远不会超过2个.示例:

a = [[1], [10], [5]] #Expected result: [16] (1+10+5)

a = [[1, 10], [3], [4]] #Expected result: [8, 17] (1+3+4, 10+3+4)

a = …
Run Code Online (Sandbox Code Playgroud)

python list

3
推荐指数
1
解决办法
179
查看次数

字典在Python中使用而不是动态变量名

我有一个很长的文本文件,有卡车配置.在每一行中,卡车的一些属性被列为字符串.每个属性在字符串中都有自己的固定宽度空间,例如:

2 chracters = number of axles
2 characters = weight of the first axle
2 characters = weight of the second axle
...
2 characters = weight of the last axle
2 characters = length of the first axle spacing (spacing means distance between axles)
2 characters = length of the second axle spacing
...
2 characters = length of the last axle spacing
Run Code Online (Sandbox Code Playgroud)

举个例子:

031028331004
Run Code Online (Sandbox Code Playgroud)

指:

number of axles = 3
first axle weight = 10
second axle …
Run Code Online (Sandbox Code Playgroud)

python variables dictionary list dynamic-variables

2
推荐指数
1
解决办法
1606
查看次数

Python内置的sorted()函数

我在书中看到如何将特定的排序函数传递给Python自己的内置sorted()函数,如下所示:

def mysort(a, b):
    if a[3] < b[3]:
        return -1
    elif a[3] > b[3]:
        return 1
    else:
        return 0

data = [
('Alpha Centauri A', 4.3, 0.26, 1.56),
('Alpha Centauri B', 4.3, 0.077, 0.45),
('Alpha Centauri C', 4.2, 0.00001, 0.00006),
("Barnard's Star", 6.0, 0.00004, 0.0005),
('Wolf 359', 7.7, 0.000001, 0.00002),
('BD +36 degrees 2147', 8.2, 0.0003, 0.006),
('Luyten 726-8 A', 8.4, 0.000003, 0.00006),
('Luyten 726-8 B', 8.4, 0.000002, 0.00004),
('Sirius A', 8.6, 1.00, 23.6),
('Sirius B', 8.6, 0.001, 0.003),
('Ross …
Run Code Online (Sandbox Code Playgroud)

python sorting arguments function

0
推荐指数
1
解决办法
763
查看次数

Numba优化功能无法提高速度

我写了一些代码,计算出300英尺长的桥梁在不同卡车上施加的弯矩。卡车数据包含在两个列表中:ax_listsp_list,分别是轴重和轴距。

代码没有太多内容,但是,这需要针对数百万种不同类型的卡车进行重复,并且我正在尝试优化我的代码,这在考虑实际数据大小集时会花费很长时间。

我尝试使用Numba来查看是否可以提高速度,但是无论是否@jit为每个函数添加Numba 装饰器,它都不会改变执行时间。我在这里做错了什么?任何帮助都将受到欢迎!我还包括以下代码,可为1000条记录生成代表性的伪数据:

import random
from numba import jit
import numpy as np
from __future__ import division

#Generate Random Data Set

ax_list=[]
sp_list=[]

for i in xrange(1000):
    n = random.randint(3,10)
    ax = []
    sp = [0]
    for i in xrange(n):
        a = round(random.uniform(8,32),1)
        ax.append(a)
    for i in xrange(n-1):
        s = round(random.uniform(4,30), 1)
        sp.append(s)
    ax_list.append(ax)
    sp_list.append(sp)

#Input Parameters
L=300
step_size=4
cstep_size=4
moment_list=[]

@jit
#Simple moment function
def Moment(x):
    if x<L/2.0:
        return 0.5*x
    else:
        return …
Run Code Online (Sandbox Code Playgroud)

python optimization performance numpy numba

0
推荐指数
1
解决办法
370
查看次数