Hướng dẫn append alternative python - nối thêm con trăn thay thế

For the below add method:

def __add__(self, other):
    self.str_list.append(other)       
    self.count += 1
    return self.str_list

How may I rewrite this without append?without append?

PS:

1) str_list is an instance variable of type list declared in init

2) No inbuilt functions to be used.

asked Apr 26, 2017 at 3:34Apr 26, 2017 at 3:34

Hướng dẫn append alternative python - nối thêm con trăn thay thế

4

It's an interesting brain teaser, the challenge being adding to a list without using a built-in function. I can only think of one approach:

self.str_list = [*self.str_list, other]       

Other approaches such as

Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
7 are technically using the built-in method
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
8 so beware for your purpose, whatever it might be.

answered Apr 26, 2017 at 3:54Apr 26, 2017 at 3:54

EyuelDKEyuelDKEyuelDK

2,8692 gold badges18 silver badges26 bronze badges2 gold badges18 silver badges26 bronze badges

4

138

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.Learn more.
Learn more.

Sự khác biệt giữa:

some_list1 = []
some_list1.append("something")

some_list2 = []
some_list2 += ["something"]

Agf

164K40 Huy hiệu vàng279 Huy hiệu bạc232 Huy hiệu Đồng40 gold badges279 silver badges232 bronze badges40 gold badges279 silver badges232 bronze badges

Hỏi ngày 7 tháng 4 năm 2009 lúc 13:44Apr 7, 2009 at 13:44Apr 7, 2009 at 13:44

3

Đối với trường hợp của bạn, sự khác biệt duy nhất là hiệu suất: append nhanh gấp đôi.

Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141

Trong trường hợp chung

Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
3 sẽ thêm một mục vào danh sách, trong khi
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
4 sẽ sao chép tất cả các yếu tố của danh sách bên phải vào danh sách bên trái.

Cập nhật: Phân tích hoàn hảo

So sánh bytecodes Chúng ta có thể giả định rằng phiên bản

Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
3 làm lãng phí các chu kỳ trong
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
6 +
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
7 và + = phiên bản - trong
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
8. Rõ ràng
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
8 vượt trội so với
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
6 +
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
7.
self.str_list = [*self.str_list, other]       
4

Chúng tôi có thể cải thiện hiệu suất thậm chí nhiều hơn bằng cách loại bỏ

Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
6 chi phí:
self.str_list = [*self.str_list, other]       
6

Đã trả lời ngày 7 tháng 4 năm 2009 lúc 14:00Apr 7, 2009 at 14:00Apr 7, 2009 at 14:00

ConstantinconstantinConstantinConstantin

Phim thương hiệu vàng 27K1059 Huy hiệu bạc79 Hàng đồng10 gold badges59 silver badges79 bronze badges10 gold badges59 silver badges79 bronze badges

6

self.str_list = [*self.str_list, other]       
7

Xem phần Phụ lục đó thêm một yếu tố duy nhất vào danh sách, có thể là bất cứ điều gì.

self.str_list = [*self.str_list, other]       
43 tham gia danh sách.

Đã trả lời ngày 7 tháng 4 năm 2009 lúc 13:57Apr 7, 2009 at 13:57Apr 7, 2009 at 13:57

DWCDWCdwcdwc

23,5K7 Huy hiệu vàng43 Huy hiệu bạc54 Huy hiệu đồng7 gold badges43 silver badges54 bronze badges7 gold badges43 silver badges54 bronze badges

1

Trong ví dụ bạn đã đưa ra, không có sự khác biệt, về đầu ra, giữa

Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
3 và
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
4. Nhưng có một sự khác biệt giữa
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
3 và
self.str_list = [*self.str_list, other]       
47 (mà câu hỏi ban đầu được hỏi).
some_list1 = []
some_list1.append("something")
3

Như bạn có thể thấy,

Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
3 và
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
4 có kết quả tương tự; Họ thêm mục vào danh sách, mà không tạo ra một danh sách mới. Sử dụng
self.str_list = [*self.str_list, other]       
47 thêm hai danh sách và tạo ra một danh sách mới.

Đã trả lời ngày 7 tháng 4 năm 2009 lúc 13:51Apr 7, 2009 at 13:51Apr 7, 2009 at 13:51

DNSDNSDNSDNS

36.4K18 Huy hiệu vàng90 Huy hiệu bạc127 Huy hiệu đồng18 gold badges90 silver badges127 bronze badges18 gold badges90 silver badges127 bronze badges

3

+= là một nhiệm vụ. Khi bạn sử dụng nó, bạn thực sự nói ‘some_list2 = some_list2+['something']. Bài tập liên quan đến Rebinding, vì vậy:

some_list1 = []
some_list1.append("something")
7

Toán tử += cũng thường nên tạo một đối tượng danh sách mới như Danh sách +Danh sách thông thường:

some_list1 = []
some_list1.append("something")
8

Tuy nhiên trong thực tế:

some_list1 = []
some_list1.append("something")
9

Điều này là do danh sách Python triển khai __iadd __ () để tạo A += tăng cường chuyển số ngắn mạch và danh sách cuộc gọi.Extend () thay thế. (Đó là một chút của một mụn cóc kỳ lạ này: Nó thường làm những gì bạn muốn nói, nhưng vì lý do khó hiểu.)

Nói chung, nếu bạn đang nối thêm/mở rộng một danh sách hiện có và bạn muốn giữ tham chiếu đến cùng một danh sách (thay vì tạo một danh sách mới), tốt nhất là nên rõ ràng và gắn bó với append ()/extend () Phương pháp.

Đã trả lời ngày 7 tháng 4 năm 2009 lúc 15:29Apr 7, 2009 at 15:29Apr 7, 2009 at 15:29

Bobincebobincebobincebobince

519K102 Huy hiệu vàng646 Huy hiệu bạc826 Huy hiệu Đồng102 gold badges646 silver badges826 bronze badges102 gold badges646 silver badges826 bronze badges

some_list2 = []
some_list2 += ["something"]
0

thực sự là

some_list2 = []
some_list2 += ["something"]
1

Đối với một giá trị, không có sự khác biệt. Tài liệu nêu rõ, rằng:

self.str_list = [*self.str_list, other]       
61 giống như
self.str_list = [*self.str_list, other]       
62
self.str_list = [*self.str_list, other]       
63 giống như
self.str_list = [*self.str_list, other]       
64
self.str_list = [*self.str_list, other]       
63 same as
self.str_list = [*self.str_list, other]       
64
self.str_list = [*self.str_list, other]       
63 same as
self.str_list = [*self.str_list, other]       
64

Do đó rõ ràng

self.str_list = [*self.str_list, other]       
61 giống như
self.str_list = [*self.str_list, other]       
66

Đã trả lời ngày 7 tháng 4 năm 2009 lúc 13:54Apr 7, 2009 at 13:54Apr 7, 2009 at 13:54

Vartecvartecvartecvartec

128K36 Huy hiệu vàng213 Huy hiệu bạc242 Huy hiệu Đồng36 gold badges213 silver badges242 bronze badges36 gold badges213 silver badges242 bronze badges

1

Sự khác biệt là Concatenate sẽ làm phẳng danh sách kết quả, trong khi phần phụ sẽ giữ nguyên các cấp độ:concatenate will flatten the resulting list, whereas append will keep the levels intact:concatenate will flatten the resulting list, whereas append will keep the levels intact:

Vì vậy, ví dụ với:

some_list2 = []
some_list2 += ["something"]
2

Sử dụng phụ lục, bạn kết thúc với một danh sách các danh sách:

some_list2 = []
some_list2 += ["something"]
3

Sử dụng Concatenate thay thế, bạn kết thúc với một danh sách phẳng:

some_list2 = []
some_list2 += ["something"]
4

Alex Shroyer

3.3222 Huy hiệu vàng27 Huy hiệu bạc51 Huy hiệu Đồng2 gold badges27 silver badges51 bronze badges2 gold badges27 silver badges51 bronze badges

Đã trả lời ngày 10 tháng 6 năm 2014 lúc 8:56Jun 10, 2014 at 8:56Jun 10, 2014 at 8:56

SRC2SRC2SRC2SRC2

1011 Huy hiệu bạc2 Huy hiệu đồng1 silver badge2 bronze badges1 silver badge2 bronze badges

Các bài kiểm tra hiệu suất ở đây không chính xác:

  1. Bạn không nên chạy hồ sơ chỉ một lần.
  2. Nếu so sánh
    Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import timeit
    >>> timeit.Timer('s.append("something")', 's = []').timeit()
    0.20177424499999999
    >>> timeit.Timer('s += ["something"]', 's = []').timeit()
    0.41192320500000079
    
    Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import timeit
    >>> timeit.Timer('s.append("something")', 's = []').timeit()
    0.23079359499999999
    >>> timeit.Timer('s += ["something"]', 's = []').timeit()
    0.44208112500000141
    
    3 so với
    self.str_list = [*self.str_list, other]       
    
    43 Một số lần, bạn nên khai báo
    Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import timeit
    >>> timeit.Timer('s.append("something")', 's = []').timeit()
    0.20177424499999999
    >>> timeit.Timer('s += ["something"]', 's = []').timeit()
    0.41192320500000079
    
    Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import timeit
    >>> timeit.Timer('s.append("something")', 's = []').timeit()
    0.23079359499999999
    >>> timeit.Timer('s += ["something"]', 's = []').timeit()
    0.44208112500000141
    
    3 là hàm cục bộ.
  3. Kết quả thời gian khác nhau trên các phiên bản Python khác nhau: 64- và 32 bit.

e.g.

some_list2 = []
some_list2 += ["something"]
5

Các bài kiểm tra tốt có thể được tìm thấy ở đây: Danh sách Python nối thêm so với += []

tdy

Phù bằng vàng 29,9k1155 Huy hiệu bạc58 Huy hiệu đồng11 gold badges55 silver badges58 bronze badges11 gold badges55 silver badges58 bronze badges

Đã trả lời ngày 2 tháng 1 năm 2012 lúc 13:42Jan 2, 2012 at 13:42Jan 2, 2012 at 13:42

MichaelmichaelMichaelMichael

2.7874 huy hiệu vàng28 Huy hiệu bạc47 Huy hiệu đồng4 gold badges28 silver badges47 bronze badges4 gold badges28 silver badges47 bronze badges

1

Ngoài các khía cạnh được mô tả trong các câu trả lời khác, nối và +[] có những hành vi rất khác nhau khi bạn đang cố gắng xây dựng một danh sách danh sách.

some_list2 = []
some_list2 += ["something"]
6

list1+['5', '6'] thêm '5' và '6' vào danh sách2 dưới dạng các yếu tố riêng lẻ. list1.Append (['5', '6']) thêm danh sách ['5', '6'] vào danh sách2 dưới dạng một phần tử duy nhất.

Đã trả lời ngày 7 tháng 4 năm 2009 lúc 15:24Apr 7, 2009 at 15:24Apr 7, 2009 at 15:24

Chris UpChurchchris UpeclChris UpchurchChris Upchurch

15.1k6 Huy hiệu vàng50 Huy hiệu bạc66 Huy hiệu Đồng6 gold badges50 silver badges66 bronze badges6 gold badges50 silver badges66 bronze badges

Hành vi phản hồi được đề cập trong các câu trả lời khác không quan trọng trong một số trường hợp nhất định:

some_list2 = []
some_list2 += ["something"]
7

Đó là bởi vì sự phân công tăng cường luôn luôn phản hồi, ngay cả khi đối tượng bị đột biến tại chỗ. Việc tái phát ở đây là

self.str_list = [*self.str_list, other]       
70, không hoạt động cho các bộ dữ liệu.

Đã trả lời ngày 14 tháng 3 năm 2012 lúc 1:03Mar 14, 2012 at 1:03Mar 14, 2012 at 1:03

Phục hồi Monicareinstate MonicaReinstate MonicaReinstate Monica

4.4881 Huy hiệu vàng23 Huy hiệu bạc35 Huy hiệu Đồng1 gold badge23 silver badges35 bronze badges1 gold badge23 silver badges35 bronze badges

"+" không làm thay đổi danh sáchdoes not mutate the listdoes not mutate the list

.append () đột biến danh sách cũmutates the old listmutates the old list

Đã trả lời ngày 5 tháng 4 năm 2020 lúc 17:56Apr 5, 2020 at 17:56Apr 5, 2020 at 17:56

AndresandresAndresAndres

6512 Huy hiệu vàng7 Huy hiệu bạc18 Huy hiệu đồng2 gold badges7 silver badges18 bronze badges2 gold badges7 silver badges18 bronze badges

Hãy lấy một ví dụ đầu tiên

some_list2 = []
some_list2 += ["something"]
8

Đã trả lời ngày 3 tháng 9 năm 2018 lúc 13:48Sep 3, 2018 at 13:48Sep 3, 2018 at 13:48

Avnish Nishadavnish NishadAvnish NishadAvnish Nishad

1.4281 Huy hiệu vàng14 Huy hiệu bạc18 Huy hiệu đồng1 gold badge14 silver badges18 bronze badges1 gold badge14 silver badges18 bronze badges

Phương thức append () thêm một mục duy nhất vào danh sách hiện có

some_list1 = []
some_list1.append("something")

Vì vậy, ở đây một số_list1 sẽ được sửa đổi.

Updated:

Trong khi việc sử dụng + để kết hợp các phần tử của danh sách (nhiều hơn một phần tử) trong danh sách hiện có tương tự như mở rộng (như được sửa bởi thông lượng).

some_list2 = []
some_list2 += ["something"]

Vì vậy, ở đây một số_list2 và ["cái gì đó"] là hai danh sách được kết hợp.

Đã trả lời ngày 15 tháng 4 năm 2019 lúc 10:12Apr 15, 2019 at 10:12Apr 15, 2019 at 10:12

1

Tính đến hôm nay và Python 3.6, kết quả được cung cấp bởi @Constantine không còn giống nhau.

Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
1

Có vẻ như

Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
3 và
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
4 hiện có hiệu suất bằng nhau, trong khi sự khác biệt tổng hợp hoàn toàn không thay đổi:
Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.20177424499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.41192320500000079

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.Timer('s.append("something")', 's = []').timeit()
0.23079359499999999
>>> timeit.Timer('s += ["something"]', 's = []').timeit()
0.44208112500000141
2

Đã trả lời ngày 1 tháng 10 năm 2020 lúc 8:25Oct 1, 2020 at 8:25Oct 1, 2020 at 8:25

Có phải += giống như phụ lục Python?

Đối với một danh sách, += giống như phương thức mở rộng hơn là phương thức nối.Với một danh sách ở bên trái của toán tử +=, một danh sách khác là cần thiết ở bên phải của toán tử.Tất cả các mục trong danh sách bên phải của toán tử được thêm vào cuối danh sách được tham chiếu đến bên trái của toán tử.+= is more like the extend method than like the append method. With a list to the left of the += operator, another list is needed to the right of the operator. All the items in the list to the right of the operator get added to the end of the list that is referenced to the left of the operator.+= is more like the extend method than like the append method. With a list to the left of the += operator, another list is needed to the right of the operator. All the items in the list to the right of the operator get added to the end of the list that is referenced to the left of the operator.

Điều gì tương tự như phần phụ trong Python?

Các lựa chọn thay thế để nối () trong python đáng chú ý nhất là mở rộng () và chèn ().extend() and insert() .extend() and insert() .

Sự khác biệt giữa add () và append () trong python là gì?

Câu trả lời ngắn gọn: append () thay đổi danh sách tại chỗ và + trả về một danh sách mới, nó hoàn toàn không ảnh hưởng đến danh sách ban đầu.append() changes the list in-place and + returns a new list, it doesn't affects the original list at all.append() changes the list in-place and + returns a new list, it doesn't affects the original list at all.

Expend () python là gì?

Phụ lục trong Python là một phương thức được xác định trước được sử dụng để thêm một mục duy nhất vào một số loại thu thập nhất định.Nếu không có phương thức phụ lục, các nhà phát triển sẽ phải thay đổi toàn bộ mã của bộ sưu tập để thêm một giá trị hoặc mục.Trường hợp sử dụng chính của nó được nhìn thấy cho một loại thu thập danh sách.a pre-defined method used to add a single item to certain collection types. Without the append method, developers would have to alter the entire collection's code for adding a single value or item. Its primary use case is seen for a list collection type.a pre-defined method used to add a single item to certain collection types. Without the append method, developers would have to alter the entire collection's code for adding a single value or item. Its primary use case is seen for a list collection type.