如何在python中执行字典的添加列表?

如何在python中执行字典的添加列表?,python,dictionary,Python,Dictionary,尝试执行此操作时,我遇到以下错误: prob1 += sum([x[d][s]*Dic_Trancosts[d][s] for (d,s) in routes]) #add the DC cost **TypeError**: string indices must be integers 你能告诉我怎么了吗 dcCap=[30, 30, 40, 70] dcCost=[700, 800, 1600, 800] customerDemand=[40, 30, 40] transcostlist

尝试执行此操作时,我遇到以下错误:

prob1 += sum([x[d][s]*Dic_Trancosts[d][s] for (d,s) in routes]) #add the DC cost
**TypeError**: string indices must be integers
你能告诉我怎么了吗

dcCap=[30, 30, 40, 70]
dcCost=[700, 800, 1600, 800]
customerDemand=[40, 30, 40]
transcostlist_of_lists=[[4, 16, 8, 4], 
[6, 6, 6, 9], 
[12, 15, 7, 8]]
DClist=['S1', 'S2', 'S3', 'S4']
Demandlist=['D1', 'D2', 'D3'] 
import pulp
from pulp import *
# The supply capacity data is made into a dictionary
Dic_DCCapacity = makeDict([DClist], dcCap,0) #default 0 if no data entry is 
found
Dic_DCCapacity
# The fixed cost of each DC is made into a dictionary
Dic_DCCost = makeDict([DClist], dcCost,0)
Dic_DCCost
# The demand data is made into a dictionary
Dic_Demand = makeDict([Demandlist], customerDemand,0)
Dic_Demand
# The cost data is made into a dictionary
Dic_Trancosts = makeDict([Demandlist,DClist], transcostlist_of_lists,0)
Dic_Trancosts

prob1 = LpProblem("Network Design Problem1", LpMinimize)
#Each pair of (Demandlist,DClist) is associated with a decision variable, 
#indicating the flow between DC and customer

#Decision variable (binary) that is associated with whether or not to open a DC  

routes = [(d,s) for d in Demandlist for s in DClist]
routes

prob1 += sum([x[d][s]*Dic_Trancosts[d][s] for (d,s) in routes]) \
#add the DC cost


# Supply maximum constraints are added to prob for each supply node (DC)

for s in DClist:
prob1 += sum([x[d][s] for d in Demandlist]) <= Dic_DCCapacity[s]

# Demand minimum constraints are added to prob for each demand node (bar)

# Write out the lp file to you directory
dcCap=[30,30,40,70]
dcCost=[7008001600800]
customerDemand=[40,30,40]
transcostlist_of_list=[[4,16,8,4],
[6, 6, 6, 9], 
[12, 15, 7, 8]]
DClist=['S1','S2','S3','S4']
Demandlist=['D1','D2','D3']
进口纸浆
从纸浆进口*
#供电能力数据被编入字典
Dic_DCCapacity=makeDict([DClist],dcCap,0)#如果未输入数据,则默认为0
建立
数据中心容量
#每个DC的固定成本被编入字典
Dic_DCCost=makeDict([DClist],DCCost,0)
Dic\DCU成本
#需求数据被编入字典
Dic_Demand=makeDict([Demandlist],客户需求,0)
Dic_需求
#成本数据被编入字典
Dic_Trancosts=makeDict([Demandlist,DClist],列表中的transcostlist,0)
Dic_Trancosts
问题1=LpProblem(“网络设计问题1”,LpMinimize)
#每对(Demandlist,DClist)与一个决策变量相关联,
#指示DC和客户之间的流量
#与是否打开DC关联的决策变量(二进制)
routes=[(d,s)用于需求列表中的d,用于DClist中的s]
路线
prob1+=总和([x[d][s]*Dic_Trancosts[d][s],用于路线中的(d,s)])\
#加上跟单信用证成本
#为每个供应节点(DC)将供应最大约束添加到prob中
对于DClist中的:

prob1+=sum([x[d][s]表示Demandlist中的d])没有示例就有点难说,但这个错误

TypeError: string indices must be integers
通常意味着您正在尝试使用intiger以外的内容索引字符串:

>>> s = "my string"
>>> s[1]
'y'
>>> s["y"]

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    s["y"]
TypeError: string indices must be integers, not str
>s=“我的字符串”
>>>s[1]
“是的”
>>>s[“y”]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
s[“y”]
TypeError:字符串索引必须是整数,而不是str
我最好的猜测是,
x
是一个字符串而不是一个字典,当您尝试使用
x[d][s]
对其进行索引时,
d
s
是字符串,Python会抛出一个错误


不确定您的代码中应该包含什么
x
,但我会开始查看那里是否有错误消息?预期的结果是什么?此外,如果减少它,示例将更容易理解。请阅读
x中的建议
在此代码中没有定义。