* ON LIST in bằng Python là gì?

Giả sử chúng ta cần ghi lại tuổi của 5 học sinh. Thay vì tạo 5 biến riêng biệt, chúng ta chỉ cần tạo một danh sách

* ON LIST in bằng Python là gì?
liệt kê các yếu tố

Tạo một danh sách Python

Một danh sách được tạo bằng Python bằng cách đặt các mục bên trong

languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++
5, được phân tách bằng dấu phẩy. Ví dụ,

# A list with 3 integers
numbers = [1, 2, 5]

print(numbers)

# Output: [1, 2, 5]

Ở đây, chúng tôi đã tạo một danh sách có tên số với 3 mục số nguyên

Một danh sách có thể có bất kỳ số lượng mục nào và chúng có thể thuộc các loại khác nhau (số nguyên, số float, chuỗi, v.v. ). Ví dụ,

# empty list
my_list = []

# list with mixed data types
my_list = [1, "Hello", 3.4]

Truy cập các phần tử danh sách Python

Trong Python, mỗi mục trong danh sách được liên kết với một số. Số được gọi là chỉ số danh sách

Chúng ta có thể truy cập các phần tử của một mảng bằng cách sử dụng số chỉ mục (0, 1, 2…). Ví dụ,

languages = ["Python", "Swift", "C++"]

# access item at index 0
print(languages[0])   # Python

# access item at index 2
print(languages[2])   # C++

Trong ví dụ trên, chúng tôi đã sử dụng khả năng hiểu danh sách để tạo danh sách với mỗi mục được tăng theo lũy thừa của 2. Chú ý mã,

Để in một danh sách trong Python, chúng ta có thể truyền danh sách dưới dạng đối số cho hàm print(). Hàm print() chuyển đổi danh sách thành một chuỗi và in nó ra đầu ra của bàn điều khiển

Nếu chúng ta cần in từng phần tử của danh sách, chúng ta có thể sử dụng câu lệnh vòng lặp như vòng lặp while hoặc for, lặp qua các phần tử của danh sách và in chúng ra đầu ra của bàn điều khiển

In danh sách ra đầu ra bàn điều khiển

Trong chương trình sau, chúng tôi lấy một danh sách tên các loại trái cây và in danh sách này bằng câu lệnh in

Chương trình Python

#initialise a list
myList = ['apple', 'banana', 'cherry']

#print list to console output
print(myList)

đầu ra

['apple', 'banana', 'cherry']

In danh sách, từng phần tử, ra đầu ra của bàn điều khiển

Trong chương trình sau, chúng tôi lấy một danh sách tên các loại trái cây và in danh sách này, từng phần tử, sử dụng vòng lặp for và câu lệnh in

Chương trình Python

#initialise a list
myList = ['apple', 'banana', 'cherry']

#print list to console output element by element
for element in myList :
	print(element)

đầu ra

apple
banana
cherry

Tóm lược

Trong hướng dẫn Ví dụ về Python này, chúng ta đã học cách in một danh sách đã cho ra đầu ra của bàn điều khiển, sử dụng hàm print()

Mảng - đôi khi được gọi là danh sách - là một cấu trúc dữ liệu cơ bản nhưng mạnh mẽ có trong hầu hết các ngôn ngữ lập trình. Chúng giúp dễ dàng lưu trữ dữ liệu được sắp xếp, sau đó cho phép truy cập dữ liệu đó nhanh chóng và dễ dàng bằng cách sử dụng chỉ mục số. Trong Python, mảng được gọi là danh sách và chúng được biết đến là dễ sử dụng hơn trong Python so với các ngôn ngữ lập trình khác

In hiệu quả tất cả các thành phần dữ liệu được lưu trữ trong danh sách có thể là một nhiệm vụ rất hữu ích, nếu không muốn nói là cần thiết cho một số quy trình công việc

Trong bài viết này, chúng ta sẽ khám phá nhiều cách khác nhau để in danh sách trong Python cùng với lợi ích của từng cách.

Danh sách in Python với hàm >>> x = [1, 2, 3, 4] >>> print(*x) 1 2 3 4 >>> 2

Cách đơn giản nhất để in danh sách là trực tiếp với hàm

>>> x = [1, 2, 3, 4]
>>> print(*x)
1 2 3 4
>>> 
2

>>> x = [1, 2, 3, 4]
>>> print(x)
[1, 2, 3, 4]

Phương thức này in cú pháp danh sách Python thô (bao gồm cả dấu ngoặc và dấu phẩy), nhưng bạn có thể sử dụng ký hiệu

>>> x = [1, 2, 3, 4]
>>> print(*x)
1 2 3 4
>>> 
5 để in danh sách một cách trang nhã hơn nhiều. Ví dụ

________số 8_______

Ở đây, chỉ các mục trong danh sách được in theo thứ tự có dấu cách ngăn cách chúng

Phương pháp in danh sách đơn giản này dường như hoạt động tốt, vì vậy người ta có thể thắc mắc tại sao không sử dụng phương pháp này để in danh sách mọi lúc. Phương pháp này hoạt động tốt đối với các danh sách nhỏ, đơn giản như danh sách được hiển thị ở trên. Nhưng nếu bạn sử dụng một danh sách có dữ liệu lớn hơn và phức tạp hơn, thì việc hiểu kết quả đầu ra sẽ nhanh chóng trở nên khó khăn.

Ví dụ: giả sử bạn có một danh sách gồm nhiều chuỗi dài

>>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
>>> print(longStrings)
['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 

Trong ví dụ trên, thật khó để tìm ra nơi một chuỗi bắt đầu và kết thúc, và điều này càng trở nên khó khăn hơn đối với các danh sách lớn hơn. Bạn có thể in loại danh sách này theo cách có tổ chức hơn, với ký hiệu

>>> x = [1, 2, 3, 4]
>>> print(*x)
1 2 3 4
>>> 
5 và đối số
>>> x = [1, 2, 3, 4]
>>> print(*x)
1 2 3 4
>>> 
7 trong hàm print(). Đối số
>>> x = [1, 2, 3, 4]
>>> print(*x)
1 2 3 4
>>> 
7 cho phép bạn thay thế khoảng trắng mặc định phân tách các mục trong hàm print() bằng bất kỳ ký tự nào khác

>>> print(*longStrings, sep='\n------\n')
first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string 
------
second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string 
------
third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string 
>>> 

Giờ đây, mỗi mục trong danh sách được phân tách theo cách giúp đầu ra dễ đọc hơn nhiều

In danh sách có vòng lặp

Một cách khác để in danh sách trong Python là sử dụng vòng lặp.

>>> x = [1, 2, 3, 4]
>>> print(*x)
1 2 3 4
>>> 
9 vòng lặp là tùy chọn đơn giản và trực quan hơn, nhưng bạn cũng có thể sử dụng vòng lặp
>>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
>>> print(longStrings)
['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
0. Vì vậy, chúng tôi sẽ khám phá cả hai cách ở đây

Với vòng lặp for, bạn có thể in danh sách bằng cách lặp qua từng mục và in nó

>>> 
>>> x = [1, 2, 3, 4]
>>> 
>>> for item in x:
..     print(item)
.. 
1
2
3
4
>>> 

Điều này cho phép bạn định dạng riêng từng mục sẽ được in ra như thế nào. Tuy nhiên, mỗi mục sẽ luôn được in trên một dòng mới. Tuy nhiên, bạn có thể khắc phục điều này bằng đối số

>>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
>>> print(longStrings)
['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
1 trong hàm print(). Đối số này cho phép bạn thay thế ký tự '\n' (dòng mới), được mặc định sau mỗi phần tử đầu ra print(), bằng một chuỗi khác

>>> for item in x:
..     print(item, end=" - ")
.. 
1 - 2 - 3 - 4 - >>> 

Bạn cũng có thể in danh sách có vòng lặp for bằng cách lặp qua phạm vi chỉ số

>>> for idx in range(len(x)):
..     print(x[idx])
.. 
1
2
3
4
>>> 

Điều này có thể hữu ích khi bạn muốn chỉ định cách bạn in các mục trong danh sách dựa trên giá trị chỉ mục của chúng

Chúng ta có thể sử dụng ý tưởng tương tự về việc sử dụng các chỉ số để in các mục và thay vào đó sử dụng vòng lặp while

>>> idx = 0
>>> while idx < len(x):
..     print(x[idx])
..     idx += 1
.. 
1
2
3
4
>>>

Đầu tiên, biến

>>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
>>> print(longStrings)
['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
2 được đặt thành 0, sau đó vòng lặp while in giá trị của
>>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
>>> print(longStrings)
['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
3 tại chỉ mục
>>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
>>> print(longStrings)
['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
2 và tăng
>>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
>>> print(longStrings)
['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
2 lên 1. Vòng lặp kết thúc khi
>>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
>>> print(longStrings)
['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
2 đạt đến độ dài của danh sách
>>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
>>> print(longStrings)
['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
3

In danh sách với hàm join()

Một cách khác để in danh sách là sử dụng phương thức

>>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
>>> print(longStrings)
['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
8 để nối các mục trong danh sách thành một chuỗi, sau đó in ra

>>> y = ['a', 'b', 'c', 'd']
>>> joinedList = ' '.join(y)
>>> print(joinedList)
a b c d
>>> 

Ở đây, các mục trong danh sách

>>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
>>> print(longStrings)
['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
9 được nối với nhau bằng khoảng cách giữa chúng. Bạn có thể thay thế khoảng trắng bằng bất kỳ chuỗi nào bạn muốn sử dụng làm dấu phân cách

Tuy nhiên, có một lưu ý khi sử dụng phương thức join(). tham gia () là một phương thức chuỗi, có nghĩa là tất cả các phần tử trong danh sách phải chuỗi. Nếu bạn biết rằng các mục trong danh sách không phải là tất cả các chuỗi hoặc bạn không chắc chắn, bạn có thể sử dụng hàm

>>> print(*longStrings, sep='\n------\n')
first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string 
------
second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string 
------
third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string 
>>> 
0 để tận dụng phép nối()

Với hàm map(), bạn có thể ánh xạ tất cả các mục trong danh sách tới hàm

>>> print(*longStrings, sep='\n------\n')
first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string 
------
second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string 
------
third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string 
>>> 
1 sẽ chuyển đổi các mục thành chuỗi trước khi chúng được nối

>>> x = [1, 2, 'a', 'b']
>>> joinedList = ' - '.join(map(str, x))
>>> print(joinedList)
1 - 2 - a - b
>>> 

Ở đây, mặc dù danh sách

>>> longStrings = ['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
>>> print(longStrings)
['first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string ', 'second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string ', 'third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string ']
>>> 
3 bao gồm cả số nguyên và chuỗi, các mục được chuyển đổi thành kiểu dữ liệu chuỗi với map() rồi nối

Ưu điểm chính của việc sử dụng phương thức nối () để in danh sách là bạn có thể lưu trữ đầu ra được in vào một biến trước đó. Điều này có thể hữu ích trong một số tình huống nhất định, khi bạn muốn làm điều gì đó khác với đầu ra hơn là in nó

In ngược danh sách

Một cách khác mà bạn có thể muốn in một danh sách là đảo ngược. Đối với điều này, python có phương thức

>>> print(*longStrings, sep='\n------\n')
first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string 
------
second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string 
------
third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string 
>>> 
3 và hàm
>>> print(*longStrings, sep='\n------\n')
first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string 
------
second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string 
------
third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string 
>>> 
4. Phương thức reverse() sẽ đảo ngược danh sách ban đầu tại chỗ - danh sách ban đầu sẽ được sửa đổi. Hàm Reverse() sẽ trả về một trình vòng lặp sẽ xuất các mục theo thứ tự đảo ngược, khiến danh sách ban đầu không bị ảnh hưởng

>>> x = [1, 2, 3, 4]
>>> print(*x)
1 2 3 4
>>> 
0

Với phương thức reverse() bạn có thể đảo ngược danh sách ban đầu và sau đó sử dụng các kỹ thuật được trình bày trong phần trước để in danh sách tương ứng. Đối với hàm Reverse(), bạn có thể chuyển đổi đầu ra của trình vòng lặp thành một danh sách bằng hàm

>>> print(*longStrings, sep='\n------\n')
first long string first long string first long string first long string first long string first long string first long string first long string first long string first long string 
------
second long string second long string second long string second long string second long string second long string second long string second long string second long string second long string 
------
third long string third long string third long string third long string third long string third long string third long string third long string third long string third long string 
>>> 
5 và sau đó in danh sách. Tuy nhiên, khi sử dụng vòng lặp for để in danh sách, bạn có thể lặp trực tiếp thông qua đầu ra của trình lặp đảo ngược()

>>> x = [1, 2, 3, 4]
>>> print(*x)
1 2 3 4
>>> 
1

Đảo ngược danh sách và sau đó in đầu ra của nó có thể rất hữu ích trong các tình huống xử lý danh sách đã sắp xếp

Tóm lược

Trong bài viết này, chúng ta đã thảo luận về nhiều cách khác nhau để in danh sách trong Python và lợi ích của từng phương pháp

Đầu tiên, bạn đã thấy cách cơ bản nhất để in trực tiếp danh sách bằng hàm print(). Sau đó, chúng tôi đã thảo luận tại sao cách cơ bản có thể không đủ. Tiếp theo, bạn đã thấy cách bạn có thể in danh sách bằng vòng lặp for và vòng lặp while. Sau đó, bạn đã thấy cách bạn có thể sử dụng phương thức nối() để in một danh sách và hiểu tại sao nó có thể có lợi trong một số tình huống nhất định. Cuối cùng, bạn đã biết cách in đảo ngược một danh sách bằng hàm Reverse() và phương thức Reverse()

Bước tiếp theo

Nếu bạn muốn tìm hiểu thêm về những kiến ​​thức cơ bản về viết mã, lập trình và phát triển phần mềm, hãy xem Sách hướng dẫn những điều cơ bản về mã hóa dành cho nhà phát triển của chúng tôi, nơi chúng tôi đề cập đến các ngôn ngữ, khái niệm và công cụ thiết yếu mà bạn sẽ cần để trở thành một nhà phát triển chuyên nghiệp

Cảm ơn và mã hóa hạnh phúc. Chúng tôi hy vọng bạn thích bài viết này. Nếu bạn có bất kỳ câu hỏi hoặc nhận xét nào, vui lòng liên hệ với jacob@initialcommit. io

* có nghĩa là gì trong danh sách Python?

Danh sách Python cũng bao gồm toán tử *, cho phép bạn tạo danh sách mới với các phần tử được lặp lại số lần đã chỉ định .

in * có nghĩa là gì trong Python?

Hàm print() in thông báo đã chỉ định ra màn hình hoặc thiết bị đầu ra tiêu chuẩn khác . Thông báo có thể là một chuỗi, hoặc bất kỳ đối tượng nào khác, đối tượng sẽ được chuyển thành chuỗi trước khi ghi ra màn hình.

Việc sử dụng * trong câu lệnh in trong Python là gì?

print() Cú pháp trong Python . Hãy phá vỡ nó. *đối tượng có thể không, một hoặc nhiều giá trị dữ liệu được in và nó có thể thuộc bất kỳ loại dữ liệu nào . Các đối tượng được chuyển đổi thành một chuỗi trước khi in.

Dòng * có nghĩa là gì trong Python?

Dấu * đang được dùng để lấy thêm lợi nhuận từ câu lệnh tách . Lưu câu trả lời này. Hiển thị hoạt động trên bài đăng này. Trong Python, nó được gọi là toán tử giải nén, nó được giới thiệu trong Python 3 (thao tác cụ thể này).