Reputation: 13
So I'm trying to run a matrix through Dijkstra's algorithm. I have a 2d list comprised of values or inf, I want to scan through the first list and find the smallest value. in the case below 5.
lst = [['inf', '10', 'inf', '5', 'inf'],
['inf', 'inf', '1', '2', 'inf'],
['inf', 'inf', 'inf', 'inf', '4'],
['inf', '3', '9', 'inf', '2'],
['7', 'inf', '4', 'inf', 'inf']]
I know I need to use some version of lst[0:] to specify that list but not really sure exactly how to implement it.
Upvotes: 0
Views: 33
Reputation: 60954
Note that your list of lists contains strings. You likely want to treat the values as floats
lst = [['inf', '10', 'inf', '5', 'inf'],
['inf', 'inf', '1', '2', 'inf'],
['inf', 'inf', 'inf', 'inf', '4'],
['inf', '3', '9', 'inf', '2'],
['7', 'inf', '4', 'inf', 'inf']]
print(min(float(x) for x in lst[0]))
# 5.0 min of first list
print(min(float(x) for y in lst for x in y))
# 1.0 min of all lists
The naive lexicographic minimum of lst[0]
is '10'
Upvotes: 0
Reputation: 249123
lst[0]
is the first element of lst
which is the first sublist.
min(lst[0])
is the smallest value in the first sublist.
Upvotes: 2