python链接两个列表_掌握Python中的链接列表

python链接两个列表

Linked lists are important data structures in computer science. A linked list is a linear collection of nodes, where each node contains a data value and a reference to the next node in the list. In this post, we will walk through an example of building a singly linked list corresponding to a music playlist.

链接列表是计算机科学中的重要数据结构。 链表是节点的线性集合,其中每个节点包含一个数据值和对列表中下一个节点的引用。 在本文中,我们将逐步介绍构建与音乐播放列表相对应的单链接列表的示例。

Let’s get started!

让我们开始吧!

First, we will define a class called ‘SongNode’. In the argument of the ‘__init__’ method, we will initialize attributes ‘current_song’ and ‘next_song’ as ‘None’:

首先,我们将定义一个名为“ SongNode”的类。 在'__init__'方法的参数中,我们将把'current_song'和'next_song'属性初始化为'None':

class SongNode:
def __init__(self, current_song=None, next_song = None):

Next we define the class attributes, ‘current_song’ and ‘next_song’:

接下来,我们定义类属性“ current_song”和“ next_song”:

class SongNode:
def __init__(self, current_song=None, next_song = None):
self.current_song = current_song
self.next_song = next_song

Now, let’s write a class that will allow us to build our linked song list. Let’s call this class ‘SongList’:

现在,让我们编写一个类,使我们能够构建链接的歌曲列表。 我们将此类称为“ SongList”:

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

We will use the ‘SongList’ class, which will contain a reference to the ‘SongNode’ type, to initialize our song list object. Let’s write an instance of our song list class and print the result:

我们将使用“ SongList”类(其中包含对“ SongNode”类型的引用)来初始化我们的歌曲列表对象。 让我们写一个歌曲列表类的实例并打印结果:

if __name__=='__main__':
#initialize linked list object
linkedlist = SongList()
print(linkedlist)
Image for post

Now let’s build a linked list of songs for our playlist. Let’s select the top 3 songs of all time by The Beatles according to Entertainment Weekly. To start let’s define the value of each node. (Note: The ‘…’ corresponds to omitted code):

现在,让我们为播放列表构建一个链接的歌曲列表。 让我们根据《 娱乐周刊 》选择披头士有史以来的前三首歌曲 首先,让我们定义每个节点的值。 (注意:“…”对应于省略的代码):

if __name__=='__main__':
...
#assign values to nodes
linkedlist.head = SongNode("A Hard Day's Night")
second = SongNode('A Day in the Life')
third = SongNode("Strawberry Fields Forever")

Now let’s define the ‘next song’ value of the head node (the first song):

现在让我们定义头节点(第一首歌)的“下一首歌”值:

if __name__=='__main__':
...
#link nodes
linkedlist.head.next_song = second

Then we define the ‘next song’ value of the second node:

然后定义第二个节点的“下一首歌”值:

if __name__=='__main__':
...
second.next_song = third

Next, let’s define a simple function that will allow us to traverse our linked song list. In a while loop we’ll print the value of the current song and redefine the current value as the next song until it reaches the tail of the linked list, which points to null:

接下来,让我们定义一个简单的函数,使我们能够遍历链接的歌曲列表。 在while循环中,我们将打印当前歌曲的值,并将当前值重新定义为下一首歌曲,直到它到达链接列表的末尾,并指向null:

class SongList:   
...
def printSongs(self):
value = self.head
while (value):
print(value.current_song)
value = value.next_song

Next, let’s print our list of songs:

接下来,让我们打印我们的歌曲列表:

if __name__=='__main__':
...
linkedlist.printSongs()
Image for post

Next, let’s define a function that allows us to insert a song at the end of our linked list. The function will take the parameter ‘new_song’. The function first checks if the head of the linked list is ‘None’. If it is, the head takes the value of new song:

接下来,让我们定义一个函数,该函数允许我们在链接列表的末尾插入一首歌曲。 该函数将采用参数“ new_song”。 该函数首先检查链接列表的开头是否为“无”。 如果是,则头部采用新歌曲的价值:

def NewSong(self, new_song):
NewSongNode = SongNode(new_song)
if self.head is None:
self.head = NewSongNode

Next, the variable ‘last’ is defined as the head node and in a while loop we iterate over the linked objects until we get to the song node that points to null. The ‘next song’ value of the ‘last’ node is then defined as the new song node:

接下来,将变量“ last”定义为头节点,并在while循环中循环访问链接的对象,直到到达指向null的歌曲节点为止。 然后,将“最后一个”节点的“下一首歌曲”值定义为新歌曲节点:

def newSong(self, new_song):
NewSongNode = SongNode(new_song)
if self.head is None:
self.head = NewSongNode
last = self.head
while(last.next_song):
last = last.next_song
last.next_song=NewSongNode

Now, let’s add two new songs “She Loves You” and “Something” and print our list of songs:

现在,让我们添加两首新歌“ She Loves You”和“ Something”,并打印我们的歌曲列表:

if __name__=='__main__':
...
linkedlist.newSong("She Loves You")
linkedlist.newSong("Something")
linkedlist.printSongs()
Image for post

I’ll stop here but I encourage you to play around with the code yourself. Specifically, you may want to try defining a function in the ‘SongList’ class that allow you to search for a node. You can also try defining a method that allows you to delete a node.

我将在这里停止,但我鼓励您自己尝试使用该代码。 具体来说,您可能想尝试在“ SongList”类中​​定义一个函数,以使您可以搜索节点。 您也可以尝试定义一种允许删除节点的方法。

To summarize, in this post we discussed how to create a linked list corresponding to a playlist of songs. We showed how to define song nodes using the ‘SongNode’ class and how to link ‘SongNode’ objects using the ‘SongList’ class. We also showed how to add additional song nodes to the end of our linked list. I hope you found this post useful/interesting. The code from this post is available on GitHub. Thank you for reading!

总而言之,在本文中我们讨论了如何创建与歌曲的播放列表相对应的链接列表。 我们展示了如何使用“ SongNode”类定义歌曲节点,以及如何使用“ SongList”类链接“ SongNode”对象。 我们还展示了如何在链接列表的末尾添加其他歌曲节点。 我希望您发现这篇文章有用/有趣。 这篇文章中的代码可在GitHub找到 。 感谢您的阅读!

翻译自: https://towardsdatascience.com/mastering-linked-lists-in-python-ce66650de666

python链接两个列表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值