Reputation: 216
So I have this document called new.txt which contains many digits of pi (see https://www.piday.org/million/) and I want to split the number into a list made out of all these digits in order. My code works but it's extremely slow, (I have tried with a less digits of pi).
def sort(stuff):
for iter_num in range(len(stuff)-1,0,-1):
for idx in range(iter_num):
if stuff[idx]>stuff[idx+1]:
temp = stuff[idx]
stuff[idx] = stuff[idx+1]
stuff[idx+1] = temp
return stuff
a = []
with open("/Users/serax/desktop/new.txt", "r") as r:
for line in r.readlines():
for char in line:
text = a.append(char)
print(sort(a))
Upvotes: 0
Views: 66
Reputation: 216
thanks to the comments i have edited my code, here's the result.
thisdict = {
".": 0,
"0": 0,
"1": 0,
"2": 0,
"3": 0,
"4": 0,
"5": 0,
"6": 0,
"7": 0,
"8": 0,
"9": 0,
}
with open("/Users/serax/documents/new.txt", "r") as r:
for line in r.readlines():
# print(sorted(line)) # built in function that can sort str/list/tuple (works)
for char in line:
for key in thisdict:
if char == key:
thisdict[key] += 1
ordered = ""
for i in thisdict:
ordered = ordered + i*thisdict[i]
print(ordered)
Upvotes: 1