-6

I would like to find the largest element in a list and zero out the rest of the elements. I am not looking for the index of the element, just what the example shows:

So I would go from:

Example:

a = [1,2,20,5,99,70,35]

To

b = [0,0,0,0,1,0,0]

However, what if I had a list of lists, and I want to go from:

c = [ [33,6,3],[10,50,20],[4,9,77] ]

To

d = [ [1,0,0],[0,1,0],[0,0,1] ]



new_a = [] #create a new empty list
a = [ [33,6,3],[10,50,20],[4,9,77] ] # the list I will loop through
for list in a: # this will go through the lists inside the list "a"
    biggest = max(list) # this will find the max elements in lists
    new_a.append([1 if i==biggest else 0 for i in list]) #append 
print(new_a)

Answer: [ [1,0,0],[0,1,0],[0,0,1] ]

Sam
  • 7,252
  • 16
  • 46
  • 65
Chuck
  • 59
  • 3
  • 8
  • you want to find the index of the largest element? – Ryan Jul 19 '16 at 16:02
  • Possible duplicate of [Pythonic way to find maximum value and its index in a list?](http://stackoverflow.com/questions/6193498/pythonic-way-to-find-maximum-value-and-its-index-in-a-list) – pat Jul 19 '16 at 16:03
  • 2
    Why [0,0,1]? 1 isn't the larget element in the list. Shouldn't it be [0,0,3]? – Kevin Jul 19 '16 at 16:03
  • @Kevin yes, but he meant the index of the largest element. the third element is the largest and it's at index 2 – Ryan Jul 19 '16 at 16:06
  • this is a different question then the one above – EoinS Jul 19 '16 at 16:08
  • 5
    Why don't you *try writing some code?* – jonrsharpe Jul 19 '16 at 16:10
  • So what is it you need? You first talk about converting some data, which is fine since it's clear and focused what you want; but then you go into another issue (nested lists) and provide a code snippet without explaining what's wrong with it. Is the second part of your question about nested lists even necessary? – Kyll Jul 21 '16 at 08:59

2 Answers2

1

One thing that might be a problem is if the list has more than one largest element. But here is my solution:

>>> a = [1, 2, 3]
>>> b = [0] * len(a)
>>> b[a.index(max(a))] = 1
>>> b
[0, 0, 1]

Note that if there are more than one largest element, then the "1" will replace the first one. For example:

a = [3, 1, 2, 3]
b = [1, 0, 0, 0]
Neil
  • 1,275
  • 11
  • 25
1
b = max(a)
[1 if x==b else 0 for x in a]

max(a) will return the maximum element in your list

so you want to essentially replace all non-max values with 0 and all max values to 1.

You can do this several ways but the above seems most to the point.

Basically this is short hand for looping through each element, a different way to write this out, replacing elements in a would be:

b=max(a)
for n, i in enumerate(a):
  if i == b:
    a[n]=1
  else:
    a[n]=0

For Example2, just do the same thing for each list in c:

[([1 if x ==max(i) else 0 for x in i] for i in c]
EoinS
  • 5,405
  • 1
  • 19
  • 32
  • 3
    I think this is *O(n^2)* – vaultah Jul 19 '16 at 16:19
  • 1
    @vaultah Yes it is.... because it'll be evaluating max(a) again and again. So to avoid it just place it outside the loop. – hashcode55 Jul 19 '16 at 16:21
  • Thank you for the answer. My apologies for not being clear. – Chuck Jul 20 '16 at 17:20
  • @Chuck i think your initial question was clear enough. I think you caught a lot of flack for it, which wasn't totally warranted. You are supposed to try a solution, and post your attempt, even if it was a very poor attempt. The above answer solves your first example, please choose it if you agree. You can ask another question for example 2, try to adapt my solution to it, and that shoudl be better received – EoinS Jul 20 '16 at 17:25
  • @EoinS, Thank you for your patience and helping words. I appreciate the kindness and help. May the Gods smile upon you where ever you are. – Chuck Jul 20 '16 at 18:00