如何在带有列表头的索引处切换节点? [英] How to switch nodes at an index with the head of a list?

查看:32
本文介绍了如何在带有列表头的索引处切换节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试创建一个函数,将索引处的节点与列表的头部进行切换.因此,如果我的列表 (list) 具有值 [1, 7, 9, 12] 并且我调用 switch(list, 2),我的结果将是 [9, 7, 1, 12].这是我到目前为止的代码:

I'm currently trying to create a function that will switch the nodes at index with the head of the list. So if my list (list) has the values [1, 7, 9, 12] and I call switch(list, 2), my result will be [9, 7, 1, 12]. This is the code I have so far:

def switch(list, index):
    ....

def testSwitch():
#test code to ensure that switch() is working correctly.
myList = createList([10, 20, 30, 40, 50, 60])
print "The initial list", listString(myList)
myList = switch(myList, 2)
print "Switching the head and the 30.  Resulting list is ", listString(myList)
myList = switch(myList, 5)
print "Switching the head and the 60.  Resuling list is ", listString(myList)
myList = switch(myList, 29)  #should result in an error

推荐答案

在列表中切换元素其实很简单:

Switching elements in a list is actually very easy:

myList[0], myList[1] = myList[1], myList[0]

这将交换 myList 中的第一个和第二个元素.Python 实际上有一个优化的字节码命令,可以非常快速地交换程序堆栈上的两个值,因此这与交换列表值的速度差不多.

This will swap the first and second elements in myList, in place. Python actually has an optimized bytecode command that very quickly swaps two values on the program stack, so this is about as fast as you can swap list values.

当然,在那种情况下,您不会返回新列表,而是修改旧列表.因此,您只需编写 switch(myList, 2),而不是 myList = switch(myList, 2).代码看起来像这样:

Of course, in that case you wouldn't be returning a new list, you'd be modifying the old one. So instead of myList = switch(myList, 2), you would just write switch(myList, 2). The code would look something like this:

def switch(lst, i):
  lst[0], lst[i] = lst[i], lst[0]

如果你想返回一个全新的列表,你需要先复制一份:

If you want to return an entirely new list, you'd need to make a copy first:

def switch(lst, i):
  newlst = list(lst)
  newlst[0], newlst[i] = newlst[i], newlst[0]
  return newlst

如果您正在使用链表,那就有点不同了.我认为链表不存在 Python 优化;常规列表很容易向其中添加项目,并且它们可以与任何类型的对象一起使用,因此链表在 Python 中失去了它们的用途.不过,这里有一个建议:

if you're working with a linked list, that is a bit of a different story. I don't think Python optimizations exist for linked lists; regular lists are very easy to add items to, and they work with any kind of object, so linked lists very much lose their purpose in Python. Nevertheless, here's a suggestion:

def switch(ll, i):
  head = ll
  currentItem = ll      # The head again
  prevItem = None       # The item that links to tempItem
  for x in range(i):    # Find the item to swap
    prevItem = currentItem
    currentItem = currentItem.next

  # Now we swap. We're rotating three items' .next values, so we can't
  # do the really optimized way.
  temp = currentItem.next
  currentItem.next = head.next
  head.next = prevItem.next
  prevItem.next = temp

链表操作就是维护到下一个项目的正确链接.另请注意,如果您尝试交换链接列表中实际不存在的位置,则上述代码将失败.检查您的输入.

Linked list manipulation is all about maintaining proper links to the next item. Also note that the above code will fail if you're trying to swap for a position that doesn't actually exist in your linked list. Check your inputs.

这篇关于如何在带有列表头的索引处切换节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆