python-3.x 在某个指数之后,找到一个指数的最佳方法是什么?

yjghlzjz  于 2022-12-20  发布在  Python
关注(0)|答案(2)|浏览(174)

我需要返回下一个invoice_index_end,只要它在第一个start索引之后,但我似乎找不到一种智能的方法来完成这一点。目前发生的情况是,如果在目标invoice开始之前有一个invoice的结束,它将找到那个1并返回其索引位置。是否有if或while函数可用于循环遍历的索引位置,直到它大于起始索引?
为我的初始列表添加了上下文。Thelst_file实际上就是一个用文件行填充的列表。

lst_file = []
def assign_file_lines_to_list():
  for invoices in file:
    lst_file.append(invoices)
  return lst_file
assign_file_lines_to_list()
file.close()

#Opens the requested error log
error_lst = []
file_name = input("Enter the full name of the error log. \n")
error_file = open(file_name, "r")

#Putserror log file into a list word by word.
for eachWord in error_file:
  error_lst.extend(eachWord.split())

#Converts all string values to integers
def str_to_int():
  for i in range(0, len(error_lst)):
    try:
      error_lst[i] = int(error_lst[i])
    except:
      continue
  return error_lst

#Everything that is converted to an integer is added to a new list
int_lst = []
for eachword in str_to_int():
  if type(eachword) == int and eachword > 999:
    int_lst.append(eachword)

#And then turned back into a string.
def int_to_str():
  for i in range(0, len(int_lst)):
    try:
      int_lst[i] = str(int_lst[i])
    except:
      print("Error converting integer to a string!")
int_to_str()

#Standardizes all invoice to 10 digits
str_lst = [str(item).zfill(10) for item in int_lst]
print(str_lst)

#Finds the index of the invoice start and invoice end
new_lst = []
invoice_index_start = 0
invoice_index_end = 0
constant = '</Invoice>\n'
for i in str_lst: #integer
  invoice_index_start = lst_file.index('<InvoiceNumber>' + i + '</InvoiceNumber>\n')
  while str_lst.index(constant) > invoice_index_start:
    invoice_index_end = lst_file.index(constant)
  #if invoice_index_end <= invoice_index_start:
#Copies everything between start and end index into new list to be deleted from original list later
    new_lst += lst_file[(invoice_index_start - 1):(invoice_index_end + 1)]
voase2hg

voase2hg1#

看起来你试图从某个值的第一次出现开始对一个可迭代对象进行切片,直到但不包括同一个值的下一个示例?
例如,如果文件的内容类似于:

<someValue>a</someValue>
<InvoiceNumber>1</InvoiceNumber>
<someValue>b</someValue>
<InvoiceNumber>2</InvoiceNumber>
<someValue>c</someValue>
<InvoiceNumber>1</InvoiceNumber>
<someValue>d</someValue>
<InvoiceNumber>3</InvoiceNumber>
<someValue>d</someValue>

在这里,您需要捕获:

<InvoiceNumber>1</InvoiceNumber>
<someValue>b</someValue>
<InvoiceNumber>2</InvoiceNumber>
<someValue>c</someValue>

如果这是不正确的,你应该在你的问题提供一些样本数据,并真正清楚地表明你试图选择什么。
为了简化这个问题,下面的示例显示了一个可能的解决方案:

data = [1, 2, 5, 3, 8, 2, 1, 2, 7, 3]

# select everything from the first `2`, up to but not including the next
print(data[data.index(2):data.index(2, data.index(2)+1)])  # [2, 5, 3, 8]

# same for `3`
print(data[data.index(3):data.index(3, data.index(3)+1)])  # [3, 8, 2, 1, 2, 7]

在您的代码中,它可能类似于:

result = lst_file[
    lst_file.index(constant):lst_file.index(constant, lst_file.index(constant)+1)
]

然而,这是假设你的lst_file是可迭代的,我们不能确定,因为你没有在你的示例代码中包含它的定义。
如果不是(但是您使用.index()表明它很可能是),则如下所示:

import itertools

result = itertools.islice(
    lst_file, 
    lst_file.index(constant), 
    lst_file.index(constant, lst_file.index(constant)+1
)
1qczuiv0

1qczuiv02#

最后我绞尽脑汁,又尝试了几种方法,这就是我最终得到的结果:

start_indexes = []
for i in str_lst:
  invoice_index_start = lst_file.index('<InvoiceNumber>' + i + '</InvoiceNumber>\n')
  start_indexes.append(invoice_index_start)

end_indexes = []
constant = '</Invoice>\n'
for i in range(0,len(start_indexes)):
  invoice_index_end = lst_file.index(constant, start_indexes[i])
  end_indexes.append(invoice_index_end + 1)

我还是很惊讶这起作用了。

相关问题