Tôi đang viết một số mã được cho là tìm thấy yếu tố chính của các con số. Chức năng chính tăng thông qua các số; Tôi đang làm điều đó bởi vì tôi muốn sử dụng mã để thực hiện các thí nghiệm thời gian. Tôi không bận tâm rằng nó không phải là siêu hiệu quả, một phần của dự án đối với tôi sẽ làm cho bản thân nó hiệu quả hơn. Nó cũng chưa hoàn thành hoàn toàn [ví dụ, nó không đơn giản hóa hệ số chính]. Tôi đã thử nghiệm tất cả các chức năng ngoại trừ chức năng chính và chúng đã hoạt động, vì vậy không có vấn đề gì với chúng.
Mã của tôi là
import math
import time
primfac=[]
def primes[n]:
sieve = [True] * n
for i in xrange[3,int[n**0.5]+1,2]:
if sieve[i]:
sieve[i*i::2*i]=[False]*[[n-i*i-1]/[2*i]+1]
return [2] + [i for i in xrange[3,n,2] if sieve[i]]
def factfind[lsp,n]: #finds factors of n among primes
for i in lsp:
if n%i==0:
primfac.append[i]
else:
i+=1
def primfacfind[n1,n2]:
while n1 < n2:
n = n1
time_start = time.clock[]
factfind[primes[n],n]
print primfac
time_elapsed = time.clock[] - time_start
print "time:", time_elapsed
primfac.clear[]
n1+=1
print primfacfind[6,15]
Và chạy nó cho đầu ra
[2, 3]
time: 7.5e-05
Traceback [most recent call last]:
File "python", line 43, in
File "python", line 39, in primfacfind
AttributeError: 'list' object has no attribute 'clear'
Và tôi không thực sự chắc chắn điều gì là sai. Nó đang cung cấp đúng số cho yếu tố chính và nó đang in thời gian, nhưng dường như nó không thể xóa danh sách. Nhận xét dòng primfac.clear[]
có nó hoạt động.
Bất kỳ trợ giúp sẽ được đánh giá cao. Cảm ơn!
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
- Bàn luận Using
clear[]
methodCó nhiều cách để xóa danh sách thông qua các phương pháp của các cấu trúc khác nhau được cung cấp bởi ngôn ngữ Python. Hãy để cố gắng hiểu từng phương pháp một.
9[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
GEEK before clear: [6, 0, 4, 1] GEEK after clear: []
0GEEK before clear: [6, 0, 4, 1] GEEK after clear: []
1GEEK before clear: [6, 0, 4, 1] GEEK after clear: []
2GEEK before clear: [6, 0, 4, 1] GEEK after clear: []
3
9[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
GEEK before clear: [6, 0, 4, 1] GEEK after clear: []
0GEEK before clear: [6, 0, 4, 1] GEEK after clear: []
6GEEK before clear: [6, 0, 4, 1] GEEK after clear: []
2Output:
GEEK before clear: [6, 0, 4, 1] GEEK after clear: []
- Phương pháp số 1: Sử dụng phương thức
clear[]
Reinitializing the list : The initialization of the list in that scope, initializes the list with no value. i.e list of size 0. Let’s see the example demonstrating Method 1 and 2 to clear listGEEK
=
0[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
1[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
2[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
3[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
2[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
5__12[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
Phương pháp số 2: Khai thác lại danh sách: Việc khởi tạo danh sách trong phạm vi đó, khởi tạo danh sách không có giá trị. tức là danh sách kích thước 0. Hãy để xem ví dụ trình diễn Phương pháp 1 và 2 để xóa danh sách
GEEK before clear: [6, 0, 4, 1] GEEK after clear: []
8=
0[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
7[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
2[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
List1 before deleting is : [1, 2, 3] List1 after clearing using clear[] : [] List2 before deleting is : [5, 6, 7] List2 after clearing using reinitialization : []
3__12List1 before deleting is : [1, 2, 3] List1 after clearing using clear[] : [] List2 before deleting is : [5, 6, 7] List2 after clearing using reinitialization : []
7=
0[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
List1 before deleting is : [1, 2, 3] List1 after clearing using *= 0: []
0
2[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
1__12[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
List1 before deleting is : [1, 2, 3] List1 after clearing using del : [] List2 before deleting is : [5, 6, 7] List2 after clearing using del : []
2
9 ________ 20 ________ 48 & nbsp;[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
________ 49 & nbsp;
List1 before deleting is : [1, 2, 3] List1 after clearing using del : [] List2 before deleting is : [5, 6, 7] List2 after clearing using del : []
0List1 before deleting is : [1, 2, 3] List1 after clearing using del : [] List2 before deleting is : [5, 6, 7] List2 after clearing using del : []
1
9 ________ 20 ________ 55 & nbsp;[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
List1 before deleting is : [1, 2, 3] List1 after clearing using *= 0: []
9List1 before deleting is : [1, 2, 3] List1 after clearing using del : [] List2 before deleting is : [5, 6, 7] List2 after clearing using del : []
0List1 before deleting is : [1, 2, 3] List1 after clearing using del : [] List2 before deleting is : [5, 6, 7] List2 after clearing using del : []
1
9[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
GEEK before clear: [6, 0, 4, 1] GEEK after clear: []
0primfac.clear[]
1________ 62 ________ 49 & nbsp;
List1 before deleting is : [1, 2, 3] List1 after clearing using del : [] List2 before deleting is : [5, 6, 7] List2 after clearing using del : []
0primfac.clear[]
5List1 before deleting is : [1, 2, 3] List1 after clearing using clear[] : [] List2 before deleting is : [5, 6, 7] List2 after clearing using reinitialization : []
7=
primfac.clear[]
8Output:
List1 before deleting is : [1, 2, 3] List1 after clearing using clear[] : [] List2 before deleting is : [5, 6, 7] List2 after clearing using reinitialization : []
9 ________ 20 ________ 71 & nbsp; Using “*= 0” : This is a lesser known method, but this method removes all elements of the list and makes it empty.[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
GEEK
=
0[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
1[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
2[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
3[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
2[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
5__12[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
Phương pháp số 2: Khai thác lại danh sách: Việc khởi tạo danh sách trong phạm vi đó, khởi tạo danh sách không có giá trị. tức là danh sách kích thước 0. Hãy để xem ví dụ trình diễn Phương pháp 1 và 2 để xóa danh sách
GEEK before clear: [6, 0, 4, 1] GEEK after clear: []
8=
0[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
7[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
2[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
List1 before deleting is : [1, 2, 3] List1 after clearing using clear[] : [] List2 before deleting is : [5, 6, 7] List2 after clearing using reinitialization : []
3__12List1 before deleting is : [1, 2, 3] List1 after clearing using clear[] : [] List2 before deleting is : [5, 6, 7] List2 after clearing using reinitialization : []
7=
0[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
List1 before deleting is : [1, 2, 3] List1 after clearing using *= 0: []
0
2[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
1__12[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
Output:
List1 before deleting is : [1, 2, 3] List1 after clearing using *= 0: []
9 ________ 20 ________ 48 & nbsp; Using del : del can be used to clear the list elements in a range, if we don’t give a range, all the elements are deleted.[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
GEEK
=
0[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
1[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
2[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
3[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
2[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
5__12[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
Phương pháp số 2: Khai thác lại danh sách: Việc khởi tạo danh sách trong phạm vi đó, khởi tạo danh sách không có giá trị. tức là danh sách kích thước 0. Hãy để xem ví dụ trình diễn Phương pháp 1 và 2 để xóa danh sách
Phương pháp số 2: Khai thác lại danh sách: Việc khởi tạo danh sách trong phạm vi đó, khởi tạo danh sách không có giá trị. tức là danh sách kích thước 0. Hãy để xem ví dụ trình diễn Phương pháp 1 và 2 để xóa danh sách
GEEK before clear: [6, 0, 4, 1] GEEK after clear: []
8=
0[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
7[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
2[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
List1 before deleting is : [1, 2, 3] List1 after clearing using clear[] : [] List2 before deleting is : [5, 6, 7] List2 after clearing using reinitialization : []
3__12List1 before deleting is : [1, 2, 3] List1 after clearing using clear[] : [] List2 before deleting is : [5, 6, 7] List2 after clearing using reinitialization : []
7=
0[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
List1 before deleting is : [1, 2, 3] List1 after clearing using *= 0: []
0
2[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
1__12[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
9 ________ 20 ________ 48 & nbsp;[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
________ 49 & nbsp;
List1 before deleting is : [1, 2, 3] List1 after clearing using del : [] List2 before deleting is : [5, 6, 7] List2 after clearing using del : []
0List1 before deleting is : [1, 2, 3] List1 after clearing using del : [] List2 before deleting is : [5, 6, 7] List2 after clearing using del : []
1
9[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
GEEK before clear: [6, 0, 4, 1] GEEK after clear: []
0
42[2, 3] time: 7.5e-05 Traceback [most recent call last]: File "python", line 43, in File "python", line 39, in primfacfind AttributeError: 'list' object has no attribute 'clear'
List1 before deleting is : [1, 2, 3] List1 after clearing using *= 0: []
9List1 before deleting is : [1, 2, 3] List1 after clearing using del : [] List2 before deleting is : [5, 6, 7] List2 after clearing using del : []
0primfac.clear[]
5Output:
List1 before deleting is : [1, 2, 3] List1 after clearing using del : [] List2 before deleting is : [5, 6, 7] List2 after clearing using del : []