Note that there are some explanatory texts on larger screens.

plurals
  1. POCode: Code isn't working to sort through a list of 1 million integers, printing top 10
    primarykey
    data
    text
    <p>This is for homework, so I must try to use as little python functions as possible, but still allow for a computer to process a list of 1 million numbers efficiently.</p> <pre><code>#!/usr/bin/python3 #Find the 10 largest integers #Don't store the whole list import sys import heapq def fOpen(fname): try: fd = open(fname,"r") except: print("Couldn't open file.") sys.exit(0) all = fd.read().splitlines() fd.close() return all words = fOpen(sys.argv[1]) numbs = map(int,words) print(heapq.nlargest(10,numbs)) li=[] count = 1 #Make the list for x in words: li.append(int(x)) count += 1 if len(li) == 10: break #Selection sort, largest-to-smallest for each in range(0,len(li)-1): pos = each for x in range(each+1,10): if li[x] &gt; li[pos]: pos = x if pos != each: li[each],li[pos] = li[pos],li[each] for each in words: print(li) each = int(each) if each &gt; li[9]: for x in range(0,9): pos = x if each &gt; li[x]: li[x] = each for i in range(x+1,10): li[pos],li[i] = li[i],li[pos] break #Selection sort, largest-to-smallest for each in range(0,len(li)-1): pos = each for x in range(each+1,10): if li[x] &gt; li[pos]: pos = x if pos != each: li[each],li[pos] = li[pos],li[each] print(li) </code></pre> <p>The code is working ALMOST the way that I want it to. I tried to create a list from the first 10 digits. Sort them, so that it in descending order. And then have python ONLY check the list, if the digits are larger than the smaller one (instead of reading through the list 10(len(x)).</p> <p>This is the output I should be getting:</p> <pre><code>&gt;&gt;&gt;[9932, 9885, 9779, 9689, 9682, 9600, 9590, 9449, 9366, 9081] </code></pre> <p>This is the output I am getting:</p> <pre><code>&gt;&gt;&gt;[9932, 9689, 9885, 9779, 9682, 9025, 9600, 8949, 8612, 8575] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload