模块文件如何接收来自列表的输入?
How can a module file receive an input from a list?
我正在尝试设置模块,我对模块还很陌生。我在网上搜索了一下,找不到这个问题的答案!
我正在尝试使用我为 QuickSort 编写的一些代码创建一个模块,但是我不确定如何从其他文件获取输入。
我在模块中的代码是:
# An algorithm to sort a list of random numbers using QuickSort
def mainSortBody(arrayToSort, begin, end):
compareVarI = arrayToSort[begin]
lower = begin + 1
upper = end
while True:
while lower <= upper and arrayToSort[upper] >= compareVarI:
upper = upper - 1
while lower <= upper and arrayToSort[lower] <= compareVarI:
lower = lower + 1
if lower <= upper:
arrayToSort[lower], arrayToSort[upper] = arrayToSort[upper], arrayToSort[lower]
else:
break
arrayToSort[begin], arrayToSort[upper] = arrayToSort[upper], arrayToSort[begin]
return upper
def speedSort(arrayToSort, begin, end):
if begin >= end:
return
p = mainSortBody(arrayToSort, begin, end)
speedSort(arrayToSort, begin, p-1)
speedSort(arrayToSort, p+1, end)
def sort(arrayToSort):
speedSort(arrayToSort, 0, len(arrayToSort) - 1)
arrayToSort = []
sort(arrayToSort)
print(arrayToSort)
我的代码在另一个文件中
import main
array = [21,31,348,283,128,348,394,586,378,1923,1381782432,12348,13284]
main.sort(array)
输出
[]
Process finished with exit code 0
任何帮助将不胜感激,谢谢。
我假设 测试文件;您 运行 的文件是 test.py:
test.py 应该是这样的:
from main import sort # importing the sort function from main.py
array = [21,31,348,283,128,348,394,586,378,1923,1381782432,12348,13284]
sort(array)
print(array) # printing the array as the function returns None
和main.py:
# An algorithm to sort a list of random numbers using QuickSort
def mainSortBody(arrayToSort, begin, end):
compareVarI = arrayToSort[begin]
lower = begin + 1
upper = end
while True:
while lower <= upper and arrayToSort[upper] >= compareVarI:
upper = upper - 1
while lower <= upper and arrayToSort[lower] <= compareVarI:
lower = lower + 1
if lower <= upper:
arrayToSort[lower], arrayToSort[upper] = arrayToSort[upper], arrayToSort[lower]
else:
break
arrayToSort[begin], arrayToSort[upper] = arrayToSort[upper], arrayToSort[begin]
return upper
def speedSort(arrayToSort, begin, end):
if begin >= end:
return
p = mainSortBody(arrayToSort, begin, end)
speedSort(arrayToSort, begin, p-1)
speedSort(arrayToSort, p+1, end)
def sort(arrayToSort):
speedSort(arrayToSort, 0, len(arrayToSort) - 1)
# only keep the functions in this file; so removed the redundant parts or else they would also be executed.
希望这能解决您的问题。
如果我错了请纠正我,但我相信你有点倒退了。 main.py
不应该是导入的模块,因为它应该是程序的主要部分。您在 main.py
和 运行 中导入您需要的内容。
您可以这样做:
quicksort.py
def mainSortBody(arrayToSort, begin, end):
compareVarI = arrayToSort[begin]
lower = begin + 1
upper = end
while True:
while lower <= upper and arrayToSort[upper] >= compareVarI:
upper = upper - 1
while lower <= upper and arrayToSort[lower] <= compareVarI:
lower = lower + 1
if lower <= upper:
arrayToSort[lower], arrayToSort[upper] = (
arrayToSort[upper],
arrayToSort[lower],
)
else:
break
arrayToSort[begin], arrayToSort[upper] = arrayToSort[upper], arrayToSort[begin]
return upper
def speedSort(arrayToSort, begin, end):
if begin >= end:
return
p = mainSortBody(arrayToSort, begin, end)
speedSort(arrayToSort, begin, p - 1)
speedSort(arrayToSort, p + 1, end)
def sort(arrayToSort):
speedSort(arrayToSort, 0, len(arrayToSort) - 1)
main.py
from quicksort import sort
if __name__ == "__main__":
array = [21, 31, 348, 283, 128, 348, 394, 586, 378, 1923, 1381782432, 12348, 13284]
sort(array)
print(array)
我正在尝试设置模块,我对模块还很陌生。我在网上搜索了一下,找不到这个问题的答案!
我正在尝试使用我为 QuickSort 编写的一些代码创建一个模块,但是我不确定如何从其他文件获取输入。
我在模块中的代码是:
# An algorithm to sort a list of random numbers using QuickSort
def mainSortBody(arrayToSort, begin, end):
compareVarI = arrayToSort[begin]
lower = begin + 1
upper = end
while True:
while lower <= upper and arrayToSort[upper] >= compareVarI:
upper = upper - 1
while lower <= upper and arrayToSort[lower] <= compareVarI:
lower = lower + 1
if lower <= upper:
arrayToSort[lower], arrayToSort[upper] = arrayToSort[upper], arrayToSort[lower]
else:
break
arrayToSort[begin], arrayToSort[upper] = arrayToSort[upper], arrayToSort[begin]
return upper
def speedSort(arrayToSort, begin, end):
if begin >= end:
return
p = mainSortBody(arrayToSort, begin, end)
speedSort(arrayToSort, begin, p-1)
speedSort(arrayToSort, p+1, end)
def sort(arrayToSort):
speedSort(arrayToSort, 0, len(arrayToSort) - 1)
arrayToSort = []
sort(arrayToSort)
print(arrayToSort)
我的代码在另一个文件中
import main
array = [21,31,348,283,128,348,394,586,378,1923,1381782432,12348,13284]
main.sort(array)
输出
[]
Process finished with exit code 0
任何帮助将不胜感激,谢谢。
我假设 测试文件;您 运行 的文件是 test.py: test.py 应该是这样的:
from main import sort # importing the sort function from main.py
array = [21,31,348,283,128,348,394,586,378,1923,1381782432,12348,13284]
sort(array)
print(array) # printing the array as the function returns None
和main.py:
# An algorithm to sort a list of random numbers using QuickSort
def mainSortBody(arrayToSort, begin, end):
compareVarI = arrayToSort[begin]
lower = begin + 1
upper = end
while True:
while lower <= upper and arrayToSort[upper] >= compareVarI:
upper = upper - 1
while lower <= upper and arrayToSort[lower] <= compareVarI:
lower = lower + 1
if lower <= upper:
arrayToSort[lower], arrayToSort[upper] = arrayToSort[upper], arrayToSort[lower]
else:
break
arrayToSort[begin], arrayToSort[upper] = arrayToSort[upper], arrayToSort[begin]
return upper
def speedSort(arrayToSort, begin, end):
if begin >= end:
return
p = mainSortBody(arrayToSort, begin, end)
speedSort(arrayToSort, begin, p-1)
speedSort(arrayToSort, p+1, end)
def sort(arrayToSort):
speedSort(arrayToSort, 0, len(arrayToSort) - 1)
# only keep the functions in this file; so removed the redundant parts or else they would also be executed.
希望这能解决您的问题。
如果我错了请纠正我,但我相信你有点倒退了。 main.py
不应该是导入的模块,因为它应该是程序的主要部分。您在 main.py
和 运行 中导入您需要的内容。
您可以这样做:
quicksort.py
def mainSortBody(arrayToSort, begin, end):
compareVarI = arrayToSort[begin]
lower = begin + 1
upper = end
while True:
while lower <= upper and arrayToSort[upper] >= compareVarI:
upper = upper - 1
while lower <= upper and arrayToSort[lower] <= compareVarI:
lower = lower + 1
if lower <= upper:
arrayToSort[lower], arrayToSort[upper] = (
arrayToSort[upper],
arrayToSort[lower],
)
else:
break
arrayToSort[begin], arrayToSort[upper] = arrayToSort[upper], arrayToSort[begin]
return upper
def speedSort(arrayToSort, begin, end):
if begin >= end:
return
p = mainSortBody(arrayToSort, begin, end)
speedSort(arrayToSort, begin, p - 1)
speedSort(arrayToSort, p + 1, end)
def sort(arrayToSort):
speedSort(arrayToSort, 0, len(arrayToSort) - 1)
main.py
from quicksort import sort
if __name__ == "__main__":
array = [21, 31, 348, 283, 128, 348, 394, 586, 378, 1923, 1381782432, 12348, 13284]
sort(array)
print(array)