<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Mảng trong Python</title>
<meta name="description" content="Mảng trong Python - Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;mang-trong-python-8613.html">
<meta name="author" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :.">
<meta name="copyright" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :. [admin@nguoicodonvn2008.info]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Mảng trong Python">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;mang-trong-python-8613.html">
<meta property="og:site_name" content=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.">
<meta property="og:url" content="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/mang-trong-python-8613.html">
<link rel="shortcut icon" href="https://www.nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/mang-trong-python-8613.html">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tam-su-tinh-yeu/" title="Tin Tức - Tâm sự tình yêu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tho-suu-tam/" title="Tin Tức - Thơ sưu tầm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/kien-thuc-may-tinh/" title="Tin Tức - Kiến thức máy tính" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">.&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.</h2>
		<p class="pull-right"><a title=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;." href="https://www.nguoicodonvn2008.info/">https://www.nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Mảng trong Python</h1>
		<ul class="list-inline">
			<li>Thứ năm - 04/07/2024 01:29</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
		</div>
				<div class="imghome">
			<img alt="Mảng trong Python" src="https://st.quantrimang.com/photos/image/2017/10/13/lenh-if-python-200-size-80x80-znd.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<h1><br />
<strong style="font-size: 13px;">Mảng trong Python là gì? Cách dùng mảng Python như thế nào?&nbsp;</strong><span style="font-size: 13px;">Hãy cùng Quantrimang.com tìm hiểu nhé!</span></h1>

<p>Tạo mảng trong Python là một tập hợp mục được lưu trữ tại các vị trí bộ nhớ liền kề. Ý tưởng ở đây là lưu trữ nhiều mục cùng loại với nhau. Hành động này hỗ trợ người dùng tính toán vị trí của từng phần tử dễ dàng hơn bằng cách thêm một giá trị bù vào giá trị cơ sở, tức là vị trí bộ nhớ của phần tử đầu tiên trong mảng (thường được biểu thị bằng tên của mảng).</p>

<p>Hiểu đơn giản, bạn có thể tưởng tượng mảng trong Python là một dãy cầu thang nơi mà mỗi bậc được đặt một giá trị. Tại đây, bạn có thể xác định vị trí của bất kỳ người bạn bằng cách đếm số bậc chứa giá trị. Mảng có thể được mô đun tên array xử lý trong Python. Chúng có thể hữu ích khi bạn chỉ phải xử lý một giá trị kiểu dữ liệu. Một người dùng có thể xem list là mảng. Tuy nhiên, họ không thể giới hạn kiểu thành phần được lưu trong danh sách. Nếu tạo mảng bằng mô đun array, toàn bộ thành phần của mảng phải thuộc cùng một kiểu.</p>

<p><strong>Ghi chú:</strong>&nbsp;Nếu bạn muốn tạo mảng thực sự trong Python, bạn cần phải sử dụng cấu trúc dữ liệu mảng của NumPy. Để giải quyết các vấn đề toán học thì mảng NumPy sẽ hiệu quả hơn.</p>

<h2>List và mô-đun array trong Python</h2>

<p>Bạn có thể thao tác với list giống như mảng nhưng không thể ép kiểu phần tử được lưu trữ trong list. Ví dụ:</p>

<pre id="pre0">
a= &#91;1, 3.5, &quot;Hello&quot;&#93;</pre>

<p>Nếu bạn tạo mảng sử dụng mô-đun array, tất cả các phần tử của mảng phải có cùng kiểu số.</p>

<pre id="pre1">
import array as arr
a = arr.array(&#039;d&#039;, &#91;1, 3.5, &quot;Hello&quot;&#93;) // Chạy code này sẽ báo lỗi</pre>

<h2>Làm thế nào để tạo mảng trong Python?</h2>
&nbsp;

<p>Từ những ví dụ trên bạn có thể đoán được, chúng ta cần nhập mô-đun array để tạo các mảng. Ví dụ:</p>

<pre id="pre2">
import array as arr
a = arr.array(&#039;d&#039;,&#91;1.1, 3.5, 4.5&#93;) print(a)</pre>

<p>Code trên tạo mảng có kiểu float. Chữ &#039;d&#039; là mã kiểu, quyết định kiểu của mảng trong quá trình tạo. Dưới đây là những mã kiểu thường dùng:</p>

<table border="1">
		<tr>
			<th>Mã kiểu</th>
			<th>C Type</th>
			<th>Python Type</th>
			<th>Kích thước tối thiểu tính theo byte</th>
		</tr>
	<tbody>
		<tr>
			<td><code>&#039;b&#039;</code></td>
			<td>signed char</td>
			<td>int</td>
			<td>1</td>
		</tr>
		<tr>
			<td><code>&#039;B&#039;</code></td>
			<td>unsigned char</td>
			<td>int</td>
			<td>1</td>
		</tr>
		<tr>
			<td><code>&#039;u&#039;</code></td>
			<td>Py_UNICODE</td>
			<td>Unicode character</td>
			<td>2</td>
		</tr>
		<tr>
			<td><code>&#039;h&#039;</code></td>
			<td>signed short</td>
			<td>int</td>
			<td>2</td>
		</tr>
		<tr>
			<td><code>&#039;H&#039;</code></td>
			<td>unsigned short</td>
			<td>int</td>
			<td>2</td>
		</tr>
		<tr>
			<td><code>&#039;i&#039;</code></td>
			<td>signed int</td>
			<td>int</td>
			<td>2</td>
		</tr>
		<tr>
			<td><code>&#039;I&#039;</code></td>
			<td>unsigned int</td>
			<td>int</td>
			<td>2</td>
		</tr>
		<tr>
			<td><code>&#039;l&#039;</code></td>
			<td>signed long</td>
			<td>int</td>
			<td>4</td>
		</tr>
		<tr>
			<td><code>&#039;L&#039;</code></td>
			<td>unsigned long</td>
			<td>int</td>
			<td>4</td>
		</tr>
		<tr>
			<td><code>&#039;f&#039;</code></td>
			<td>float</td>
			<td>float</td>
			<td>4</td>
		</tr>
		<tr>
			<td><code>&#039;d&#039;</code></td>
			<td>double</td>
			<td>float</td>
			<td>8</td>
		</tr>
	</tbody>
</table>

<p>Ta sẽ không thảo luận về&nbsp;<a href="https://quantrimang.com/hoc/kieu-du-lieu-trong-lap-trinh-c-156102" title="Kiểu dữ liệu trong C">các kiểu dữ liệu C</a>&nbsp;khác nhau trong bài này. Ta sẽ dùng mã &#039;i&#039; cho số nguyên và &#039;d&#039; cho số thập phân trong toàn bài.</p>

<p><strong>Ghi chú:</strong>&nbsp;Mã code &#039;u&#039; cho các ký tự Unicode không còn được chấp nhận từ phiên bản Python 3.3. Tránh sự dụng nó khi có thể.</p>

<h2>Làm sao để truy cập vào các phần tử của mảng?</h2>

<p>Chúng ta sử dụng index để truy cập đến các phần tử của mảng. Index cũng bắt đầu từ 0, tương tự như trong&nbsp;<a href="https://quantrimang.com/gioi-thieu-qua-ve-chuoi-so-list-trong-python-140881#mcetoc_1bs0pu9a20" title="Kiểu dữ liệu List trong Python">list Python</a>.</p>

<pre id="pre3">
import array as arr 
a = arr.array(&#039;i&#039;, &#91;2, 4, 6, 8&#93;) 

print(&quot;Phần tử đầu tiên:&quot;, a&#91;0&#93;) 
print(&quot;Phần tử thứ 2:&quot;, a&#91;1&#93;) 
print(&quot;Phần tử cuối cùng:&quot;, a&#91;-1&#93;)</pre>

<p>Chạy chương trình trên ta được:</p>

<pre id="pre4">
<code>Phần tử đầu tiên: 2
Phần tử thứ 2: 4
Phần tử cuối cùng: 8</code></pre>

<p>Bạn có thể truy cập vào một dải phần tử trong mảng, sử dụng toán tử cắt lát&nbsp;<strong>:</strong>.</p>

<pre id="pre5">
import array as arr 

numbers_list = &#91;5, 85, 65, 15, 95, 52, 36, 25&#93; 
numbers_array = arr.array(&#039;i&#039;, numbers_list) 

print(numbers_array&#91;2:5&#93;) # Phần tử thứ 3 đến 5 
print(numbers_array&#91;:-5&#93;) # Phần tử đầu tiên đến 4 
print(numbers_array&#91;5:&#93;) # Phần tử thứ 6 đến hết 
print(numbers_array&#91;:&#93;) # Phần tử đầu tiên đến cuối cùng</pre>

<p>Khi bạn chạy code trên sẽ nhận được output là:</p>

<pre id="pre6">
<code>array(&#039;i&#039;, &#91;65, 15, 95&#93;)
array(&#039;i&#039;, &#91;5, 85, 65&#93;)
array(&#039;i&#039;, &#91;52, 36, 25&#93;)
array(&#039;i&#039;, &#91;5, 85, 65, 15, 95, 52, 36, 25&#93;)</code></pre>

<h2>Thay đổi, thêm phần tử trong mảng Python</h2>

<p>Mảng có thể thay đổi, các phần tử của nó có thể thay đổi theo cách tương tự như list.</p>
&nbsp;

<pre id="pre7">
import array as arr 
numbers = arr.array(&#039;i&#039;, &#91;1, 1, 2, 5, 7, 9&#93;) 

# thay đổi phần tử đầu tiên 
numbers&#91;0&#93; = 0 
print(numbers) 
# Output: array(&#039;i&#039;, &#91;0, 1, 2, 5, 7, 9&#93;) 

# thay phần tử thứ 3 đến thứ 5 
numbers&#91;2:5&#93; = arr.array(&#039;i&#039;, &#91;4, 6, 8&#93;) 
print(numbers) 
# Output: array(&#039;i&#039;, &#91;0, 1, 4, 6, 8, 9&#93;)</pre>

<p>Bạn có thêm một mục vào list sử dụng append() hoặc thêm vài mục sử dụng&nbsp;<code>extend()</code>:</p>

<pre id="pre8">
import array as arr 

numbers = arr.array(&#039;i&#039;, &#91;3, 5, 7&#93;) 

numbers.append(4) 
print(numbers) # Output: array(&#039;i&#039;, &#91;3, 5, 7, 4&#93;) 

# extend() nối vào cuối mảng 
numbers.extend(&#91;5, 6, 7&#93;) 
print(numbers) # Output: array(&#039;i&#039;, &#91;3, 5, 7, 4, 5, 6, 7&#93;)</pre>

<p>2 mảng cũng có thể nối lại thành một nhờ toán tử +:</p>

<pre id="pre9">
import array as arr 

mang_le = arr.array(&#039;i&#039;, &#91;3, 5, 7&#93;) 
mang_chan = arr.array(&#039;i&#039;, &#91;2, 6, 8&#93;) 

numbers = arr.array(&#039;i&#039;) # tạo mảng trống 
numbers = mang_le + mang_chan 
# Code by quantrimang.com 
print(numbers) 
# Output: array(&#039;i&#039;, &#91;3, 5, 7, 2, 6, 8&#93;)</pre>

<h2>Xóa phần tử của mảng trong Python</h2>

<p>Để xóa một hoặc nhiều phần tử của mảng ta sử dụng lệnh del.</p>

<pre id="pre10">
import array as arr 
number = arr.array(&#039;i&#039;, &#91;1, 3, 3, 5, 7&#93;) 

del number&#91;2&#93; # xóa phần tử thứ 3 
print(number) # Output: array(&#039;i&#039;, &#91;1, 3, 5, 7&#93;) 

del number # xóa toàn bộ mảng 
print(number) # Error: array &#039;number&#039; is not defined</pre>

<p>Có thể sử dụng remove() để xóa mục đã cho hoặc pop() để xóa mục với index cho trước:</p>

<pre id="pre11">
import array as arr 

numbers = arr.array(&#039;i&#039;, &#91;1, 1, 3, 5, 9&#93;) 

numbers.remove(1) 
print(numbers) # Output: array(&#039;i&#039;, &#91;1, 3, 5, 9&#93;) 
print(numbers.pop(2)) # Output: 12 
print(numbers) # Output: array(&#039;i&#039;, &#91;1, 3, 9&#93;)</pre>

<h2>Độ phức tạp khi loại bỏ array trong Python</h2>

<p>Trong mảng của Python, bạn có nhiều cách để in toàn bộ mảng với tất cả các nhân tố, thế nhưng để in một phạm vi nhân tố cụ thể từ mảng, bạn cần dùng toán tử Slice. Toán tử slice được thực hiện trên mảng cùng với dấu hai chấm (:). Để in các nhân tố ngay từ đầu tới mảng dùng &#91;:Index&#93;, để in các nhân tố ở cuối dùng &#91;:-Index&#93;, để in các nhân tố từ index cụ thể cho tới cuối dùng &#91;Index:&#93;, để in các nhân tố trong một phạm vi, dùng &#91;Start Index:End Index&#93; và để in toàn bộ danh sách bằng toán tử slice, dùng &#91;:&#93;. Ngoài ra, để in toàn bộ mảng theo thứ tự đảo ngược, hãy sử dụng &#91;::-1&#93;.</p>

<p>Ví dụ:</p>

<pre id="pre12">
# Ví dụ chứng minh độ phức tạp khi xóa phần tử trong mảng 
# Chia tách phần tử trong mảng
# Nhập mô đun mảng
import array as arr
# Tạo danh sách
l = &#91;1, 2, 3, 4, 5, 6, 7, 8, 9, 10&#93;
a = arr.array(&#039;i&#039;, l)
print(&quot;Initial Array: &quot;)
for i in (a):
    print(i, end=&quot; &quot;)
# In các nhân tố của một mảng
# Dùng toán tử Slice 
Sliced_array = a&#91;3:8&#93;
print(&quot;\nSlicing elements in a range 3-8: &quot;)
print(Sliced_array)
# In các nhân tố từ điểm xác định trước tới cuối
Sliced_array = a&#91;5:&#93;
print(&quot;\nElements sliced from 5th &quot;
      &quot;element till the end: &quot;)
print(Sliced_array)
# In các nhân tố từ điểm bắt đầu tới cuối
Sliced_array = a&#91;:&#93;
print(&quot;\nPrinting all elements using slice operation: &quot;)
print(Sliced_array)</pre>
&nbsp;

<p><strong>Kết quả:</strong></p>

<pre id="pre13">
<code>Mảng ban đầu:
1 2 3 4 5 6 7 8 9 10
Tách các nhân tố trong mảng 3-8:
array(&#039;i&#039;, &#91;4, 5, 6, 7, 8&#93;)

Tách nhân tố trong mảng từ thứ 5 tới cuối:
array(&#039;i&#039;, &#91;6, 7, 8, 9, 10&#93;)

In tất cả các nhân tố bằng toán tử slice:
array(&#039;i&#039;, &#91;1, 2, 3, 4, 5, 6, 7, 8, 9, 10&#93;)</code></pre>

<h2>Lặp các thành phần trong mảng</h2>

<p>Bạn có thể dùng vòng lặp&nbsp;<code>for in</code>&nbsp;qua tất cả các thành phần của một mảng. Ví dụ:<br />
In từng mục trong mảng cars:</p>

<pre id="pre14">
for x in cars:
  print(x)</pre>

<h2>Các phương thức mảng trong Python</h2>

<p>Python có sẵn một bộ các phương thức cho bạn dùng trên lists/arrays (danh sách/mảng).</p>

<table border="1" cellpadding="2" cellspacing="2">
	<tbody>
		<tr>
			<td>
			<p><strong>Phương thức</strong></p>
			</td>
			<td>
			<p><strong>Mô tả</strong></p>
			</td>
		</tr>
		<tr>
			<td>
			<p><a>append()</a></p>
			</td>
			<td>
			<p>Thêm một phần tử vào cuối danh sách</p>
			</td>
		</tr>
		<tr>
			<td>
			<p><a>clear()</a></p>
			</td>
			<td>
			<p>Xóa tất cả phần tử khỏi danh sách</p>
			</td>
		</tr>
		<tr>
			<td>
			<p><a>copy()</a></p>
			</td>
			<td>
			<p>Trả về một bản sao danh sách</p>
			</td>
		</tr>
		<tr>
			<td>
			<p><a>count()</a></p>
			</td>
			<td>
			<p>Trả về số của các thành phần với giá trị cụ thể</p>
			</td>
		</tr>
		<tr>
			<td>
			<p><a>extend()</a></p>
			</td>
			<td>
			<p>Thêm các phần tử vào danh sách (hoặc có thể lặp lại bất kỳ) vào cuối danh sách hiện tại</p>
			</td>
		</tr>
		<tr>
			<td>
			<p><a>index()</a></p>
			</td>
			<td>
			<p>Trả về chỉ mục của phần tử đầu tiên với giá trị cụ thể</p>
			</td>
		</tr>
		<tr>
			<td>
			<p><a>insert()</a></p>
			</td>
			<td>
			<p>Thêm một phần tử vào vị trí cụ thể</p>
			</td>
		</tr>
		<tr>
			<td>
			<p><a>pop()</a></p>
			</td>
			<td>
			<p>Loại bỏ phần tử ở vị trí nhất định</p>
			</td>
		</tr>
		<tr>
			<td>
			<p><a>remove()</a></p>
			</td>
			<td>
			<p>Loại bỏ mục đầu tiên với giá trị cụ thể</p>
			</td>
		</tr>
		<tr>
			<td>
			<p><a>reverse()</a></p>
			</td>
			<td>
			<p>Đảo ngược thứ tự của danh sách</p>
			</td>
		</tr>
		<tr>
			<td>
			<p><a>sort()</a></p>
			</td>
			<td>
			<p>Phân loại danh sách</p>
			</td>
		</tr>
	</tbody>
</table>

<p><strong>Lưu ý:</strong>&nbsp;Python không có sẵn hỗ trợ cho các mảng (array), thay vào đó, bạn có thể dùng Python Lists.</p>

<h3>So sánh Lists và Arrays trong Python</h3>

<p>Trong Python, bạn có thể xem list là mảng. Tuy nhiên, bạn không thể hạn chế kiểu phần tử được lưu trong một list. Ví dụ:</p>

<pre id="pre15">
# phần tử của các kiểu khác nhau
a = &#91;1, 3.5, &quot;Hello&quot;&#93;</pre>

<p>Nếu tạo mảng bằng mô đun array, toàn bộ phần tử của mảng phải có cùng kiểu số.</p>

<pre id="pre16">
import array as arr
# Error
a = arr.array(&#039;d&#039;, &#91;1, 3.5, &quot;Hello&quot;&#93;)</pre>

<p>Kết quả:</p>

<pre id="pre17">
Traceback (most recent call last):
File &quot;&lt;string&gt;&quot;, line 3, in &lt;module&gt;
a = arr.array(&#039;d&#039;, &#91;1, 3.5, &quot;Hello&quot;&#93;)
TypeError: must be real number, not str</pre>

<h2>Khi nào thì sử dụng mảng?</h2>

<p>List linh hoạt hơn mảng, chúng có thể lưu trữ phần tử với nhiều kiểu dữ liệu khác nhau, bao gồm cả chuỗi. List cũng nhanh hơn mảng, vậy thì tại sao lại cần dùng mảng? Nếu bạn phải thực hiện những phép tính toán toán học trên mảng và ma trận, bạn nên sử dụng thư viện NumPy.</p>

<p><strong>Vậy các trường hợp dùng mảng được tạo từ mô đun mảng Python là gì?</strong></p>

<p>Kiểu&nbsp;<code>array.array</code>&nbsp;chỉ là một wrapper nhỏ trên mảng C mà cung cấp khả năng lưu trữ hiệu quả về không gian của kiểu dữ liệu loại C. Nếu cần phân bổ một mảng mà bạn biết là sẽ không đổi thì các mảng có thể chạy nhanh hơn và dùng ít bộ nhớ hơn list.</p>

<p>Trừ khi không thực sự cần mảng (có thể cần mô đun mảng để can thiệp vào code C), sử dụng mô đun mảng ở đây không cần thiết.</p>
		</div>
				<div id="author">
						<p>
				<strong>Nguồn tin:</strong>
				Quantrimang.com
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/mang-trong-python-8613.html" title="Mảng trong Python">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/mang-trong-python-8613.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; .&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.
		</div>
		<div id="contact">
			<a href="mailto:admin@nguoicodonvn2008.info">admin@nguoicodonvn2008.info</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://www.nguoicodonvn2008.info/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
		</script>
		<div class="car-top">
  <span><img src="https://www.nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<script src="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4c_e856T",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://www.nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://www.nguoicodonvn2008.info",
            "logo": "https://www.nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js"></script>
<script type="text/javascript">
var $scrolltop = $('.car-top');
$scrolltop.on('click', function () {
    $('html,body').animate({
        scrollTop: 0
    }, 800);
    $(this).addClass("car-run");
    setTimeout(function(){ $scrolltop.removeClass('car-run');}, 1000);
    return false;
});
$(window).on('scroll', function ()
{ 
    if($(window).scrollTop() >= 200)
    {
        $scrolltop.addClass("show");
        $scrolltop.addClass("car-down");
    }
    else
    {
       $scrolltop.removeClass("show");
       setTimeout(function(){ $scrolltop.removeClass('car-down');}, 300);
    }
});
</script>
</body>
</html>