How to swap maximums with the minimums? (python)
Asked Answered
E

4

-2

Is there a method to swap the maximum and the minimum of a list?

The list will be as follows and the program has to be continued so that it will print the maximum swapped with the minimum, the second maximum swapped with the second minimum and third maximum swapped with the third minimum.

Eg. Enter input- 0 1 2 3 4 5 6 7 8 9 -1 Output- 9873456210

a = []
nums = raw_input("Enter input- ")
for n in nums.split():
    n = int(n)
    if n < 0:
        break
    a.append(n)
if len(a)<7:
    print "please enter more than 7 integers"
Ensilage answered 4/6, 2014 at 14:21 Comment(3)
I guess it is your homework. You managed to ask for list of numbers, but what was your attempt to try any method for solving actual task for switching the values? Do not aim first at the best, show us something.Disputable
just a.reverse() <- only if you already have minimum->maximum listLumpkin
@Streak Not really. Reverse turns the list upside down, the question is about swapping just minimal and maximal value in the list.Disputable
M
1

There is no method for this in python. You can try using primitive methods to build what you want using lists.

This code does the job:

#!/usr/bin/python
a = []
b = []
nums = raw_input("Enter input- ")
#append all to a list
for n in nums.split():
    n = int(n)
    if n < 0:
        break
    a.append(n)

#get the maximums
b = list(a)
first_max = max(b)
b.remove(first_max)
second_max = max(b)
b.remove(second_max)
third_max = max(b)

#get the minimums
b = list(a)
first_min = min(b)
b.remove(first_min)
second_min = min(b)
b.remove(second_min)
third_min = min(b)

## now swap 
xMax, yMax, zMax = a.index(first_max), a.index(second_max), a.index(third_max)
xMin, yMin, zMin = a.index(first_min), a.index(second_min), a.index(third_min)
a[xMax], a[xMin] = a[xMin], a[xMax]
a[yMax], a[yMin] = a[yMin], a[yMax]
a[zMax], a[zMin] = a[zMin], a[zMax]

print a
Meritorious answered 4/6, 2014 at 14:57 Comment(0)
L
1

I assume the list contains no duplicate values. Then you can construct a new sorted list to find the lowest and highest numbers.

After that you take the corresponding values from the sorted list, find their index in the original list and swap them.

data = list(range(10))
helper = sorted(data)
for i in range(3):
    low_value = helper[i]
    high_value = helper[-(i+1)]
    low_index = data.index(low_value)
    high_index = data.index(high_value)
    print(low_index, high_index)
    data[low_index], data[high_index] = data[high_index], data[low_index]
print(data)
Likker answered 4/6, 2014 at 15:50 Comment(0)
A
0

For a simple approach I created the following:

#Initial setup:
z = [10, 8, 20, 2]
u = [-x for x in z]
z_copy = [x for x in z]
weights_reversed = [None] * len(u)

#algorithm
for i in range(len(u)):
    index_F = u.index(max(u))
    u[index_F] = -10^80 # to remove that value from consideration
    index_Z = z.copy.index(max(z_copy))
    max_wt = z_copy.pop(index_Z)
    weights_reversed[index_F] = max_wt

The result of the above creates a list [8, 10, 2, 20]

Audiometer answered 14/2, 2022 at 19:17 Comment(0)
G
-3

Check this out.

>>> sorted(student_tuples, key=itemgetter(2), reverse=True)
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
Giule answered 4/6, 2014 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.