【问题标题】:How to retrieve only the current element while check the previous and next value in array (python)如何在检查数组中的上一个和下一个值时仅检索当前元素(python)
【发布时间】:2021-09-21 04:14:18
【问题描述】:

我正在重新访问如何在 Python 中编写以迭代数组列表,并且我正在尝试检查上一个和下一个值,并打印出列表中的当前值。例如,在我的列表中,我有 ["NotActive", "Active", "Depart", "Land", "Arrived"]

默认值为“NotActive”,我试图将 currentValue 设为“Depart”,我想检查下一个值为“Land”,前一个值为“Active”。但是,我一直将当前值设为“土地”

我创建了一个可以遍历列表的 for 循环。它会迭代,但不确定为什么如果一直迭代它就不会到达 Arrived。

知道发生了什么

 status_array = request.data['status'] -> here is the list fields listed above
 result = "NotActive" -> default value which now is the current value

try:
  for name in status_array:
     status = Flight.objects.get(name=name)
     status_id = status.status_id
except Flight.DoesNotExist:
   listed_result = {
      "result": status_id
   } -> here is the status_array for request.data['status'] the request going to be print out
print(listed_result) = ["Landed"]

【问题讨论】:

  • 您可以使用enumerate 在循环中访问列表中的上一个和下一个值。
  • for previous, current, next in zip(data, data[1:], data[2:]):
  • 在代码中你应该使用# insitead of ->。或至少 # -> 将其作为评论。

标签: python django-rest-framework iterator


【解决方案1】:

我想也许你可以考虑实现一个 Python DoubleLinkedList 类,这样你的数据就可以进入这个数据结构。

双链表包含几个节点,每个节点都有一个previous节点和一个next节点。在一个节点中,我们可以存储数据。而且,在(公共)属性的帮助下,我们实际上可以从前一个节点或下一个节点检索数据,给定一个当前节点来引用。我的参考是here

【讨论】:

  • 如何从这里的双链表中走出来,反向和正向?它会像我们想要检索的特定元素一样打印 1 个结果吗
【解决方案2】:

如果您只想检查上一个和下一个,您可以只为上一个设置一个临时变量。

#Set the previous/next to empty
prev = ''
next = ''
for i in range(len(status_array)):
    #Check if we are at the end
    if status_array[i] == 'Arrived':
        #Get the previous information
        status_prev = Flight.objects.get(name=prev)
        prev_status_id = status_prev.status_id
        #Get the current information
        status = Flight.objects.get(name=status_array[i])
        status_id = status.status_id
    #Check to see if we are at the start
    if prev == '':
        next = status_array[i+1]
        status_next = Flight.objects.get(name=next)
        next_status_id = status_next.status_id
        #Set the current to be the previous
        prev = status_array[i]
        #Get the current status info
        status = Flight.objects.get(name=status_array[i])
        status_id = status.status_id

    #Else we have a previous
    else:
        #Get the previous information
        status_prev = Flight.objects.get(name=prev)
        prev_status_id = status_prev.status_id
        #Get the current information
        status = Flight.objects.get(name=status_array[i])
        status_id = status.status_id
        #Make sure we have not 'Arrived'
        if status_array[i] != 'Arrived':
            next = status_array[i+1]
            status_next = Flight.objects.get(name=next)
            next_status_id = status_next.status_id
        #Update the previous
        prev = status_array[i]

我希望这指向正确的方向,但如前所述,尝试使用双向链表会更容易。干杯!

【讨论】:

  • 我发现从我的版本到你的版本,它打印的结果与我只想打印当前元素的结果相同。然而在我的它打印当前元素 [4],我检索你的元素 [4],它打印与我的相同的输出。它永远不会点击“到达”,当我将到达请求正文中时,它会打印空字符串。知道为什么
猜你喜欢
  • 1970-01-01
  • 2021-05-15
  • 1970-01-01
  • 1970-01-01
  • 2020-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多