convert a string list into a list of integer numbers

Asked

Viewed 9,117 times

0

x = ['443' , '552' , '342' , '368' , '9867' , '335' , '9412' , '7436' , '1692' , '85' , '990' , '332' , '8816' , '4567' , '279' , '119' , '2290' , '234' , '9863' , '345' , '230' , '5574' , '230' , '5432']

How can I convert this list into a new list , but whole numbers? And after doing that, how do I go through this list and define what the maximum value , minimum, and also find average and median, without using the functions max(), min(), Mean(), Median() and Count().

  • 1

    And what have you already done? In which part are you doubtful?

3 answers

6


First you need to convert all the strings whole-wheat;

First Solution (conventional)

x = ['123', '456', '789']
valores = []
for val in x:
    valores.append(int(val))

Second Solution (comprehensilist on)

valores = [int(val) for val in x]

Mean is the sum of all values divided by the amount of values.

media = sum(valores) / len(valores)

Median is half the sum of all values.

mediana = sum(valores) / 2

Maximum and minimum; use the function sort to sort values, so the first value will always be the lowest and the last the highest.

valores.sort()
min = valores[0]
max = valores[-1]

Complete

x = ['123', '456', '789']
valores = [int(val) for val in x]

media = sum(valores) / len(valores)
print(media)

mediana = sum(valores) / 2
print(mediana)

valores.sort()
min = valores[0] # Primeiro valor da lista
print(min)

max = valores[-1] # Ultimo valor da lista
print(max)

See it working on Ideone

  • 2

    Just one caveat: the median is not half the sum of all the values. More details.

  • missed also you find the maximum and minimum value without using the functions.

  • 2

    @Cleomirsantos includes what was missing

  • to calculate the median if the number of elements is even or odd uses - if the formula n+1/2 , where n is the number of elements in the list, I read about it in wikipedia. I think it works both ways.

2

Let’s go in parts, first convert all string values to integer, let’s go through the entire list for this. Converting one by one all list elements that are string, example:

x = ['11', '14', '152', '987']
novo_x = []
for i in x:
    novo_x.append(int(i))

So you created a new list with the same values but now all are integer.

About the highest and lowest value... Without using max() and min() you will need to go through the entire list saving the highest and lowest value and go looking to see if you find another value higher or lower than the current one, example:

x = [11, 14, 152, 987]
Max = 0
for i in range(len(x)):
    if(x[i] > Max):
        Max = x[i]

output: 987

The average you can take the sum of all the values in the list using sum() divided by the total of items in the list using Len()... Getting:

x = [11, 14, 152, 987]
media = sum(x)/len(x)

-- Edit --

On the median... the colleague below, Peter corrected me:

The median is the value that separates the larger half and the smaller half of a sample, if this sample is even, the example I gave below applies, but if it is odd, follows the example that the colleague below gave.

x = [11, 14, 152, 987]
mediana = sum(x)/2

1

Only one thing about the accepted answer: the median, according to Wikipedia, is the value separating the larger half and the smaller half of a sample, population or probability distribution.

I mean, it’s the middle element of the list if the list has an odd number of elements, or the average of the two middle elements if the list has an even number of elements.

In code:

x = ['443' , '552' , '342' , '368' , '9867' , '335' , '9412' , '7436' , '1692' , '85' , '990' , '332' , '8816' , '4567' , '279' , '119' , '2290' , '234' , '9863' , '345' , '230' , '5574' , '230' , '5432']
valores = list(map(int, x))

if len(valores_ordenados) % 2 == 0:
    mediana = (valores_ordenados[len(valores_ordenados)//2] + valores_ordenados[len(valores_ordenados)//2 - 1])/2
else:
    mediana = valores_ordenados[len(valores_ordenados)//2]
  • Failed to include the largest and smallest number

  • 1

    @RFL this has already been answered in other answers; just wanted to complete what was missing/wrong in them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.