Python 3:
Câu trả lời của User225312 là chính xác:
A. Để đếm số lượng ký tự trong đối tượng >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 1, bạn có thể sử dụng hàm >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 2: To count number of characters in >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 1 object, you can use >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 2 function:
>>> print(len('please anwser my question')) 25B. Để có được kích thước bộ nhớ theo byte được phân bổ để lưu trữ đối tượng >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 1, bạn có thể sử dụng hàm >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 4 To get memory size in bytes allocated to store >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 1 object, you can use >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 4 function
>>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50Python 2:
Nó trở nên phức tạp cho Python 2.
A. Hàm >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 2 trong Python 2 trả về số lượng byte được phân bổ để lưu trữ các ký tự được mã hóa trong một đối tượng >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 1. The >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 2 function in Python 2 returns count of bytes allocated to store encoded characters in a >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 1 object.
Đôi khi nó sẽ bằng số nhân vật:
>>> print(len('abc')) 3Nhưng đôi khi, nó sẽ không:
>>> print(len('йцы')) # String contains Cyrillic symbols 6Đó là bởi vì >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 1 có thể sử dụng mã hóa có độ dài thay đổi bên trong. Vì vậy, để đếm các ký tự trong >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 1, bạn nên biết mã hóa đối tượng >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 1 của bạn đang sử dụng. Sau đó, bạn có thể chuyển đổi nó thành đối tượng >>> print(len('abc')) 3 0 và nhận số lượng ký tự:
>>> print(len('йцы'.decode('utf8'))) #String contains Cyrillic symbols 3B. Hàm >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 4 làm điều tương tự như trong Python 3 - nó trả về số lượng byte được phân bổ để lưu trữ toàn bộ đối tượng chuỗi The >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 4 function does the same thing as in Python 3 - it returns count of bytes allocated to store the whole string object
>>> print(getsizeof('йцы')) 27 >>> print(getsizeof('йцы'.decode('utf8'))) 32W3Schools được tối ưu hóa cho việc học và đào tạo. Ví dụ có thể được đơn giản hóa để cải thiện việc đọc và học tập. Hướng dẫn, tài liệu tham khảo và ví dụ được xem xét liên tục để tránh lỗi, nhưng chúng tôi không thể đảm bảo tính chính xác đầy đủ của tất cả các nội dung. Trong khi sử dụng W3Schools, bạn đồng ý đã đọc và chấp nhận các điều khoản sử dụng, cookie và chính sách bảo mật của chúng tôi.
Bản quyền 1999-2022 bởi dữ liệu refsnes. Đã đăng ký Bản quyền. W3Schools được cung cấp bởi W3.CSS.
W3Schools is Powered by W3.CSS.
Cải thiện bài viết
Lưu bài viết
Hàm python len () Trả về độ dài của chuỗi. & Nbsp; returns the length of the string.
Cú pháp python len ()
Cú pháp: Len (chuỗi) & nbsp;len(string)
Return: nó trả về một số nguyên là độ dài của chuỗi. & Nbsp;It returns an integer which is the length of the string.
Ví dụ về Python Len ()
Phương pháp Len () với chuỗi trong Python.
Python3
>>> print(len('abc')) 3 2>>> print(len('abc')) 3 3 >>> print(len('abc')) 3 4
>>> print(len('abc')) 3 5>>> print(len('abc')) 3 6>>> print(len('abc')) 3 7>>> print(len('abc')) 3 8
Output:
13Ví dụ 1: hàm len () với bộ dữ liệu và chuỗi
Ở đây chúng tôi đang đếm độ dài của các bộ dữ liệu và danh sách.
Python
>>> print(len('abc')) 3 9>>> print(len('abc')) 3 3 >>> print(len('abc')) 3 6>>> print(len('йцы')) # String contains Cyrillic symbols 6 2>>> print(len('йцы')) # String contains Cyrillic symbols 6 3>>> print(len('йцы')) # String contains Cyrillic symbols 6 4>>> print(len('йцы')) # String contains Cyrillic symbols 6 3>>> print(len('йцы')) # String contains Cyrillic symbols 6 6>>> print(len('йцы')) # String contains Cyrillic symbols 6 7
>>> print(len('abc')) 3 5>>> print(len('abc')) 3 6>>> print(len('abc')) 3 7>>> print(len('йцы'.decode('utf8'))) #String contains Cyrillic symbols 3 1
>>> print(len('йцы'.decode('utf8'))) #String contains Cyrillic symbols 3 2>>> print(len('abc')) 3 3 >>> print(len('йцы'.decode('utf8'))) #String contains Cyrillic symbols 3 4>>> print(len('йцы')) # String contains Cyrillic symbols 6 2>>> print(len('йцы')) # String contains Cyrillic symbols 6 3>>> print(len('йцы')) # String contains Cyrillic symbols 6 4>>> print(len('йцы')) # String contains Cyrillic symbols 6 3>>> print(len('йцы')) # String contains Cyrillic symbols 6 6>>> print(len('йцы')) # String contains Cyrillic symbols 6 3>>> print(getsizeof('йцы')) 27 >>> print(getsizeof('йцы'.decode('utf8'))) 32 1
>>> print(len('abc')) 3 5>>> print(len('abc')) 3 6>>> print(len('abc')) 3 7>>> print(getsizeof('йцы')) 27 >>> print(getsizeof('йцы'.decode('utf8'))) 32 6
Output:
3 4Ví dụ 2: Kiểu Python Len ()
Python3
Output:
TypeError: object of type 'bool' has no len()Ví dụ 3: Python Len () với từ điển và bộ
Python3
>>> print(getsizeof('йцы')) 27 >>> print(getsizeof('йцы'.decode('utf8'))) 32 7>>> print(len('abc')) 3 3 >>> print(getsizeof('йцы')) 27 >>> print(getsizeof('йцы'.decode('utf8'))) 32 9130131>>> print(len('йцы')) # String contains Cyrillic symbols 6 2133134135>>> print(len('йцы')) # String contains Cyrillic symbols 6 4137
>>> print(len('abc')) 3 5>>> print(len('abc')) 3 6>>> print(len('abc')) 3 73 41
3 42>>> print(len('abc')) 3 3 3 44>>> print(len('йцы')) # String contains Cyrillic symbols 6 2133>>> print(len('йцы')) # String contains Cyrillic symbols 6 4133>>> print(len('йцы')) # String contains Cyrillic symbols 6 6133>>> print(getsizeof('йцы')) 27 >>> print(getsizeof('йцы'.decode('utf8'))) 32 1
>>> print(len('abc')) 3 5>>> print(len('abc')) 3 6>>> print(len('abc')) 3 7TypeError: object of type 'bool' has no len()6
Output:
2 4Ví dụ 4: Python Len () với các đối tượng tùy chỉnh
Python3
TypeError: object of type 'bool' has no len()7 TypeError: object of type 'bool' has no len()8
TypeError: object of type 'bool' has no len()92 40 2 412 422 43
2 442 422 46>>> print(len('abc')) 3 3 2 48
TypeError: object of type 'bool' has no len()92 40 >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 012 42.
2 44>>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 05 2 42.
>>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 08>>> print(len('abc')) 3 3 >>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 10>>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 11>>> print(len('йцы')) # String contains Cyrillic symbols 6 7
>>> print(len('abc')) 3 5>>> print(len('abc')) 3 6>>> print(len('abc')) 3 7>>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 16
Output:
>>> from sys import getsizeof >>> print(getsizeof('please anwser my question')) 50 0Độ phức tạp về thời gian: & nbsp;
hàm len () & nbsp; có độ phức tạp thời gian của O (1). trong trường hợp trung bình và được khấu haofunction has time complexity of O(1). in average and amortized case