今日已更新 166 条资讯 | 累计 20138 条内容
关于我们

标签:#list

找到 2 篇相关文章

AI 资讯

Why Does a List Change in Two Variables?

We assume you already know how to write simple Python programs and understand basic syntax (if, for, functions, lists). Here, we're not discussing how to use the language, but why it works the way it does. In the previous article, we learned that variables in Python don't store data themselves. Instead, a name simply refers to an object. With numbers, this feels quite intuitive: x = 10 y = x Both names refer to the same object whose value is 10 . So far, everything seems straightforward. But as soon as we start working with lists, Python's behavior often surprises people. Consider this example: a = [ 1 , 2 ] b = a a . append ( 3 ) print ( b ) Many beginners expect the output to be: [ 1 , 2 ] After all, we modified a , not b . Instead, Python prints: [ 1 , 2 , 3 ] It looks as if changing one variable somehow changed the other. At first glance, it may seem like Python is keeping the two variables synchronized behind the scenes. In reality, the explanation is much simpler. Let's go back to the idea from the previous article. When we write: a = [ 1 , 2 ] Python creates a list object, and the name a becomes bound to it. Visually: a ───► [1, 2] Next comes this line: b = a This is the crucial part. Python does not create a second list. No copy is made. Instead, the name b is bound to exactly the same object. Now the picture looks like this: a ─┐ ├──► [1, 2] b ─┘ Notice that there's still only one list. The only thing that changed is that there are now two names referring to it. That's why the next line: a . append ( 3 ) doesn't create a new list. It modifies the existing object. After the call to append() , the picture becomes: a ─┐ ├──► [1, 2, 3] b ─┘ Since both names refer to the same object, the change is visible through both of them. In other words, Python didn't modify two different lists. It modified one list that simply has two names. This behavior is one of the most common sources of confusion for beginners. For example, you might write a function that modifies a l

2026-07-03 原文 →
AI 资讯

How to Implement Linked List Data Structure

A linked list is an ordered linear data structure where elements are not stored in sequential memory locations, instead they are stored in nodes that are linked together by a pointers. Linked list are used in data intensive application because linked list offer specific benefits for high frequency data manipulation, this benefits include: Efficient insertion and deletion Adding and removing elements from a linked list is highly efficient, unlike arrays which requires shifting all subsequent elements to maintain indexing. A linked list only requires updating the pointer. Dynamic sizing: linked list can grow or shrink during runtime without needing to pre-allocate memory. Memory management: Nodes in a linked list are only allocated when needed which prevents memory wastage. Flexible Traversal: Doubly and circular list allow you to move forward or backward, which makes them helpful for complex navigation The first node in a linked list is called the head which signifies the start of the list, while the last node is called the tail and has a pointer of null except in a circular linked list. Each node in a linked list has two things which are: the actual data the pointer or reference There are three main types of linked list: Singly linked list Doubly linked list Circular linked list Singly Linked List: Singly linked list are lists where each node has a next pointer that points to the next node. Doubly Linked List: Doubly linked list are list where each node has a next and previous pointer that points to the previous and next node. Circular Linked List: Circular linked list are list where the last node points back to the first node, forming a circle. Table of Contents create node class create linked list class isEmpty and getSize Methods prepend and append Method removeHead and removeTail Methods insert and search Methods getIndex and removeIndex Methods clear and print Methods create node class First let's open our code editor and create a new file called singlyLinkedLi

2026-06-02 原文 →