-3

I have a nested list which looks like this.

[[0.0, 1.4142135623730951, 2.8284271247461903, 2.23606797749979],
 [1.4142135623730951, 0.0, 1.4142135623730951, 1.0],
 [2.8284271247461903, 1.4142135623730951, 0.0, 1.0],
 [2.23606797749979, 1.0, 1.0, 0.0]]

I want to find the minimum element in every sub list. Thanks for the help!

8
  • 2
    Well, what have you tried?
    – pstatix
    Dec 17, 2017 at 3:22
  • Honestly I tried googling this answer before before posting. I am not sure why people are giving negative feedback to the question. Did I do something wrong? Dec 17, 2017 at 3:36
  • @AshleyLarson Surely you have tried something?
    – RoadRunner
    Dec 17, 2017 at 3:40
  • 1
    All your numbers are square roots of integers, the main diagonal is all zeros, and the matrix is symmetric. Why? What problem are you really solving? XY Problem. Dec 17, 2017 at 3:53
  • I am trying to solve the travelling salesman problem. The nested list is the distance matrix for 4 different cities. So choosing zeros would not make sense. So I replaced all the zeros by a very high number and now the next problem I am stuck is about not repeating the indexes. Dec 17, 2017 at 4:00

2 Answers 2

2

Well because others are already posting answers, you can store the minimum value of each sublist in a list using what is called list comprehension like so:

new_s = [min(x) for x in s]

Python has a built-in min() function that takes an iterable (i.e. one of your sublists) and finds the minimum value. By using list comprehension you build a list of those values. It can be read as:

"A list of minimum values for each x (sublist) in s (parent list)"

Edit: For commented use:

new_s = [sorted(x)[1] for x in s]

Can be read as:

"A list of the 2nd element in the sorted array of x for each x (sublist) in s (parent list)"

6
  • If I need not the lowest element but the second lowest element. I tried something like new_s=[min(x) for x in s if min(x)>0] but it gave the same answer. Dec 17, 2017 at 3:48
  • @AshleyLarson Please see my edit. However, you really should have updated your question if your true goal was to get the 2nd to lowest, not the lowest.
    – pstatix
    Dec 17, 2017 at 22:58
  • I replaced all the zeros with a very large number and used your method then. It worked fine. Dec 17, 2017 at 23:28
  • @AshleyLarson But do you understand what has happened. That is more important than "it worked fine"
    – pstatix
    Dec 17, 2017 at 23:34
  • Yep you created a sorted list and then chose the element with index 1. Thanks for the answer! Dec 19, 2017 at 0:19
2

You can use map, which is slightly more efficient than list comprehension when utilizing a builtin function, in this case min:

s = [[0.0, 1.4142135623730951, 2.8284271247461903, 2.23606797749979],
[1.4142135623730951, 0.0, 1.4142135623730951, 1.0],
[2.8284271247461903, 1.4142135623730951, 0.0, 1.0],
[2.23606797749979, 1.0, 1.0, 0.0]]
new_s = list(map(min, s))

Output:

[0.0, 0.0, 0.0, 0.0]

An alternative list comprehension as @pstatix mentioned:

new_s = [min(i) for i in s]
5
  • That does not output that. In fact, new_s will store a map object. You have to convert the map to a list by setting new_s = list(map(min, s)).
    – pstatix
    Dec 17, 2017 at 3:24
  • 2
    Better yet is list comprehension: new_s = [min(x) for x in s]
    – pstatix
    Dec 17, 2017 at 3:24
  • It's self explanatory, isn't it?
    – P.hunter
    Dec 17, 2017 at 3:42
  • 2
    For something as simple as this, both are pythonic solutions.
    – RoadRunner
    Dec 17, 2017 at 3:45
  • @StefanPochmann one would declare an empty list, and then followed by a for loop and a operation to append to that list with min() or do the stuff within the [] which increases the readability, I'd choose latter ,for you i don't know.
    – P.hunter
    Dec 17, 2017 at 3:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.