convert a string list to a list of integers

1
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 to a new list, but to integers? And after doing this, as I scroll through this list and set the maximum, minimum, and also average and median encounter, without using the max (), min (), mean (), median (), and count () / em>

    
asked by anonymous 22.05.2018 / 21:45

3 answers

5

First you need to convert all strings to integers;

First Solution (conventional)

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

Second solution (list comprehension)

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

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

media = sum(valores) / len(valores)

Median is half the sum of all values.

mediana = sum(valores) / 2

Maximum and minimum; use the sort function to sort the values, so the first value will always be the smallest and the last one the largest.

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 working on Ideone

    
22.05.2018 / 22:24
2

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

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

So you've created a new list with the same values but now all are integers.

About the highest and lowest values ... Without using max () and min () you will have to scroll through the entire list saving the largest and smallest value and go looking to see if there is another value bigger or smaller 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 mean you can get 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 -

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

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

x = [11, 14, 152, 987]
mediana = sum(x)/2
    
22.05.2018 / 22:09
1

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

That is, it is the middle element of the list whether 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]
    
22.05.2018 / 23:39