杰瑞科技汇

Python中datalength如何计算?

Of course! In Python, "datalength" isn't a single built-in function, but the concept of finding the length of data is fundamental and can be done in several ways depending on the type of data you're working with.

Python中datalength如何计算?-图1
(图片来源网络,侵删)

Here’s a complete guide covering the most common data types.


For Strings (Text)

The primary way to get the length of a string is using the built-in len() function. It returns the number of characters in the string.

my_string = "Hello, World!"
# Get the length of the string
length = len(my_string)
print(f"The string is: '{my_string}'")
print(f"The length of the string is: {length}")

Output:

The string is: 'Hello, World!'
The length of the string is: 13

Important Note on Unicode: Python 3 strings are Unicode by default. len() correctly counts characters, including special characters, emojis, and letters from different languages.

Python中datalength如何计算?-图2
(图片来源网络,侵删)
# Example with multi-byte characters (emojis, accents)
unicode_string = "Café 🚀"
length = len(unicode_string)
print(f"The string is: '{unicode_string}'")
print(f"The length of the string is: {length}") # 'Café' is 4 chars, ' ' is 1, '🚀' is 1. Total 6.

Output:

The string is: 'Café 🚀'
The length of the string is: 6

For Lists, Tuples, and Sets (Collections)

len() also works for collections of items. It returns the number of elements in the collection, not the total size of the data in bytes.

Lists (Ordered, Mutable)

my_list = [10, 20, 30, "apple", "banana"]
length = len(my_list)
print(f"The list is: {my_list}")
print(f"The number of elements in the list is: {length}")

Output:

The list is: [10, 20, 30, 'apple', 'banana']
The number of elements in the list is: 5

Tuples (Ordered, Immutable)

my_tuple = (10, 20, 30, "apple", "banana")
length = len(my_tuple)
print(f"The number of elements in the tuple is: {length}")

Output:

Python中datalength如何计算?-图3
(图片来源网络,侵删)
The number of elements in the tuple is: 5

Sets (Unordered, Unique Elements)

my_set = {10, 20, 30, 40, 40} # Note: 40 is a duplicate
length = len(my_set)
print(f"The set is: {my_set}")
print(f"The number of unique elements in the set is: {length}")

Output:

The set is: {40, 10, 20, 30}
The number of unique elements in the set is: 4

For Dictionaries (Key-Value Pairs)

When you use len() on a dictionary, it returns the number of key-value pairs.

my_dict = {
    "name": "Alice",
    "age": 30,
    "city": "New York"
}
length = len(my_dict)
print(f"The dictionary is: {my_dict}")
print(f"The number of key-value pairs is: {length}")

Output:

The dictionary is: {'name': 'Alice', 'age': 30, 'city': 'New York'}
The number of key-value pairs is: 3

For Bytes and Bytearrays (Raw Binary Data)

This is where the concept of "length" gets more specific. For raw binary data, len() returns the number of bytes.

bytes (Immutable)

# A bytes literal, often used for encoding strings
my_bytes = b"hello"
length = len(my_bytes)
print(f"The bytes object is: {my_bytes}")
print(f"The length in bytes is: {length}")

Output:

The bytes object is: b'hello'
The length in bytes is: 5

bytearray (Mutable)

# A mutable version of bytes
my_bytearray = bytearray(b"world")
length = len(my_bytearray)
print(f"The bytearray object is: {my_bytearray}")
print(f"The length in bytes is: {length}")

Output:

The bytearray object is: bytearray(b'world')
The length in bytes is: 5

For NumPy Arrays

When working with numerical data in the numpy library, len() behaves differently. For a 1D array, it returns the number of elements. For a multi-dimensional array, it returns the size of the first dimension.

import numpy as np
# 1D array
arr_1d = np.array([1, 2, 3, 4, 5])
len_1d = len(arr_1d)
print(f"1D array: {arr_1d}, len(): {len_1d}") # Expected: 5
# 2D array (matrix)
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
len_2d = len(arr_2d)
print(f"2D array:\n{arr_2d}\nlen(): {len_2d}") # Expected: 2 (number of rows)

Output:

1D array: [1 2 3 4 5], len(): 5
2D array:
[[1 2 3]
 [4 5 6]]
len(): 2

To get the total number of elements in a NumPy array, use the .size attribute:

total_elements = arr_2d.size
print(f"Total elements in 2D array (arr_2d.size): {total_elements}") # Expected: 6

For Pandas DataFrames and Series

The pandas library has its own conventions for "length".

pandas.Series (1D labeled array)

len() and the .size attribute both return the number of rows (elements).

import pandas as pd
s = pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd'])
length = len(s)
size = s.size
print(f"Series:\n{s}")
print(f"len(s): {length}")
print(f"s.size: {size}")

Output:

Series:
a    10
b    20
c    30
d    40
dtype: int64
len(s): 4
s.size: 4

pandas.DataFrame (2D table)

len() and .shape[0] return the number of rows. The .shape attribute is a tuple (rows, columns).

data = {'col1': [1, 2, 3], 'col2': [4, 5, 6]}
df = pd.DataFrame(data)
num_rows = len(df)
shape = df.shape
print(f"DataFrame:\n{df}")
print(f"len(df): {num_rows}") # Number of rows
print(f"df.shape: {shape}")   # (rows, columns)

Output:

DataFrame:
   col1  col2
0     1     4
1     2     5
2     3     6
len(df): 3
df.shape: (3, 2)

Summary Table

Data Type Function/Attribute What it Returns Example
String len() Number of characters len("hi") -> 2
List / Tuple len() Number of elements len([1, 2]) -> 2
Set len() Number of unique elements len({1, 2, 2}) -> 2
Dict len() Number of key-value pairs len({'a':1}) -> 1
Bytes len() Number of bytes len(b"hi") -> 2
NumPy Array len() Size of the first dimension (rows for 2D) len(np.array([[1],[2]])) -> 2
NumPy Array .size Total number of elements in the array np.array([[1],[2]]).size -> 2
Pandas Series len() / .size Number of rows (elements) len(pd.Series([1,2])) -> 2
Pandas DataFrame len() / .shape[0] Number of rows len(pd.DataFrame({'a':[1]})) -> 1
分享:
扫描分享到社交APP
上一篇
下一篇