简体   繁体   English

SolverStudio如何在for循环(PuLP)中引用2D列表中的1列

[英]SolverStudio how to reference 1 column in a 2D list in a for loop(PuLP)

I have 2 data sets x1 and x2. 我有2个数据集x1和x2。 I want to be able to get a total sum of all the products of x1 and x2 only in the rows where the From column has Auckland in it. 我希望能够仅在“发件人”列中包含“奥克兰”的行中获得x1和x2的所有乘积的总和。 see here 看这里

The final answer should be (5*1) + (2*1) + (3*1) + (4*1) or 14. The PuLP code that I wrote to do this is given below 最终答案应该是(5 * 1)+(2 * 1)+(3 * 1)+(4 * 1)或14。下面给出了我为此编写的PuLP代码

# Import PuLP modeller functions
from pulp import *

varFinal = sum([x1[a] * x2[a] for a in Arcs if a == Nodes[0]])

print Nodes[0]
print Arcs[0]

Final = varFinal

The output that gets printed to the console is 打印到控制台的输出是

Auckland 奥克兰市
('Auckland', 'Albany') (“奥克兰”,“阿尔巴尼”)

I realise that my final value is zero because Arcs[some number] does not equal Nodes[some number]. 我意识到我的最终值为零,因为Arcs [some number]不等于Nodes [some number]。 Is there anyway to change the code so my final value is 14? 无论如何,有没有更改代码,所以我的最终值为14?

Any help is appreciated. 任何帮助表示赞赏。

Welcome to stack overflow! 欢迎堆栈溢出! Cause you've only posted part of your code, I have to guess at what data-types you're using. 因为您只发布了部分代码,所以我不得不猜测您正在使用哪种数据类型。 From the output, I'm guessing your Nodes are strings, and your Arcs are tuples of strings. 从输出中,我猜您的Nodes是字符串,而您的Arcs是字符串的元组。

Your attempt is very close, you want the from column to have Auckland in it. 您的尝试非常接近,您希望from列中包含Auckland。 You can index into a tuple the same way you would into an array, so you want to do: a[0] == Nodes[0] . 您可以像在数组中一样将其索引到元组中,因此您需要这样做: a[0] == Nodes[0]

Below is a self-contained example with the first bit of your data in which outputs the following (note that I've changed to python 3.x print statements (with parentheses)): 以下是一个独立的示例,其中数据的第一位输出以下内容(请注意,我已更改为python 3.x打印语句(带括号)):

Output: 输出:

Auckland
('Auckland', 'Albany')
14

Code: 码:

# Import PuLP modeller functions
from pulp import *

# Data
Nodes = ['Auckland',
        'Wellington',
        'Hamilton',
        'Kansas City',
        'Christchuch',
        'Albany',
        'Whangarei',
        'Rotorua',
        'New Plymouth']

Arcs = [('Auckland','Albany'),
        ('Auckland','Hamilton'),
        ('Auckland','Kansas City'),
        ('Auckland','Christchuch'),
        ('Wellington','Hamilton'),
        ('Hamilton','Albany'),
        ('Kansas City','Whangarei'),
        ('Christchuch','Rotorua')]

x1_vals = [1, 2, 3, 4, 5, 9, 11, 13]
x2_vals = [5, 1, 1, 1, 1, 1, 1, 1]

x1 = dict((Arcs[i], x1_vals[i]) for i in range(len(Arcs)))
x2 = dict((Arcs[i], x2_vals[i]) for i in range(len(Arcs)))

varFinal = sum([x1[a] * x2[a] for a in Arcs if a[0] == Nodes[0]])

print(Nodes[0])
print(Arcs[0])
print(varFinal)

For future reference, answers are most likely to be forthcoming if you include code which others can try to run (without external data dependencies), that way people can try to run it, fix it, and re-post it. 供您将来参考,如果您包含其他人可以尝试运行的代码(没有外部数据依赖项),那么人们很可能会给出答案,那样人们就可以尝试运行它,对其进行修复并重新发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM