Rumah > pembangunan bahagian belakang > Tutorial Python > Helaian Cheat Struktur Data Python Komprehensif

Helaian Cheat Struktur Data Python Komprehensif

WBOY
Lepaskan: 2024-07-19 05:18:09
asal
550 orang telah melayarinya

Comprehensive Python Data Structures Cheat sheet

Helaian Cheat Struktur Data Python Komprehensif

Jadual Kandungan

  1. Senarai
  2. Tuple
  3. Set
  4. Kamus
  5. Rentetan
  6. Susun atur
  7. Timbunan
  8. Baris Beratur
  9. Senarai Terpaut
  10. Pokok
  11. Timbunan
  12. Graf
  13. Struktur Data Terperinci

Senarai

Senarai disusun, urutan boleh ubah.

Penciptaan

empty_list = []
list_with_items = [1, 2, 3]
list_from_iterable = list("abc")
list_comprehension = [x for x in range(10) if x % 2 == 0]
Salin selepas log masuk

Operasi Biasa

# Accessing elements
first_item = my_list[0]
last_item = my_list[-1]

# Slicing
subset = my_list[1:4]  # Elements 1 to 3
reversed_list = my_list[::-1]

# Adding elements
my_list.append(4)  # Add to end
my_list.insert(0, 0)  # Insert at specific index
my_list.extend([5, 6, 7])  # Add multiple elements

# Removing elements
removed_item = my_list.pop()  # Remove and return last item
my_list.remove(3)  # Remove first occurrence of 3
del my_list[0]  # Remove item at index 0

# Other operations
length = len(my_list)
index = my_list.index(4)  # Find index of first occurrence of 4
count = my_list.count(2)  # Count occurrences of 2
my_list.sort()  # Sort in place
sorted_list = sorted(my_list)  # Return new sorted list
my_list.reverse()  # Reverse in place
Salin selepas log masuk

Teknik Lanjutan

# List as stack
stack = [1, 2, 3]
stack.append(4)  # Push
top_item = stack.pop()  # Pop

# List as queue (not efficient, use collections.deque instead)
queue = [1, 2, 3]
queue.append(4)  # Enqueue
first_item = queue.pop(0)  # Dequeue

# Nested lists
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened = [item for sublist in matrix for item in sublist]

# List multiplication
repeated_list = [0] * 5  # [0, 0, 0, 0, 0]

# List unpacking
a, *b, c = [1, 2, 3, 4, 5]  # a=1, b=[2, 3, 4], c=5
Salin selepas log masuk

Tuple

Tuple dipesan, urutan tidak berubah.

Penciptaan

empty_tuple = ()
single_item_tuple = (1,)  # Note the comma
tuple_with_items = (1, 2, 3)
tuple_from_iterable = tuple("abc")
Salin selepas log masuk

Operasi Biasa

# Accessing elements (similar to lists)
first_item = my_tuple[0]
last_item = my_tuple[-1]

# Slicing (similar to lists)
subset = my_tuple[1:4]

# Other operations
length = len(my_tuple)
index = my_tuple.index(2)
count = my_tuple.count(3)

# Tuple unpacking
a, b, c = (1, 2, 3)
Salin selepas log masuk

Teknik Lanjutan

# Named tuples
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(11, y=22)
print(p.x, p.y)

# Tuple as dictionary keys (immutable, so allowed)
dict_with_tuple_keys = {(1, 2): 'value'}
Salin selepas log masuk

set

Set ialah koleksi unsur unik yang tidak tersusun.

Penciptaan

empty_set = set()
set_with_items = {1, 2, 3}
set_from_iterable = set([1, 2, 2, 3, 3])  # {1, 2, 3}
set_comprehension = {x for x in range(10) if x % 2 == 0}
Salin selepas log masuk

Operasi Biasa

# Adding elements
my_set.add(4)
my_set.update([5, 6, 7])

# Removing elements
my_set.remove(3)  # Raises KeyError if not found
my_set.discard(3)  # No error if not found
popped_item = my_set.pop()  # Remove and return an arbitrary element

# Other operations
length = len(my_set)
is_member = 2 in my_set

# Set operations
union = set1 | set2
intersection = set1 & set2
difference = set1 - set2
symmetric_difference = set1 ^ set2
Salin selepas log masuk

Teknik Lanjutan

# Frozen sets (immutable)
frozen = frozenset([1, 2, 3])

# Set comparisons
is_subset = set1 <= set2
is_superset = set1 >= set2
is_disjoint = set1.isdisjoint(set2)

# Set of sets (requires frozenset)
set_of_sets = {frozenset([1, 2]), frozenset([3, 4])}
Salin selepas log masuk

Kamus

Kamus ialah pemetaan boleh ubah bagi pasangan nilai kunci.

Penciptaan

empty_dict = {}
dict_with_items = {'a': 1, 'b': 2, 'c': 3}
dict_from_tuples = dict([('a', 1), ('b', 2), ('c', 3)])
dict_comprehension = {x: x**2 for x in range(5)}
Salin selepas log masuk

Operasi Biasa

# Accessing elements
value = my_dict['key']
value = my_dict.get('key', default_value)

# Adding/Updating elements
my_dict['new_key'] = value
my_dict.update({'key1': value1, 'key2': value2})

# Removing elements
del my_dict['key']
popped_value = my_dict.pop('key', default_value)
last_item = my_dict.popitem()  # Remove and return an arbitrary key-value pair

# Other operations
keys = my_dict.keys()
values = my_dict.values()
items = my_dict.items()
length = len(my_dict)
is_key_present = 'key' in my_dict
Salin selepas log masuk

Teknik Lanjutan

# Dictionary unpacking
merged_dict = {**dict1, **dict2}

# Default dictionaries
from collections import defaultdict
dd = defaultdict(list)
dd['key'].append(1)  # No KeyError

# Ordered dictionaries (Python 3.7+ dictionaries are ordered by default)
from collections import OrderedDict
od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])

# Counter
from collections import Counter
c = Counter(['a', 'b', 'c', 'a', 'b', 'b'])
print(c.most_common(2))  # [('b', 3), ('a', 2)]
Salin selepas log masuk

rentetan

String ialah jujukan aksara Unikod yang tidak boleh diubah.

Penciptaan

single_quotes = 'Hello'
double_quotes = "World"
triple_quotes = '''Multiline
string'''
raw_string = r'C:\Users\name'
f_string = f"The answer is {40 + 2}"
Salin selepas log masuk

Operasi Biasa

# Accessing characters
first_char = my_string[0]
last_char = my_string[-1]

# Slicing (similar to lists)
substring = my_string[1:4]

# String methods
upper_case = my_string.upper()
lower_case = my_string.lower()
stripped = my_string.strip()
split_list = my_string.split(',')
joined = ', '.join(['a', 'b', 'c'])

# Other operations
length = len(my_string)
is_substring = 'sub' in my_string
char_count = my_string.count('a')
Salin selepas log masuk

Teknik Lanjutan

# String formatting
formatted = "{} {}".format("Hello", "World")
formatted = "%s %s" % ("Hello", "World")

# Regular expressions
import re
pattern = r'\d+'
matches = re.findall(pattern, my_string)

# Unicode handling
unicode_string = u'\u0061\u0062\u0063'
Salin selepas log masuk

Tatasusunan

Tatasusunan ialah jujukan padat nilai angka (daripada modul tatasusunan).

Penciptaan dan Penggunaan

from array import array
int_array = array('i', [1, 2, 3, 4, 5])
float_array = array('f', (1.0, 1.5, 2.0, 2.5))

# Operations (similar to lists)
int_array.append(6)
int_array.extend([7, 8, 9])
popped_value = int_array.pop()
Salin selepas log masuk

Tumpukan

Timbunan boleh dilaksanakan menggunakan senarai atau collections.deque.

Pelaksanaan dan Penggunaan

# Using list
stack = []
stack.append(1)  # Push
stack.append(2)
top_item = stack.pop()  # Pop

# Using deque (more efficient)
from collections import deque
stack = deque()
stack.append(1)  # Push
stack.append(2)
top_item = stack.pop()  # Pop
Salin selepas log masuk

Beratur

Baris gilir boleh dilaksanakan menggunakan collections.deque atau queue.Queue.

Pelaksanaan dan Penggunaan

# Using deque
from collections import deque
queue = deque()
queue.append(1)  # Enqueue
queue.append(2)
first_item = queue.popleft()  # Dequeue

# Using Queue (thread-safe)
from queue import Queue
q = Queue()
q.put(1)  # Enqueue
q.put(2)
first_item = q.get()  # Dequeue
Salin selepas log masuk

Senarai Terpaut

Python tidak mempunyai senarai terpaut terbina dalam, tetapi ia boleh dilaksanakan.

Perlaksanaan Mudah

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        if not self.head:
            self.head = Node(data)
            return
        current = self.head
        while current.next:
            current = current.next
        current.next = Node(data)
Salin selepas log masuk

pokok

Pokok boleh dilaksanakan menggunakan kelas tersuai.

Perlaksanaan Pokok Binari Mudah

class TreeNode:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

class BinaryTree:
    def __init__(self, root):
        self.root = TreeNode(root)

    def insert(self, value):
        self._insert_recursive(self.root, value)

    def _insert_recursive(self, node, value):
        if value < node.value:
            if node.left is None:
                node.left = TreeNode(value)
            else:
                self._insert_recursive(node.left, value)
        else:
            if node.right is None:
                node.right = TreeNode(value)
            else:
                self._insert_recursive(node.right, value)
Salin selepas log masuk

timbunan

Heaps boleh dilaksanakan menggunakan modul heapq.

Penggunaan

import heapq

# Create a heap
heap = []
heapq.heappush(heap, 3)
heapq.heappush(heap, 1)
heapq.heappush(heap, 4)

# Pop smallest item
smallest = heapq.heappop(heap)

# Create a heap from a list
my_list = [3, 1, 4, 1, 5, 9]
heapq.heapify(my_list)
Salin selepas log masuk

graf

Graf boleh dilaksanakan menggunakan kamus.

Perlaksanaan Mudah

class Graph:
    def __init__(self):
        self.graph = {}

    def add_edge(self, u, v):
        if u not in self.graph:
            self.graph[u] = []
        self.graph[u].append(v)

    def bfs(self, start):
        visited = set()
        queue = [start]
        visited.add(start)
        while queue:
            vertex = queue.pop(0)
            print(vertex, end=' ')
            for neighbor in self.graph.get(vertex, []):
                if neighbor not in visited:
                    visited.add(neighbor)
                    queue.append(neighbor)
Salin selepas log masuk

Struktur Data Lanjutan

cuba

class TrieNode:
    def __init__(self):
        self.children = {}
        self.is_end = False

class Trie:
    def __init__(self):
        self.root = TrieNode()

    def insert(self, word):
        node = self.root
        for char in word:
            if char not in node.children:
                node.children[char] = TrieNode()
            node = node.children[char]
        node.is_end = True

    def search(self, word):
        node = self.root
        for char in word:
            if char not in node.children:
                return False
            node = node.children[char]
        return node.is_end
Salin selepas log masuk

Set Berpisah (Union-Find)

class DisjointSet:
    def __init__(self, vertices):
        self.parent = {v: v for v in vertices}
        self.rank = {v: 0 for v in vertices}

    def find(self, item):
        if self.parent[item] != item:
            self.parent[item] = self.find(self.parent[item])
        return self.parent[item]

    def union(self, x, y):
        xroot = self.find(x)
        yroot = self.find(y)
        if self.rank[xroot] < self.rank[yroot]:
            self.parent[xroot] = yroot
        elif self.rank[xroot] > self.rank[yroot]:
            self.parent[yroot] = xroot
        else:
            self.parent[yroot] = xroot
            self.rank[xroot] += 1
Salin selepas log masuk

Lembaran curang komprehensif ini merangkumi pelbagai struktur data Python, daripada jenis asas terbina dalam kepada pelaksanaan tersuai yang lebih maju. Setiap bahagian termasuk kaedah penciptaan, operasi biasa dan teknik lanjutan jika berkenaan.
0

Atas ialah kandungan terperinci Helaian Cheat Struktur Data Python Komprehensif. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:dev.to
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan