0
我在保存多個文件時遇到問題。我想將所有正在打印的for循環保存到文件中。我會刪除打印語句並保存到文件中。我正在閱讀的文件大約有10,000個整數。我正在閱讀的.txt文件如下。在Python中保存多個結果
http://nyx.net/~bcohen/CS2050/hw3a.txt
def hashing(buck, file):
y = len(file)
size = int(y/buck) # Size of Buckets
nums = file
lst = [set([]) for i in range(buck)]
for i in range(y):
z = nums[i] % buck
val = nums[i]
lst[z].add(val)
return lst
#*******************************************************************
def reading():
filename = input("What is the file name ")
file = open(filename, "r")
int_list = []
for i in file.read().split():
int_list.append(int(i))
return int_list
#*******************************************************************
data = reading()
print("The file has ",len(data),"Elements")
buckets = int(input("How many buckets will be used? "))
hashed = hashing(buckets,data)
for i in range(buckets):
print("Size of bucket #",i,"is equal to ",len(hashed[i]))
#file = open("Results.txt", "w")
#file.write()
#file.close()
你需要多一點的邏輯把前者'print'參數爲一個字符串。或者,您可以將'file = file'作爲關鍵字參數傳遞給'print',而不是使用'file.write'。 – Blckknght
我只是刪除了[i] hashed = hashing(bucket [i],data),它工作。感謝您的幫助 –