Python列表索引方法在类中的行为很奇怪

2024-09-30 20:35:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的类,其中一个方法是获取指定值的列表的索引,因为它包含该值。 方法是下面类中的get_index_at_time(time)

class TimeStamp:
   def __init__(self):
      self.timeList = []
      self.indexList = []
      self.localDate = None

   def add(self, inTime, idx):
      if self.localDate is None:
         # Get todays date
         self.localDate = datetime.datetime.now()
      # Fix timeList with time adjusted to inTime
      thisTime = create_timestamp(self.localDate, inTime)
      self.timeList.append(thisTime)
      self.indexList.append(idx)

   def get_time_at_index(self, ix):
      if len(self.timeList) > ix and 0 <= ix:
         return self.timeList[ix]

   def get_index_at_time(self, inTime):
      ix = self.timeList.index(inTime)
      if ix:
         return self.indexList[ix]
      else:
         return -1

对于列表中的第一个元素,由于某种奇怪的原因,这会失败,例如索引0,但我已检查了具有相同数据的列表上的index方法是否适用于索引0。只有在类内部使用时,它的行为才奇怪

运行脚本将提供以下功能:

$ python3 simple_index_test.py 

====================================================
Testing list index method for dbgList:
Test passed
====================================================
Start testing TimeStamp.get_time_at_index
Test passed
====================================================
Start testing TimeStamp.get_index_at_time
 - Test Failed for time stamp: 2020-04-12 01:01:01
====================================================
Comparing the values in dbgList with idx1.TimeList
Test passed
====================================================

代码如下:

import datetime

def create_timestamp(localDate, inTime):
   timeString = localDate.strftime('%Y-%m-%d') + ' ' + inTime
   return datetime.datetime.strptime(timeString, '%Y-%m-%d %H:%M:%S')

class TimeStamp:
   def __init__(self):
      self.timeList = []
      self.indexList = []
      self.localDate = None

   def add(self, inTime, idx):
      if self.localDate is None:
         # Get todays date
         self.localDate = datetime.datetime.now()
      # Fix timeList with time adjusted to inTime
      thisTime = create_timestamp(self.localDate, inTime)
      self.timeList.append(thisTime)
      self.indexList.append(idx)

   def get_time_at_index(self, ix):
      if len(self.timeList) > ix and 0 <= ix:
         return self.timeList[ix]

   def get_index_at_time(self, inTime):
      ix = self.timeList.index(inTime)
      if ix:
         return self.indexList[ix]
      else:
         return -1

log1 = [
   [0.123, '01:01:01', 0],
   [0.134, '01:01:02', 1],
   [0.145, '01:01:03', 1],
   [0.156, '01:01:04', 2],
   [0.167, '01:01:05', 2],
   [0.178, '01:01:06', 2],
   [0.189, '01:01:07', 3],
]

def main():
   # Get todays date
   localDate = datetime.datetime.now()
   idx1 = TimeStamp()
   # debug list for testing the index method directly
   dbgList = []
   # Fill idx1
   for ix,val in enumerate(log1):
      idx1.add(val[1], ix)
      # Debugging why test fails, create an identical list of same time stamp as in the TimeStamp class
      thisTime = create_timestamp(localDate, val[1])
      dbgList.append(thisTime)

   print('====================================================')
   print('Testing list index method for dbgList:')
   testPassed = True
   for ix, val in enumerate(dbgList):
      rx = dbgList.index(val)
      if rx != ix:
         print('List index() method failed match for: ' + str(val))
         testPassed = False
   if testPassed:
      print('Test passed')
   # Test method get_time_at_index
   print('====================================================')
   print('Start testing TimeStamp.get_time_at_index')
   testPassed = True
   # Loop over all elements in log1
   for ix,val in enumerate(log1):
      thisTime = idx1.get_time_at_index(ix)
      # Create the expected time stamp
      expectedTime = create_timestamp(localDate, val[1])
      if thisTime != expectedTime:
         print(' - Test Failed for index: ' + str(ix))
         testPassed = False
   if testPassed:
      print('Test passed')
   # Test method get_index_at_time
   print('====================================================')
   print('Start testing TimeStamp.get_index_at_time')
   testPassed = True
   # Loop over all elements in log1
   for ix,val in enumerate(log1):
      # Create the time we want to get index for
      wantedTime = create_timestamp(localDate, val[1])
      thisIndex = idx1.get_index_at_time(wantedTime)
      if thisIndex != ix:
         print(' - Test Failed for time stamp: ' + str(wantedTime))
         testPassed = False
   if testPassed:
      print('Test passed')
   print('====================================================')
   print('Comparing the values in dbgList with idx1.TimeList')
   testPassed = True
   for ix, val in enumerate(dbgList):
      idxTimeStamp = idx1.timeList[ix]
      if idxTimeStamp != val:
         print(' - Test Failed for time stamps (dbg) (idx1.timeList): ' + str(val) + ' ' + str(idxTimeStamp))
         testPassed = False
   if testPassed:
      print('Test passed')
   print('====================================================')


if __name__ == "__main__":
   main()


Tags: testselfforgetindexiftimeval
1条回答
网友
1楼 · 发布于 2024-09-30 20:35:23

我看到第一个索引(index-0)出现了这个问题。如果您打印thisIndex(在测试时间戳。在测试时间获取索引)variabel,对于第一个循环,这将是-1

请参见get_index_at_time()函数。这是因为if语句将返回Falsefor 0。因此,函数将返回-1。你可以为if添加一个条件。我们开始:

def get_index_at_time(self, inTime):
      ix = self.timeList.index(inTime)
      if ix or ix == 0:
         return self.indexList[ix]
      else:
         return -1

输出将是:

====================================================
Testing list index method for dbgList:
Test passed
====================================================
Start testing TimeStamp.get_time_at_index
Test passed
====================================================
Start testing TimeStamp.get_index_at_time
Test passed
====================================================
Comparing the values in dbgList with idx1.TimeList
Test passed
====================================================

(对不起,英语不好)

相关问题 更多 >