How to read a list of 1000 elements from a txt file in python and then sort it with Quicksort

1
def leeLista():
    arreglo=[]
    a=int(raw_input("Ingresa el tamano del arreglo: "))


    for i in range(0,a):
        arreglo.append(int(raw_input("Ingresa los valores %d : " % i)))
        #en lugar de ingresarlos los quiero extraer de un archivo txt 
    pos=int(raw_input("Selecciona el pivote: "))
    return arreglo
    
asked by Agon 03.05.2017 в 20:18
source

4 answers

0

As Jorge Arturo says in his commentary, it depends on the limiters that you have in your txt file. Assuming that each value is separated by a line break in your file then it would be something like the following.

arch = open('archivo.txt', 'r')

for linea in arch:
    arreglo.append(int(linea))

arch.close()

In order for this to be applicable, all lines in your file must be integer values too.

    
answered by 03.05.2017 / 20:36
source
0

You open the file with open then read the text with read() and you convert your string in a list with split() , you can add your separator within split:

file = open(“testfile.text”, “r”) 
text = file.read() 
items = text.split(",")

in my example the text would be something like "a, b, c, d" and split convert it to ["a", "b", "c", "d"]

    
answered by 03.05.2017 в 20:34
0

How is the file format? I'm going to assume it's a flat file with a value per line and what you're looking for is to load this into a list.

def read_file(file):
    """Lee un archivo de texto y devuelve una lista con sus lineas"""
    lista = []
    with open(file) as procfile:
      for line in procfile:
        lista.append(line.strip())
    return lista

Now if the file has a single line with values separated by ',' you can do this:

def read_file(file):
    """Lee un archivo de texto y devuelve una lista"""
    with open(file) as procfile:
      line = procfile.readline()
      return [i for i in line.split(',')]
    
answered by 03.05.2017 в 21:00
0
def quicksort(arreglo,izq,der):
    i=izq
    j=der
    piv=arreglo[(izq + der)/2]

    while( i <= j ):
        while arreglo[i]<piv and j<=der:
            i=i+1
        while piv<arreglo[j] and j>izq:
            j=j-1
        if i<=j:
            aux = arreglo[i]; 
            arreglo[i] = arreglo[j]; 
            arreglo[j] = aux;
            i=i+1;  
            j=j-1;

        if izq < j:
          quicksort( arreglo, izq, j );
    if i < der:
        quicksort( arreglo, i, der );

def imprimeLista(arreglo,tam):
    for i in range(0,tam): print arreglo[i],



def leeLista():
    arreglo=[]
    a=int(raw_input("Ingresa el tamano del arreglo: "))

    #for i in range(0,a):
     #   arreglo.append(int(raw_input("Ingresa los valores %d : " % i)))

    arch = open('or.txt', 'r')

    for linea in arch:
        arreglo.append(linea.strip())

    arch.close()    

    pos=int(raw_input("Selecciona el pivote: "))
    return arreglo

A=leeLista()
tiempo_inicial = time()
quicksort(A,0,len(A)-1)
tiempo_final = time()
tiempo_ejecucion = tiempo_final - tiempo_inicial
print'El tiempo de ejecucion fue ::', tiempo_ejecucion
imprimeLista(A,len(A))

This is my code but it does not do the ordering just print the arrangement as it is

Result:

El tiempo de ejecucion fue :: 2.69412994385e-05
2,24,3,26,21,1, 4, 5, 29,6, 7, 8, 9, 10, 12, 13, 14, 15, 30,16, 17, 18, 19, 20, 22, 23, 25,  27, 28,11
    
answered by 04.05.2017 в 03:07