I tried looping through a .txt file that has 101 subarrays in a single array but that didn’t work because all characters are strings meaning it is a 0d array. I have no idea how to overcome this, because this is the first I am dealing with .txt files. How do i convert it to the type I want it to be, and be able to loop through the numbers in the file as normal?
import numpy as np import matplotlib.pyplot as plt import time #Opening file with open("1a_shuffled.txt", "r") as file: kfc = file.read() #placing file content in np.array array array = np.array(kfc) #function for insertion sort def insertion_sort(list): start = time.time() for sublist in list: for i in range(1,len(sublist)): currentElement = sublist[i] currentPosition = i - 1 while currentPosition >=0 and sublist[currentPosition] > currentElement: sublist[currentPosition + 1] = sublist[currentPosition] currentPosition = currentPosition -1 sublist[currentPosition + 1] = currentElement end = time.time() print(end - start) #calling the function and printing it insertion_sort(array) print(array) import numpy as np import matplotlib.pyplot as plt import time #Opening file with open("1a_shuffled.txt", "r") as file: kfc = file.read() #placing file content in np.array array array = np.array(kfc) #function for insertion sort def insertion_sort(list): start = time.time() for sublist in list: for i in range(1,len(sublist)): currentElement = sublist[i] currentPosition = i - 1 while currentPosition >=0 and sublist[currentPosition] > currentElement: sublist[currentPosition + 1] = sublist[currentPosition] currentPosition = currentPosition -1 sublist[currentPosition + 1] = currentElement end = time.time() print(end - start) #calling the function and printing it insertion_sort(array) print(array)
submitted by /u/Living_Cartoonist791
[link] [comments]
r/learnpython I tried looping through a .txt file that has 101 subarrays in a single array but that didn’t work because all characters are strings meaning it is a 0d array. I have no idea how to overcome this, because this is the first I am dealing with .txt files. How do i convert it to the type I want it to be, and be able to loop through the numbers in the file as normal? import numpy as np import matplotlib.pyplot as plt import time #Opening file with open(“1a_shuffled.txt”, “r”) as file: kfc = file.read() #placing file content in np.array array array = np.array(kfc) #function for insertion sort def insertion_sort(list): start = time.time() for sublist in list: for i in range(1,len(sublist)): currentElement = sublist[i] currentPosition = i – 1 while currentPosition >=0 and sublist[currentPosition] > currentElement: sublist[currentPosition + 1] = sublist[currentPosition] currentPosition = currentPosition -1 sublist[currentPosition + 1] = currentElement end = time.time() print(end – start) #calling the function and printing it insertion_sort(array) print(array) import numpy as np import matplotlib.pyplot as plt import time #Opening file with open(“1a_shuffled.txt”, “r”) as file: kfc = file.read() #placing file content in np.array array array = np.array(kfc) #function for insertion sort def insertion_sort(list): start = time.time() for sublist in list: for i in range(1,len(sublist)): currentElement = sublist[i] currentPosition = i – 1 while currentPosition >=0 and sublist[currentPosition] > currentElement: sublist[currentPosition + 1] = sublist[currentPosition] currentPosition = currentPosition -1 sublist[currentPosition + 1] = currentElement end = time.time() print(end – start) #calling the function and printing it insertion_sort(array) print(array) submitted by /u/Living_Cartoonist791 [link] [comments]
I tried looping through a .txt file that has 101 subarrays in a single array but that didn’t work because all characters are strings meaning it is a 0d array. I have no idea how to overcome this, because this is the first I am dealing with .txt files. How do i convert it to the type I want it to be, and be able to loop through the numbers in the file as normal?
import numpy as np import matplotlib.pyplot as plt import time #Opening file with open("1a_shuffled.txt", "r") as file: kfc = file.read() #placing file content in np.array array array = np.array(kfc) #function for insertion sort def insertion_sort(list): start = time.time() for sublist in list: for i in range(1,len(sublist)): currentElement = sublist[i] currentPosition = i - 1 while currentPosition >=0 and sublist[currentPosition] > currentElement: sublist[currentPosition + 1] = sublist[currentPosition] currentPosition = currentPosition -1 sublist[currentPosition + 1] = currentElement end = time.time() print(end - start) #calling the function and printing it insertion_sort(array) print(array) import numpy as np import matplotlib.pyplot as plt import time #Opening file with open("1a_shuffled.txt", "r") as file: kfc = file.read() #placing file content in np.array array array = np.array(kfc) #function for insertion sort def insertion_sort(list): start = time.time() for sublist in list: for i in range(1,len(sublist)): currentElement = sublist[i] currentPosition = i - 1 while currentPosition >=0 and sublist[currentPosition] > currentElement: sublist[currentPosition + 1] = sublist[currentPosition] currentPosition = currentPosition -1 sublist[currentPosition + 1] = currentElement end = time.time() print(end - start) #calling the function and printing it insertion_sort(array) print(array)
submitted by /u/Living_Cartoonist791
[link] [comments]