📜  listas zh python (1)

📅  最后修改于: 2023-12-03 15:17:24.572000             🧑  作者: Mango

Listas in Python

Introduction

A list is a common data type in Python used to store a collection of items. It is a ordered, mutable and iterable container. Lists are very versatile and can be used for a wide range of tasks, from simple to complex data manipulation.

Creating a List

To create a list in Python, we can use square brackets [] and separate each item with a comma. For example:

my_list = [1, 2, 3, 4, 5]

We can also use the list() function to create a list from another iterable object, like a string or a tuple. For example:

my_string = "hello"
my_list = list(my_string)
print(my_list)
# Output: ['h', 'e', 'l', 'l', 'o']
Operations on Lists
Retrieving Items

We can access an item in a list by using its index, which starts at 0. For example:

my_list = [1, 2, 3, 4, 5]
print(my_list[0])
# Output: 1

We can also use negative indexing to retrieve an item from the end of the list. For example:

my_list = [1, 2, 3, 4, 5]
print(my_list[-1])
# Output: 5
Slicing Lists

We can slice a list to retrieve a portion of it. Slicing is done by specifying a starting index and an ending index, separated by a colon (:). For example:

my_list = [1, 2, 3, 4, 5]
print(my_list[1:3])
# Output: [2, 3]

If we omit the starting index, it defaults to 0. If we omit the ending index, it defaults to the length of the list. For example:

my_list = [1, 2, 3, 4, 5]
print(my_list[:3])
# Output: [1, 2, 3]

print(my_list[3:])
# Output: [4, 5]
Modifying Lists

We can modify a list by changing, adding or removing items.

To change an item in a list, we can use indexing like this:

my_list = [1, 2, 3, 4, 5]
my_list[2] = 6
print(my_list)
# Output: [1, 2, 6, 4, 5]

To add an item to the end of a list, we can use the append() method. For example:

my_list = [1, 2, 3, 4, 5]
my_list.append(6)
print(my_list)
# Output: [1, 2, 3, 4, 5, 6]

To add an item to a specific position in a list, we can use the insert() method. For example:

my_list = [1, 2, 3, 4, 5]
my_list.insert(2, 6)
print(my_list)
# Output: [1, 2, 6, 3, 4, 5]

To remove an item from a list, we can use the remove() method. For example:

my_list = [1, 2, 3, 4, 5]
my_list.remove(3)
print(my_list)
# Output: [1, 2, 4, 5]
Other List Operations

We can use the len() function to find the length of a list. For example:

my_list = [1, 2, 3, 4, 5]
print(len(my_list))
# Output: 5

We can use the in keyword to check if an item is in a list. For example:

my_list = [1, 2, 3, 4, 5]
print(3 in my_list)
# Output: True

We can use the extend() method to add multiple items to a list at once. For example:

my_list = [1, 2, 3, 4, 5]
my_list.extend([6, 7])
print(my_list)
# Output: [1, 2, 3, 4, 5, 6, 7]

We can use the sort() method to sort a list in ascending order. For example:

my_list = [3, 1, 4, 2, 5]
my_list.sort()
print(my_list)
# Output: [1, 2, 3, 4, 5]

We can use the sorted() function to sort a list in ascending order and create a new list. For example:

my_list = [3, 1, 4, 2, 5]
new_list = sorted(my_list)
print(new_list)
# Output: [1, 2, 3, 4, 5]
Conclusion

Lists are a powerful and versatile data type in Python. With their ability to store multiple items in an ordered, mutable and iterable container, they are essential for many data manipulation tasks.