Hướng dẫn python large list performance - hiệu suất danh sách lớn python

Tôi đang phát triển một phần mềm nhỏ cho một dự án đại học và tôi đang gặp vấn đề: mã có hiệu suất quá thấp.

Show

Đó là một phần mềm chỉnh sửa hình ảnh và hình ảnh là một danh sách 3D lớn (danh sách chính là toàn bộ hình ảnh, mỗi danh sách bên trong nó là một đường ngang và mỗi danh sách bên trong một danh sách là một pixel, chứa ba phần tử).

Tôi cần phải thực hiện các điều chỉnh pixel-by-pixel, như nhân tất cả chúng với một hằng số, vì vậy nó sẽ giống như

for y in range(0,len(image)): 
    for x in range (0,len(image[0])):
        for c in range (0,3):
            im[y][x][c] = (im[y][x][c])*a

Trong đó

53.8211231232
57.1711571217
19.8829259872
28.5986201763
1 là danh sách 3D
53.8211231232
57.1711571217
19.8829259872
28.5986201763
2 là lượng đường ngang trong hình ảnh (kích thước dọc)
53.8211231232
57.1711571217
19.8829259872
28.5986201763
3 là lượng pixel trong một đường ngang (kích thước ngang) và
53.8211231232
57.1711571217
19.8829259872
28.5986201763
4 là thành phần của pixel (đi từ 0 đến 2).
53.8211231232
57.1711571217
19.8829259872
28.5986201763
2 is the amount of horizontal lines in the image (vertical size)
53.8211231232
57.1711571217
19.8829259872
28.5986201763
3 is the amount of pixels in a horizontal line (horizontal size)
and
53.8211231232
57.1711571217
19.8829259872
28.5986201763
4 is the component of the pixel (going from 0 to 2).

Vòng lặp này mất vài phút để trải qua một hình ảnh 12 MP duy nhất và số lượng hình ảnh tôi phải xử lý là theo thứ tự của hàng trăm, vì vậy điều này là không thể.

Tôi có thể làm gì để có được một hiệu suất lớn hơn? Ngay cả việc chỉnh sửa phần mềm cũng mất vài giây vì nó có thể là một hoạt động khá lớn, nhưng mã này quá chậm.

Cảm ơn bạn!

Cũng đáng chỉ ra rằng câu trả lời cho câu hỏi này dựa trên kích thước nhỏ của danh sách/tuple được thêm vào mỗi lần lặp. Đối với các danh sách lớn hơn, mở rộng rõ ràng là vượt trội (và liệt kê so với các bộ dữ liệu không tạo ra sự khác biệt). Bắt đầu với câu trả lời của MGILSON, tôi đã kiểm tra hành vi cho các bộ sưu tập với 600 mục, thay vì 2: Gọi phụ lục 600 lần mất gấp 8 lần so với

Nội dung chính ShowShow

  • append () vs extend (): tổng quan
  • Phương thức append ().append() method.
  • Hãy bắt đầu thêm các phần tử vào danh sách với phương thức append ().append() method.
  • Trong Python, bạn có thể sử dụng phương thức Extend () để thêm nhiều phần tử vào cuối danh sách. Tương tự như phương thức append (), phương thức mở rộng () sửa đổi danh sách ban đầu.extend() method to add multiple elements to the end of a list. Similar to the append() method, the extend() method modifies the original list.
  • Đằng sau hậu trường, phương thức mở rộng () các vòng lặp thông qua các yếu tố được cung cấp dưới dạng đối số và thêm chúng vào cuối danh sách.extend() method loops through the elements provided as arguments and adds them to the end of the list.
  • Tóm tắt lại
  • Hiệu suất: expend () vs extend ()
  • Thêm nhiều phần tử: expend () vs extend ()
  • Thêm một phần tử duy nhất: append () vs extend ()
  • Sử dụng phương thức append () bằng cách truyền một phần tử duy nhất làm đối số.append() method by passing a single element as an argument.
  • Phần tử là phần tử được chèn vào danh sách.
  • Nếu bạn cố gắng thêm nhiều yếu tố làm danh sách vào cuối danh sách, bạn sẽ nhận được kết quả tương tự khi sử dụng append () theo cách này.
  • Đọc thêm
  • Là mở rộng hay nối liền Python nhanh hơn?
  • Mở rộng nhanh hơn so với phần phụ lục?
  • Có phải phụ lục chậm trong Python?
  • Khi nào nên sử dụng Phụ lục vs mở rộng Python?

53.8211231232
57.1711571217
19.8829259872
28.5986201763
3 với danh sách/tuple được xác định thủ công (tức là
53.8211231232
57.1711571217
19.8829259872
28.5986201763
4):

Nội dung chính ShowShowShow

  • append () vs extend (): tổng quan
  • Tóm tắt lại
  • Hiệu suất: expend () vs extend ()
  • Thêm nhiều phần tử: expend () vs extend ()
  • Thêm một phần tử duy nhất: append () vs extend ()
  • Sử dụng phương thức append () bằng cách truyền một phần tử duy nhất làm đối số.append() method by passing a single element as an argument.
  • Phần tử là phần tử được chèn vào danh sách.
  • Nếu bạn cố gắng thêm nhiều yếu tố làm danh sách vào cuối danh sách, bạn sẽ nhận được kết quả tương tự khi sử dụng append () theo cách này.
  • Đọc thêm
  • Là mở rộng hay nối liền Python nhanh hơn?
  • Mở rộng nhanh hơn so với phần phụ lục?
  • Có phải phụ lục chậm trong Python?
  • Khi nào nên sử dụng Phụ lục vs mở rộng Python?
42.4969689846
5.45146393776
5.38034892082

53.8211231232
57.1711571217
19.8829259872
28.5986201763
3 với danh sách/tuple được xác định thủ công (tức là
53.8211231232
57.1711571217
19.8829259872
28.5986201763
4):

Nội dung chính ShowShow

Sử dụng phương thức append () bằng cách truyền một phần tử duy nhất làm đối số.

Phần lớn năm giây này thực sự là sự sáng tạo danh sách/tuple. Chuẩn bị nó trước khi cuộc gọi

import timeit

def append_loop(foo, reps):
    for i in range(reps):
        foo.append(i)

def append_comp(foo, reps):
    [foo.append(i) for i in range(reps)]

def extend_lst(foo, reps):
    foo.extend([i for i in range(reps)])

def extend_tup(foo, reps):
    foo.extend((i for i in range(reps)))

repetitions = 600

print timeit.timeit('append_loop([], repetitions)', setup='from __main__ import append_loop, repetitions')
print timeit.timeit('append_comp([], repetitions)', setup='from __main__ import append_comp, repetitions')
print timeit.timeit('extend_lst([], repetitions)', setup='from __main__ import extend_lst, repetitions')
print timeit.timeit('extend_tup([], repetitions)', setup='from __main__ import extend_tup, repetitions')

53.8211231232
57.1711571217
19.8829259872
28.5986201763
5 mang lại thời gian để mở rộng đến
1.42491698265
0.657584905624

cho danh sách và tuple, tương ứng.

53.8211231232
57.1711571217
19.8829259872
28.5986201763

Đối với một trường hợp thực tế hơn (và công bằng hơn), người ta có thể tự động tạo dữ liệu trong cuộc gọi chức năng:

.

Mở rộng nhanh hơn so với phần phụ lục?

Có phải phụ lục chậm trong Python?append() and extend() method is that:

  • 53.8211231232
    57.1711571217
    19.8829259872
    28.5986201763
    
    3 với danh sách/tuple được xác định thủ công (tức là
    53.8211231232
    57.1711571217
    19.8829259872
    28.5986201763
    
    4):append() method adds a single element to the end of a list.
  • Nội dung chính ShowShowextend() method adds multiple items.
Sử dụng phương thức append () bằng cách truyền một phần tử duy nhất làm đối số.
extend() append()
Phần lớn năm giây này thực sự là sự sáng tạo danh sách/tuple. Chuẩn bị nó trước khi cuộc gọi
53.8211231232
57.1711571217
19.8829259872
28.5986201763
5 mang lại thời gian để mở rộng đến
1.42491698265
0.657584905624
cho danh sách và tuple, tương ứng.Đối với một trường hợp thực tế hơn (và công bằng hơn), người ta có thể tự động tạo dữ liệu trong cuộc gọi chức năng:
.O(n), with n being a number of list elements. Thời gian là:O(1) or constant.

Như chúng ta có thể thấy, mở rộng với sự hiểu biết danh sách vẫn nhanh hơn hai lần so với việc thêm vào. Ngoài ra, sự hiểu biết tuple có vẻ chậm hơn đáng kể so với danh sách hiểu và append() and extend().

append () vs extend (): tổng quan

Phương thức append ().append() method.

Phương thức mở rộng () thêm nhiều mục.extend() method adds multiple items.

Dưới đây là một bảng gian lận nhanh chóng mô tả sự khác biệt.

Thêm nhiều yếu tố vào một danh sách.

Thêm đầu vào như một phần tử duy nhất vào một danh sách.

  • Chiều dài tăng theo số lượng các yếu tố được thêm vào. append() method.append() method.
  • Chiều dài tăng thêm 1. extend() method. extend() method.

Độ phức tạp về thời gian là O (N), với N là một số yếu tố danh sách.O(n), with n being a number of list elements. list data type.list data type.

Độ phức tạp thời gian O (1) hoặc không đổi.O(1) or constant. append() method.append() method.

Trong hướng dẫn toàn diện này, bạn sẽ xem xét chi tiết về sự khác biệt giữa append () và extend ().append() and extend().append() method. This method adds an element to the end (right-hand side) of an existing list.append() method. This method adds an element to the end (right-hand side) of an existing list.

Trong Python, một danh sách là một loại dữ liệu có thể thay đổi được sử dụng để lưu trữ các yếu tố. Danh sách hỗ trợ dễ dàng truy cập vào các yếu tố. Bạn có thể thêm, cập nhật và xóa các mục.

53.8211231232
57.1711571217
19.8829259872
28.5986201763
2

Where:

  • Trong Python, bạn cần phải đối phó với danh sách rất nhiều. is the existing list to which we want to add an element. is the existing list to which we want to add an element.
  • Khi làm việc với một danh sách, bạn thường muốn thêm các yếu tố vào nó sau khi tạo. is the element to be added to the end of list. is the element to be added to the end of list.

Ví dụ: nếu danh sách của bạn đại diện cho một hàng đợi cho một bữa trưa, bạn muốn có thể thêm/xóa người tham gia bất cứ lúc nào.

Trong hướng dẫn này, chúng tôi sẽ tập trung vào hai cách để thêm các yếu tố vào cuối (phía bên phải) của một danh sách:append() method is by adding a single value to the end of a list.append() method is by adding a single value to the end of a list.

Phương thức append ().append() method.

Phương thức mở rộng (). extend() method.

53.8211231232
57.1711571217
19.8829259872
28.5986201763
3

Output:

53.8211231232
57.1711571217
19.8829259872
28.5986201763
0

Các phương pháp này được tích hợp vào kiểu dữ liệu danh sách.list data type.

Hãy bắt đầu thêm các phần tử vào danh sách với phương thức append ().append() method.

Loại danh sách Python hỗ trợ phương thức append (). Phương pháp này thêm một phần tử vào phần cuối (phía bên phải) của một danh sách hiện có.append() method. This method adds an element to the end (right-hand side) of an existing list.append() method.append() method.

53.8211231232
57.1711571217
19.8829259872
28.5986201763
1

Như đã đề cập, phương thức append () không mất nhiều hơn một đối số. Do đó, việc làm điều này dẫn đến một lỗi:append() method does not take more than one argument. Thus, doing this results in an error:append() method does not take more than one argument. Thus, doing this results in an error:append() method does not take more than one argument. Thus, doing this results in an error:

53.8211231232
57.1711571217
19.8829259872
28.5986201763
2

Tại thời điểm này, bạn có thể xem xét một cách giải quyết của việc thêm nhiều số làm danh sách.

53.8211231232
57.1711571217
19.8829259872
28.5986201763
3

Output:

1.42491698265
0.657584905624
0

Nhưng như bạn có thể thấy, điều này không hoạt động như bạn muốn.

Thay vì thêm các phần tử một cách riêng biệt, phương thức append () chỉ cần thêm toàn bộ danh sách vào cuối danh sách khác như một phần tử duy nhất.append() method just adds the whole list to the end of the other list as a single element.append() method just adds the whole list to the end of the other list as a single element.append() method just adds the whole list to the end of the other list as a single element.

Vì vậy, bây giờ danh sách các số bao gồm ba số và một danh sách các số.

Phương thức append () coi danh sách các số là một đối tượng.

Nếu bạn muốn thêm nhiều phần tử vào danh sách bằng phương thức append (), bạn cần tạo một vòng lặp. append() method, you need to create a loop. append() method, you need to create a loop. append() method, you need to create a loop.

Ví dụ 3

Hãy để thêm nhiều phần tử vào cuối danh sách với một vòng lặp và phương thức append ():append() method:append() method:append() method:

1.42491698265
0.657584905624
1

Output:

1.42491698265
0.657584905624
2

Bây giờ các con số được thêm vào cuối danh sách theo cách bạn muốn.

Tuy nhiên, vì việc thêm nhiều yếu tố trong một lần là một điều phổ biến cần làm, có một phương pháp riêng để làm điều đó.

Để thêm nhiều phần tử vào cuối danh sách, hãy sử dụng phương thức Extend ().extend() method.extend() method.extend() method.

extend()

Trong Python, bạn có thể sử dụng phương thức Extend () để thêm nhiều phần tử vào cuối danh sách. Tương tự như phương thức append (), phương thức mở rộng () sửa đổi danh sách ban đầu.extend() method to add multiple elements to the end of a list. Similar to the append() method, the extend() method modifies the original list.extend() method to add multiple elements to the end of a list. Similar to the append() method, the extend() method modifies the original list.extend() method to add multiple elements to the end of a list. Similar to the append() method, the extend() method modifies the original list.

Cú pháp của phương thức mở rộng () là: extend() method is: extend() method is: extend() method is:

1.42491698265
0.657584905624
3

Where:

  • Danh sách là tên của danh sách bạn muốn thêm các yếu tố vào. is the name of the list you want to add elements to. is the name of the list you want to add elements to. is the name of the list you want to add elements to.
  • Các yếu tố là một điều không thể thay đổi, chẳng hạn như một danh sách, có các yếu tố bạn muốn thêm vào cuối danh sách. is an iterable, such as a list, that has the elements you want to add to the end of list. is an iterable, such as a list, that has the elements you want to add to the end of list. is an iterable, such as a list, that has the elements you want to add to the end of list.

Đằng sau hậu trường, phương thức mở rộng () các vòng lặp thông qua các yếu tố được cung cấp dưới dạng đối số và thêm chúng vào cuối danh sách.extend() method loops through the elements provided as arguments and adds them to the end of the list.extend() method loops through the elements provided as arguments and adds them to the end of the list.extend() method loops through the elements provided as arguments and adds them to the end of the list.

Một trường hợp sử dụng phổ biến cho phương thức mở rộng () là thêm một số phần tử vào danh sách hiện có. extend() method is to add a number of elements to an existing list. extend() method is to add a number of elements to an existing list. extend() method is to add a number of elements to an existing list.

ví dụ 1

Ví dụ: hãy để thêm một số vào cuối danh sách các số:

1.42491698265
0.657584905624
4

Output:

1.42491698265
0.657584905624
2

Như bạn có thể thấy, danh sách các số ban đầu đã được cập nhật và bây giờ có thêm một số ở cuối của nó.append() method you saw in the previous chapter.append() method you saw in the previous chapter.append() method you saw in the previous chapter.

Ví dụ 2extend() function argument does not need to be a list. As I mentioned, it can be any iterable type in Python.extend() function argument does not need to be a list. As I mentioned, it can be any iterable type in Python.extend() function argument does not need to be a list. As I mentioned, it can be any iterable type in Python.

Một ví dụ khác, hãy để cố gắng thêm nhiều số vào danh sách các số bằng phương thức append ().

Như đã đề cập, phương thức append () không mất nhiều hơn một đối số. Do đó, việc làm điều này dẫn đến một lỗi:

Tại thời điểm này, bạn có thể xem xét một cách giải quyết của việc thêm nhiều số làm danh sách.extend() method.extend() method.extend() method.

Ví dụ 2

Một ví dụ khác, hãy để cố gắng thêm nhiều số vào danh sách các số bằng phương thức append ().

1.42491698265
0.657584905624
6

Output:

1.42491698265
0.657584905624
2

Như đã đề cập, phương thức append () không mất nhiều hơn một đối số. Do đó, việc làm điều này dẫn đến một lỗi:

Tại thời điểm này, bạn có thể xem xét một cách giải quyết của việc thêm nhiều số làm danh sách.extend() method.extend() method.extend() method:

1.42491698265
0.657584905624
8

Output:

1.42491698265
0.657584905624
9

Ví dụ 2

Ví dụ 3extend() method loops through the string character by character and adds each character to the end of the list.

Hãy để thêm nhiều phần tử vào cuối danh sách với một vòng lặp và phương thức append ():append() method:extend() method:extend() method. This is because the type of argument needs to be iterable, that is, something that can be looped through

Bây giờ các con số được thêm vào cuối danh sách theo cách bạn muốn.

Tuy nhiên, vì việc thêm nhiều yếu tố trong một lần là một điều phổ biến cần làm, có một phương pháp riêng để làm điều đó.extend() method loops through the string character by character and adds each character to the end of the list.extend() method fails.

Để thêm nhiều phần tử vào cuối danh sách, hãy sử dụng phương thức Extend ().extend() method.extend() method. This is because the type of argument needs to be iterable, that is, something that can be looped through

Trong Python, bạn có thể sử dụng phương thức Extend () để thêm nhiều phần tử vào cuối danh sách. Tương tự như phương thức append (), phương thức mở rộng () sửa đổi danh sách ban đầu.extend() method to add multiple elements to the end of a list. Similar to the append() method, the extend() method modifies the original list.extend() method, you need to place the element into an iterable.

Cú pháp của phương thức mở rộng () là: extend() method is:extend() method fails.

Danh sách là tên của danh sách bạn muốn thêm các yếu tố vào. is the name of the list you want to add elements to.extend() method to add a single element to the end of a list. To do this, the element needs to be wrapped inside of an iterable.

import timeit

def append_loop(foo, reps):
    for i in range(reps):
        foo.append(i)

def append_comp(foo, reps):
    [foo.append(i) for i in range(reps)]

def extend_lst(foo, reps):
    foo.extend([i for i in range(reps)])

def extend_tup(foo, reps):
    foo.extend((i for i in range(reps)))

repetitions = 600

print timeit.timeit('append_loop([], repetitions)', setup='from __main__ import append_loop, repetitions')
print timeit.timeit('append_comp([], repetitions)', setup='from __main__ import append_comp, repetitions')
print timeit.timeit('extend_lst([], repetitions)', setup='from __main__ import extend_lst, repetitions')
print timeit.timeit('extend_tup([], repetitions)', setup='from __main__ import extend_tup, repetitions')
0

Output:

Các yếu tố là một điều không thể thay đổi, chẳng hạn như một danh sách, có các yếu tố bạn muốn thêm vào cuối danh sách. is an iterable, such as a list, that has the elements you want to add to the end of list.extend() method, you need to place the element into an iterable.

Đằng sau hậu trường, phương thức mở rộng () các vòng lặp thông qua các yếu tố được cung cấp dưới dạng đối số và thêm chúng vào cuối danh sách.extend() method loops through the elements provided as arguments and adds them to the end of the list.append() method as it is semantically more appropriate.

Một trường hợp sử dụng phổ biến cho phương thức mở rộng () là thêm một số phần tử vào danh sách hiện có. extend() method is to add a number of elements to an existing list.extend() method to add a single element to the end of a list. To do this, the element needs to be wrapped inside of an iterable.

53.8211231232
57.1711571217
19.8829259872
28.5986201763
0 append() and extend() methods work in Python.

Tuy nhiên, thay vì thực hiện theo cách này, hãy sử dụng phương thức append () vì nó phù hợp hơn về mặt ngữ nghĩa.append() method as it is semantically more appropriate.append() method as it is semantically more appropriate.append() and extend() methods is:

  • Tóm tắt lạiappend() method adds a single element to a list.
  • Tại thời điểm này, bạn nên có một sự hiểu biết tốt về cách các phương thức expend () và mở rộng () hoạt động trong Python. append() and extend() methods work in Python. append() and extend() methods work in Python.extend() method adds multiple elements to a list.

Tóm lại, sự khác biệt giữa các phương thức expend () và extend () là:append() and extend() methods is:append() and extend() methods is:

Hiệu suất: expend () vs extend ()

Dựa trên ví dụ trước, bạn có thể thực hiện những điều tương tự bằng cách sử dụng append () và extend ().append() and extend().append() and extend().append() and extend().

Để thêm nhiều yếu tố, bạn có thể sử dụng:

  • Mở rộng () bằng cách cho nó một lập luận có thể lặp lại. by giving it an iterable argument. by giving it an iterable argument. by giving it an iterable argument.
  • append () bằng cách lặp thông qua một phần tử và nối riêng từng phần tử. by looping through an iterable and appending each element separately. by looping through an iterable and appending each element separately. by looping through an iterable and appending each element separately.

Tương tự như vậy, để thêm một phần tử vào cuối danh sách, bạn có thể sử dụng:

  • Phương thức nối () bằng cách chuyển phần tử đơn như một đối số. method by passing the single element as an argument. method by passing the single element as an argument. method by passing the single element as an argument.
  • Mở rộng phương thức () bằng cách gói phần tử duy nhất thành một thứ có thể lặp lại và chuyển nó như một đối số. method by wrapping the single element into an iterable and passing it as an argument. method by wrapping the single element into an iterable and passing it as an argument. method by wrapping the single element into an iterable and passing it as an argument.

Điều này có thể khiến bạn tự hỏi làm thế nào các phương pháp này so với nhau khi nói đến hiệu suất.

Hãy để so sánh các hiệu suất của các phương thức expend () và extend () bằng cách chạy các thử nghiệm đơn giản.append() and extend() methods by running simple experiments.append() and extend() methods by running simple experiments.append() and extend() methods by running simple experiments.

Thêm nhiều phần tử: expend () vs extend ()

Hãy cùng chạy một thử nghiệm thêm nhiều yếu tố vào danh sách.

Chúng tôi sẽ so sánh append () với phương pháp vòng lặp để mở rộng () phương thức.append() with for loop approach to extend() method.append() with for loop approach to extend() method.append() with for loop approach to extend() method.

Đây là mã:

import timeit

def append_loop(foo, reps):
    for i in range(reps):
        foo.append(i)

def append_comp(foo, reps):
    [foo.append(i) for i in range(reps)]

def extend_lst(foo, reps):
    foo.extend([i for i in range(reps)])

def extend_tup(foo, reps):
    foo.extend((i for i in range(reps)))

repetitions = 600

print timeit.timeit('append_loop([], repetitions)', setup='from __main__ import append_loop, repetitions')
print timeit.timeit('append_comp([], repetitions)', setup='from __main__ import append_comp, repetitions')
print timeit.timeit('extend_lst([], repetitions)', setup='from __main__ import extend_lst, repetitions')
print timeit.timeit('extend_tup([], repetitions)', setup='from __main__ import extend_tup, repetitions')
2

Output:

import timeit

def append_loop(foo, reps):
    for i in range(reps):
        foo.append(i)

def append_comp(foo, reps):
    [foo.append(i) for i in range(reps)]

def extend_lst(foo, reps):
    foo.extend([i for i in range(reps)])

def extend_tup(foo, reps):
    foo.extend((i for i in range(reps)))

repetitions = 600

print timeit.timeit('append_loop([], repetitions)', setup='from __main__ import append_loop, repetitions')
print timeit.timeit('append_comp([], repetitions)', setup='from __main__ import append_comp, repetitions')
print timeit.timeit('extend_lst([], repetitions)', setup='from __main__ import extend_lst, repetitions')
print timeit.timeit('extend_tup([], repetitions)', setup='from __main__ import extend_tup, repetitions')
3

Như bạn có thể thấy, việc nối thêm ~ 5 lần dài hơn so với kéo dài.

Vì vậy, hiệu suất khôn ngoan, sử dụng phương thức mở rộng () khi thêm nhiều phần tử vào danh sách.extend() method when adding multiple elements to a list.extend() method when adding multiple elements to a list.extend() method when adding multiple elements to a list.

Thêm một phần tử duy nhất: append () vs extend ()

Một ví dụ khác, hãy để so sánh hiệu suất của việc thêm một phần tử vào cuối danh sách bằng cách:

  • Sử dụng phương thức append () bằng cách truyền một phần tử duy nhất làm đối số.append() method by passing a single element as an argument.append() method by passing a single element as an argument.append() method by passing a single element as an argument.
  • Sử dụng phương thức mở rộng () bằng cách gói phần tử thành một điều khác.extend() method by wrapping the element into an iterable.extend() method by wrapping the element into an iterable.extend() method by wrapping the element into an iterable.

Đây là mã:

import timeit

def append_loop(foo, reps):
    for i in range(reps):
        foo.append(i)

def append_comp(foo, reps):
    [foo.append(i) for i in range(reps)]

def extend_lst(foo, reps):
    foo.extend([i for i in range(reps)])

def extend_tup(foo, reps):
    foo.extend((i for i in range(reps)))

repetitions = 600

print timeit.timeit('append_loop([], repetitions)', setup='from __main__ import append_loop, repetitions')
print timeit.timeit('append_comp([], repetitions)', setup='from __main__ import append_comp, repetitions')
print timeit.timeit('extend_lst([], repetitions)', setup='from __main__ import extend_lst, repetitions')
print timeit.timeit('extend_tup([], repetitions)', setup='from __main__ import extend_tup, repetitions')
4

Output:

import timeit

def append_loop(foo, reps):
    for i in range(reps):
        foo.append(i)

def append_comp(foo, reps):
    [foo.append(i) for i in range(reps)]

def extend_lst(foo, reps):
    foo.extend([i for i in range(reps)])

def extend_tup(foo, reps):
    foo.extend((i for i in range(reps)))

repetitions = 600

print timeit.timeit('append_loop([], repetitions)', setup='from __main__ import append_loop, repetitions')
print timeit.timeit('append_comp([], repetitions)', setup='from __main__ import append_comp, repetitions')
print timeit.timeit('extend_lst([], repetitions)', setup='from __main__ import extend_lst, repetitions')
print timeit.timeit('extend_tup([], repetitions)', setup='from __main__ import extend_tup, repetitions')
5

Như bạn có thể thấy, việc nối thêm ~ 5 lần dài hơn so với kéo dài.

Vì vậy, hiệu suất khôn ngoan, sử dụng phương thức mở rộng () khi thêm nhiều phần tử vào danh sách.extend() method when adding multiple elements to a list.extend() method when adding multiple elements to a list.

Thêm một phần tử duy nhất: append () vs extend () append() vs extend() in Python.

Một ví dụ khác, hãy để so sánh hiệu suất của việc thêm một phần tử vào cuối danh sách bằng cách:

Sử dụng phương thức append () bằng cách truyền một phần tử duy nhất làm đối số.append() method by passing a single element as an argument.append() method by passing a single element as an argument.

Sử dụng phương thức mở rộng () bằng cách gói phần tử thành một điều khác.extend() method by wrapping the element into an iterable.extend() method by wrapping the element into an iterable. append() and extend() methods are not the only ways to add elements to the end of a list.

Vì vậy, hiệu suất khôn ngoan, sử dụng phương thức mở rộng () khi thêm nhiều phần tử vào danh sách.extend() method when adding multiple elements to a list.

  • Thêm một phần tử duy nhất: append () vs extend () append() vs extend() in Python.insert() method.
  • Sử dụng phương thức append () bằng cách truyền một phần tử duy nhất làm đối số.append() method by passing a single element as an argument.+ operator.

Sử dụng phương thức mở rộng () bằng cách gói phần tử thành một điều khác.extend() method by wrapping the element into an iterable. append() and extend() methods are not the only ways to add elements to the end of a list.

insert()

Vì vậy, hiệu suất khôn ngoan, sử dụng phương thức mở rộng () khi thêm nhiều phần tử vào danh sách.insert() method can be used to insert an element at a specific index in a list.

Thêm một phần tử duy nhất: append () vs extend () append() vs extend() in Python.insert() method.

import timeit

def append_loop(foo, reps):
    for i in range(reps):
        foo.append(i)

def append_comp(foo, reps):
    [foo.append(i) for i in range(reps)]

def extend_lst(foo, reps):
    foo.extend([i for i in range(reps)])

def extend_tup(foo, reps):
    foo.extend((i for i in range(reps)))

repetitions = 600

print timeit.timeit('append_loop([], repetitions)', setup='from __main__ import append_loop, repetitions')
print timeit.timeit('append_comp([], repetitions)', setup='from __main__ import append_comp, repetitions')
print timeit.timeit('extend_lst([], repetitions)', setup='from __main__ import extend_lst, repetitions')
print timeit.timeit('extend_tup([], repetitions)', setup='from __main__ import extend_tup, repetitions')
6

Where:

  • Sử dụng phương thức append () bằng cách truyền một phần tử duy nhất làm đối số.+ operator. is the index at which you want to add the element.
  • Sử dụng phương thức mở rộng () bằng cách gói phần tử thành một điều khác. append() and extend() methods are not the only ways to add elements to the end of a list. is the element to be inserted in the list.

Các phương pháp tương tự khácinsert() method can be used to insert an element at a specific index in a list.insert() to add an element to the end of a list too.insert() to add an element to the end of a list too.

Trong Python, các phương thức expend () và extend () không phải là cách duy nhất để thêm các phần tử vào cuối danh sách.

import timeit

def append_loop(foo, reps):
    for i in range(reps):
        foo.append(i)

def append_comp(foo, reps):
    [foo.append(i) for i in range(reps)]

def extend_lst(foo, reps):
    foo.extend([i for i in range(reps)])

def extend_tup(foo, reps):
    foo.extend((i for i in range(reps)))

repetitions = 600

print timeit.timeit('append_loop([], repetitions)', setup='from __main__ import append_loop, repetitions')
print timeit.timeit('append_comp([], repetitions)', setup='from __main__ import append_comp, repetitions')
print timeit.timeit('extend_lst([], repetitions)', setup='from __main__ import extend_lst, repetitions')
print timeit.timeit('extend_tup([], repetitions)', setup='from __main__ import extend_tup, repetitions')
7

Output:

53.8211231232
57.1711571217
19.8829259872
28.5986201763
0

Trong Python, phương thức chèn () có thể được sử dụng để chèn một phần tử tại một chỉ mục cụ thể trong danh sách.

Cú pháp như sau:append() this way.append() this way.append() this way.

Chỉ mục là chỉ mục mà bạn muốn thêm phần tử.

import timeit

def append_loop(foo, reps):
    for i in range(reps):
        foo.append(i)

def append_comp(foo, reps):
    [foo.append(i) for i in range(reps)]

def extend_lst(foo, reps):
    foo.extend([i for i in range(reps)])

def extend_tup(foo, reps):
    foo.extend((i for i in range(reps)))

repetitions = 600

print timeit.timeit('append_loop([], repetitions)', setup='from __main__ import append_loop, repetitions')
print timeit.timeit('append_comp([], repetitions)', setup='from __main__ import append_comp, repetitions')
print timeit.timeit('extend_lst([], repetitions)', setup='from __main__ import extend_lst, repetitions')
print timeit.timeit('extend_tup([], repetitions)', setup='from __main__ import extend_tup, repetitions')
9

Output:

53.8211231232
57.1711571217
19.8829259872
28.5986201763
0

Phần tử là phần tử được chèn vào danh sách.

Điều này có nghĩa là bạn có thể sử dụng chèn () để thêm một phần tử vào cuối danh sách.

Chẳng hạn, hãy để chèn một số ở cuối danh sách:

Phương thức này chỉ cho phép bạn chèn một phần tử tại một thời điểm.

53.8211231232
57.1711571217
19.8829259872
28.5986201763
1

Output:

1.42491698265
0.657584905624
2

Nếu bạn cố gắng thêm nhiều yếu tố làm danh sách vào cuối danh sách, bạn sẽ nhận được kết quả tương tự khi sử dụng append () theo cách này.

Ví dụ:append() and extend() methods in Python lists.append() and extend() methods in Python lists.append() and extend() methods in Python lists.

Toán tử +

  • Append () có thể được sử dụng để thêm một phần tử cùng một lúc.append() can be used to add one element at a time.append() can be used to add one element at a time.append() can be used to add one element at a time.
  • Phương thức mở rộng () có thể được sử dụng để thêm nhiều phần tử cùng một lúc. extend() method can be used to add multiple elements at once. extend() method can be used to add multiple elements at once. extend() method can be used to add multiple elements at once.

Cảm ơn vì đã đọc.

Mã hóa hạnh phúc!

Đọc thêm

Trang web lập trình tốt nhất

50 câu hỏi phỏng vấn Python

Là mở rộng hay nối liền Python nhanh hơn?

.extend with list comprehension is still over two times faster than appending.extend with list comprehension is still over two times faster than appending.extend with list comprehension is still over two times faster than appending.

Mở rộng nhanh hơn so với phần phụ lục?

Đối với các danh sách lớn với một triệu yếu tố, thời gian chạy của phương thức mở rộng () nhanh hơn 60% so với thời gian chạy của phương thức append ().60% faster than the runtime of the append() method.60% faster than the runtime of the append() method.60% faster than the runtime of the append() method.

Có phải phụ lục chậm trong Python?

Thỉnh thoảng, sẽ phải sao chép tất cả các yếu tố của một danh sách vào một danh sách lớn hơn,.Phụ lục phải chậm hơn so với việc phân bổ trước toàn bộ độ dài của danh sách và gán cho các chỉ số riêng lẻ của danh sách đó (tất nhiên bạn chỉ có thể làm điều đó nếu bạn biết bạn sẽ cần danh sách trước như thế nào).append must be slower than pre-allocating the entire length of the list and assigning to individual indices of that list (of course you can only do that if you know how big you will need the list to be beforehand).append must be slower than pre-allocating the entire length of the list and assigning to individual indices of that list (of course you can only do that if you know how big you will need the list to be beforehand).append must be slower than pre-allocating the entire length of the list and assigning to individual indices of that list (of course you can only do that if you know how big you will need the list to be beforehand).

Khi nào nên sử dụng Phụ lục vs mở rộng Python?

append () thêm một phần tử vào cuối danh sách trong khi .extend () có thể thêm nhiều phần tử riêng lẻ vào cuối danh sách.extend() can add multiple individual elements to the end of the list.extend() can add multiple individual elements to the end of the list. extend() can add multiple individual elements to the end of the list.