Mifaaani ya Python

Python 集合(数组)

Python 编程语言中有四种集合数据类型:

  • 列表(List)是一种有序和可更改的集合。允许重复的成员。
  • 元组(Tuple)是一种有序且不可更改的集合。允许重复的成员。
  • 集合(Set)是一个无序和无索引的集合。没有重复的成员。
  • 词典(Dictionary)是一个无序,可变和有索引的集合。没有重复的成员。

It is useful to understand the properties of the type when choosing a collection type.

Choosing the correct type for a specific dataset may mean retaining meaning, and it may mean improving efficiency or security.

List

A list is an ordered and mutable collection. In Python, lists are written with square brackets.

Mfano

Create a list:

thislist = ["apple", "banana", "cherry"]
print(thislist)

Mfano wa kufungua

Access item

You can access list items by referencing the index number:

Mfano

Print the second item of the list:

thislist = ["apple", "banana", "cherry"]
print(thislist[1])

Mfano wa kufungua

Negative indices

Negative indices represent starting from the end, -1 represents the last item, -2 represents the second last item, and so on.

Mfano

Print the last item of the list:

thislist = ["apple", "banana", "cherry"]
print(thislist[-1])

Mfano wa kufungua

Index range

You can specify an index range by specifying the start and end of the range.

After specifying a range, the returned value will be a new list containing the specified items.

Mfano

Return the third, fourth, and fifth items:

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])

Mfano wa kufungua

Note:The search will start from index 2 (inclusive) to index 5 (exclusive).

Remember, the index of the first item is 0.

Range of negative indices

If you want to start searching from the end of the list, specify a negative index:

Mfano

This example will return items from index -4 (inclusive) to index -1 (exclusive):

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[-4:-1])

Mfano wa kufungua

Change item value

To change the value of a specific item, refer to the index number:

Mfano

Change the second item:

thislist = ["apple", "banana", "cherry"]
thislist[1] = "mango"
print(thislist)

Mfano wa kufungua

To traverse the list

You can use for Loop through list items:

Mfano

Print all items in the list one by one:

thislist = ["apple", "banana", "cherry"]
for x in thislist:
  print(x)

Mfano wa kufungua

You will learn Makikuu ya For ya Python Learn about for More about loops.

Check if item exists

To determine if a specified item exists in the list, use in Keyword:

Mfano

Check if 'apple' exists in the list:

thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
  print("Yes, 'apple' is in the fruits list")

Mfano wa kufungua

List length

To determine how many items are in the list, use len() Method:

Mfano

Print the number of items in the list:

thislist = ["apple", "banana", "cherry"]
print(len(thislist))

Mfano wa kufungua

Add item

To add an item to the end of the list, use append() Method:

Mfano

Kutumia: append() Method to append items:

thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)

Mfano wa kufungua

To add an item at a specified index, use insert() Method:

Mfano

插入项目作为第二个位置:

thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)

Mfano wa kufungua

删除项目

有几种方法可以从列表中删除项目:

Mfano

remove() 方法删除指定的项目:

thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)

Mfano wa kufungua

Mfano

pop() 方法删除指定的索引(如果未指定索引,则删除最后一项):

thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)

Mfano wa kufungua

Mfano

del 关键字删除指定的索引:

thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)

Mfano wa kufungua

Mfano

del 关键字也能完整地删除列表:

thislist = ["apple", "banana", "cherry"]
del thislist

Mfano wa kufungua

Mfano

clear() 方法清空列表:

thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)

Mfano wa kufungua

复制列表

您只能通过键入 list2 = list1 来复制列表,因为:list2 将只是对 list1 的引用,list1 中所做的更改也将自动在 list2 中进行。

有一些方法可以进行复制,一种方法是使用内置的 List 方法 copy()

Mfano

Kutumia: copy() 方法来复制列表:

thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)

Mfano wa kufungua

制作副本的另一种方法是使用内建的方法 list()

Mfano

Kutumia: list() 方法复制列表:

thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)

Mfano wa kufungua

合并两个列表

在 Python 中,有几种方法可以连接或串联两个或多个列表。

最简单的方法之一是使用 + 运算符。

Mfano

合并两个列表:

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)

Mfano wa kufungua

连接两个列表的另一种方法是将 list2 中的所有项一个接一个地追加到 list1 中:

Mfano

把 list2 追加到 list1 中:

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
for x in list2:
  list1.append(x)
print(list1)

Mfano wa kufungua

或者,您可以使用 extend() 方法,其目的是将一个列表中的元素添加到另一列表中:

Mfano

使用 extend() 方法将 list2 添加到 list1 的末尾:

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)

Mfano wa kufungua

list() 构造函数

也可以使用 list() 构造函数创建一个新列表。

Mfano

Kutumia: list() Kumekua kina cha kuzengera orodha:

thislist = list(("apple", "banana", "cherry")) # Tafuta mabuni ya pili
print(thislist)

Mfano wa kufungua

Vifaa vya kipimo

Python ina kikomo cha vifaa ambavyo vinaweza kutumika kwenye kipimo

Method Kuelewa
append() Kuingiza kiwango kwenye kipimo cha kikamilifu
clear() Kufungua vifaa vya orodha vyote
copy() Kurejea nafasi ya orodha
count() Kurejea idadi ya matokeo yenye thamani zilizotumika
extend() Kuingiza matokeo ya orodha (au kiwango yoyote kinachopendekeza) kwenye kipimo cha kikamilifu
index() Kurejea kipimo cha kwanza yenye thamani zilizotumika
insert() Kuingiza kiwango kwenye nafasi zilizotumika
pop() Kufungua kiwango kwenye nafasi zilizotumika
remove() Kufungua matokeo yenye thamani zilizotumika
reverse() Kurejea orodha kirefu
sort() Kurejea orodha