杰瑞科技汇

Python array如何转为list?

Of course! Converting a Python array to a list is a common task, especially when working with data from libraries like NumPy.

Python array如何转为list?-图1
(图片来源网络,侵删)

The key is to understand that "array" can mean two different things in Python:

  1. A Python list: This is the built-in, flexible, but potentially slow array-like structure.
  2. A NumPy array: This is a powerful, high-performance array from the popular NumPy library.

The method you use depends on which type of "array" you're starting with.


Converting a NumPy Array to a List (Most Common Case)

If you're working with numerical or scientific computing, you're almost certainly using NumPy. Converting a NumPy array to a Python list is straightforward using the .tolist() method.

Why Convert?

  • Compatibility: Many standard Python libraries and functions expect lists, not NumPy arrays.
  • Flexibility: Lists can hold mixed data types (e.g., numbers and strings), while NumPy arrays are homogeneous (all elements must be the same type).
  • Simplicity: For small-scale data, Python's built-in list is often easier to work with.

Example:

# First, make sure you have NumPy installed
# pip install numpy
import numpy as np
# 1. Create a NumPy array
my_numpy_array = np.array([10, 20, 30, 40, 50])
print(f"Original NumPy Array: {my_numpy_array}")
print(f"Type of original: {type(my_numpy_array)}")
print("-" * 20)
# 2. Convert the NumPy array to a Python list
my_list = my_numpy_array.tolist()
print(f"Converted List: {my_list}")
print(f"Type of converted: {type(my_list)}")

Output:

Python array如何转为list?-图2
(图片来源网络,侵删)
Original NumPy Array: [10 20 30 40 50]
Type of original: <class 'numpy.ndarray'>
--------------------
Converted List: [10, 20, 30, 40, 50]
Type of converted: <class 'list'>

Multi-dimensional Arrays

The .tolist() method works perfectly for multi-dimensional arrays as well, converting them into nested lists.

import numpy as np
# Create a 2D NumPy array
multi_array = np.array([[1, 2, 3], [4, 5, 6]])
# Convert to a list of lists
multi_list = multi_array.tolist()
print(f"Original 2D Array:\n{multi_array}\n")
print(f"Converted 2D List: {multi_list}")
print(f"Type: {type(multi_list)}")

Output:

Original 2D Array:
[[1 2 3]
 [4 5 6]]
Converted 2D List: [[1, 2, 3], [4, 5, 6]]
Type: <class 'list'>

Converting a Python array.array to a List

Python has a built-in array module that creates more memory-efficient arrays than lists, but they are less flexible. You convert these to lists using the built-in list() constructor.

Example:

import array
# 1. Create an array.array object
# The first argument 'i' specifies the type code for integers.
my_array = array.array('i', [100, 200, 300, 400])
print(f"Original array.array: {my_array}")
print(f"Type of original: {type(my_array)}")
print("-" * 20)
# 2. Convert the array.array to a Python list
my_list = list(my_array)
print(f"Converted List: {my_list}")
print(f"Type of converted: {type(my_list)}")

Output:

Python array如何转为list?-图3
(图片来源网络,侵删)
Original array.array: array('i', [100, 200, 300, 400])
Type of original: <class 'array.array'>
--------------------
Converted List: [100, 200, 300, 400]
Type of converted: <class 'list'>

"Converting" a Python list (It's already a list!)

This is a common point of confusion. If you have a Python list, you don't need to convert it. It's already a list!

# This is already a Python list
my_list = [1, 'a', 3.14, True]
print(f"My object: {my_list}")
print(f"Type: {type(my_list)}")
# If you try to use list() on it, you just get a copy of the list
new_list = list(my_list)
print(f"\nResult of list(my_list): {new_list}")
print(f"Type: {type(new_list)}")

Output:

My object: [1, 'a', 3.14, True]
Type: <class 'list'>
Result of list(my_list): [1, 'a', 3.14, True]
Type: <class 'list'>

Summary Table

Starting Type Conversion Method Example Result Type
NumPy ndarray .tolist() method np_array.tolist() list
array.array list() constructor list(array_obj) list
Python list Not needed (it's already a list) my_list list

Key Takeaway

  • If you're using NumPy, use .tolist().
  • If you're using the built-in array module, use the list() constructor.
  • If you have a standard list, you're already done
分享:
扫描分享到社交APP
上一篇
下一篇