-1

I am using PulP to solve a linear programming problem. I am having the following error in the objective function:

     TypeError: list indices must be integers or slices, not str

My objective function is

    prob += lpSum([routes_vars][man][pa]*costs_distance[man][pa] for (man,pa) in routes)

according to the error message, I think my problem is the costs_distance dictionary that has string values

     costs_distance = {'201': {'10267': '167724.1407', '10272': '151859.5908', '10275': '150131.7254', '10277': '153266.1819', '10279': '147949.5275', '10281': '145429.9767', '10283': '144757.2507', '10286': '166474.849', '10288': '152733.6419'}, '2595': {'10267': '186216.5193', '10272': '170351.9694', '10275': '168624.1039', '10277': '171758.5604', '10279': '166441.906', '10281': '163922.3553', '10283': '163249.6293', '10286': '186363.4807', '10288': '171226.0204'},

How can I convert only the dictionary string values ('167724.1407', '151859.5908', '150131.7254'... ) into int values?

2 Answers2

0

Your issue has nothing to do with the costs_distance dictionary (otherwise the error message wouldn't mention a list). It's this part:

[routes_vars][man][pa]

I'm not sure what you expect this to return, but it first constructs a list with a single element (routes_vars) then tries to slice it using [man], which doesn't make any sense.

Selcuk
  • 57,004
  • 12
  • 102
  • 110
0

If sure you meant converting dictionary values to float and not int

list1 = costs_distance['201'].values()
list2 = list(map(float, list1))
Seyi Daniel
  • 2,259
  • 2
  • 8
  • 18