Hướng dẫn python list clear not working - danh sách python xóa không hoạt động

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

  • Đọc
  • Bàn luận
  • 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[] method

      Có 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.

      [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
      GEEK before clear: [6, 0, 4, 1]
      GEEK after clear: []
      
      0
      GEEK before clear: [6, 0, 4, 1]
      GEEK after clear: []
      
      1
      GEEK before clear: [6, 0, 4, 1]
      GEEK after clear: []
      
      2

      GEEK before clear: [6, 0, 4, 1]
      GEEK after 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'
      
      9
      GEEK before clear: [6, 0, 4, 1]
      GEEK after clear: []
      
      0
      GEEK before clear: [6, 0, 4, 1]
      GEEK after clear: []
      
      6
      GEEK before clear: [6, 0, 4, 1]
      GEEK after clear: []
      
      2

      Output:

      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 list

      GEEK =

      [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'
      
      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

      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=
      [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'
      
      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
      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__12

      List1 before deleting is : [1, 2, 3]
      List1 after clearing using clear[] : []
      List2 before deleting is : [5, 6, 7]
      List2 after clearing using reinitialization : []
      
      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'
      
      0
      List1 before deleting is : [1, 2, 3]
      List1 after clearing using *= 0: []
      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'
      
      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

      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

      [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;

      ________ 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 : []
      
      0
      List1 before deleting is : [1, 2, 3]
      List1 after clearing using del : []
      List2 before deleting is : [5, 6, 7]
      List2 after clearing using del : []
      
      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'
      
      9 ________ 20 ________ 55 & nbsp;

      List1 before deleting is : [1, 2, 3]
      List1 after clearing using *= 0: []
      9
      List1 before deleting is : [1, 2, 3]
      List1 after clearing using del : []
      List2 before deleting is : [5, 6, 7]
      List2 after clearing using del : []
      
      0
      List1 before deleting is : [1, 2, 3]
      List1 after clearing using del : []
      List2 before deleting is : [5, 6, 7]
      List2 after clearing using del : []
      
      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'
      
      9
      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[]5

      List1 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[]8

      Output:

      List1 before deleting is : [1, 2, 3]
      List1 after clearing using clear[] : []
      List2 before deleting is : [5, 6, 7]
      List2 after clearing using reinitialization : []
      
    • [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 ________ 71 & nbsp;
      Using “*= 0” : This is a lesser known method, but this method removes all elements of the list and makes it empty.

      GEEK =

      [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'
      
      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

      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=
      [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'
      
      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
      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__12

      List1 before deleting is : [1, 2, 3]
      List1 after clearing using clear[] : []
      List2 before deleting is : [5, 6, 7]
      List2 after clearing using reinitialization : []
      
      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'
      
      0
      List1 before deleting is : [1, 2, 3]
      List1 after clearing using *= 0: []
      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'
      
      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

      Output:

      List1 before deleting is : [1, 2, 3]
      List1 after clearing using *= 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'
      
      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.

      GEEK =

      [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'
      
      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

      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=
      [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'
      
      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
      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__12

      List1 before deleting is : [1, 2, 3]
      List1 after clearing using clear[] : []
      List2 before deleting is : [5, 6, 7]
      List2 after clearing using reinitialization : []
      
      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'
      
      0
      List1 before deleting is : [1, 2, 3]
      List1 after clearing using *= 0: []
      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'
      
      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;

      ________ 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 : []
      
      0
      List1 before deleting is : [1, 2, 3]
      List1 after clearing using del : []
      List2 before deleting is : [5, 6, 7]
      List2 after clearing using del : []
      
      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'
      
      9
      GEEK before clear: [6, 0, 4, 1]
      GEEK after clear: []
      
      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'
      
      42
      List1 before deleting is : [1, 2, 3]
      List1 after clearing using *= 0: []
      9
      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[]5

      Output:

      List1 before deleting is : [1, 2, 3]
      List1 after clearing using del : []
      List2 before deleting is : [5, 6, 7]
      List2 after clearing using del : []
      

    Làm thế nào để tôi hoàn toàn xóa một danh sách trong Python?

    Để xóa toàn bộ danh sách Python, hãy sử dụng phương thức rõ ràng [] của một danh sách.Để xóa một phần tử tại một chỉ mục cụ thể, hãy sử dụng phương thức pop [] hoặc câu lệnh DEL.use the clear[] method of a list. To remove an element at a specific index, use the pop[] method or the del statement.

    Điều gì là rõ ràng [] Python?

    Từ điển python rõ ràng [] Phương thức rõ ràng [] loại bỏ tất cả các mục khỏi từ điển.Cú pháp: Dict.clear [] Tham số: Phương thức rõ ràng [] không lấy bất kỳ tham số nào.removes all items from the dictionary. Syntax: dict.clear[] Parameters: The clear[] method doesn't take any parameters.

    Danh sách rõ ràng [] chức năng làm gì?

    Phương thức rõ ràng [] của giao diện danh sách trong java được sử dụng để loại bỏ tất cả các phần tử khỏi thùng chứa danh sách.Phương pháp này không xóa thùng chứa danh sách, thay vào đó nó chỉ cần xóa tất cả các yếu tố khỏi danh sách.remove all of the elements from the List container. This method does not deleted the List container, instead it justs removes all of the elements from the List.

    Làm cách nào để xóa tất cả các mục trong một danh sách?

    Xóa các yếu tố khỏi danh sách:..
    Sử dụng phương thức Remove [] ..
    Sử dụng phương thức pop [] của đối tượng ..
    Sử dụng toán tử DEL ..

    Bài Viết Liên Quan

    Chủ Đề