简体   繁体   English

Python 带枚举的 For 循环:“字符串索引超出范围”

[英]Python For Loop with Enumerate: 'String Index Out Of Range'

I have been trying to write a for loop in Python, where the goal is to iterate through a list of alphabetically-sorted letters and a corresponding list of numbers, and perform a cumulative calculation:我一直在尝试在 Python 中编写一个 for 循环,其目标是遍历按字母顺序排序的字母列表和相应的数字列表,并执行累积计算:

  1. If a letter in the first list is different than the previous letter (ie it is the first time that that letter appears in the list), divide the corresponding number from the second list by 0.5 and then append it to a new list.如果第一个列表中的字母与前一个字母不同(即,这是该字母第一次出现在列表中),则将第二个列表中的相应数字除以 0.5,然后 append 将其放入新列表中。
  2. Until the letter in the first list changes, take the previous value from the new list, multiply it by 0.5, and then add the current item from the number list and append the result to the new list.直到第一个列表中的字母发生变化,从新列表中取出以前的值,乘以0.5,然后将数字列表中的当前项和append结果添加到新列表中。

This example shows how this would work in Excel: MS Excel calculation此示例显示这将如何在 Excel 中工作: MS Excel 计算

At first, I thought that I could do something like what was outlined here , using a while loop and having it reference previous values, but then this seemed like it wouldn't work for this purpose since there is already an existing list (the letters) for which I would need to reference previous values (rather than just referencing previous values in a new list).起初,我认为我可以做一些类似于此处概述的事情,使用 while 循环并让它引用以前的值,但后来这似乎无法用于此目的,因为已经存在一个列表(字母) 我需要引用以前的值(而不是仅仅在新列表中引用以前的值)。

I then tried to use a for loop with enumerate, but was getting a 'string index out of range' error:然后我尝试将 for 循环与枚举一起使用,但出现“字符串索引超出范围”错误:

import numpy as np
import pandas as pd

d={'Letters':['A','A','A','B','B','B'],'Numbers':[1,2,3,4,5,6]}

df=pd.DataFrame(data=d)

Number=df.Numbers
Letter=df.Letters

NumberOutput=[]
for index,(x,y) in enumerate(zip(Number,Letter)):
    if index==0:
        NumberOutput.append(x/0.5)
    elif index>0 and y!=y[index-1]:
        NumberOutput.append(x/0.5)
    else:
        NumberOutput.append((NumberOutput[index-1]*0.5)+x)

I am assuming that the problem here is that the for loop is trying to reference the previous Letter value at position 0 in the list, ie the previous string index doesn't exist, but the loop explicitly handles the case of index==0 before trying to reference index-1, so I'm not clear on why this causes this error.我假设这里的问题是 for 循环试图引用列表中 position 0 处的前一个 Letter 值,即前一个字符串索引不存在,但循环明确处理了 index==0 之前的情况试图引用 index-1,所以我不清楚为什么会导致此错误。

I ended up going a different route and using two separate for loops, one that creates a list of 'LetterFlags' (that flag a 1 anytime the letter in the first list changes and a 0 otherwise), and then a second one that uses the 'LetterFlag' to determine whether or not the current Letter is different from the previous one.我最终走上了一条不同的路线,并使用了两个单独的 for 循环,一个创建了一个“LetterFlags”列表(只要第一个列表中的字母发生变化就标记 1,否则标记 0),然后第二个使用'LetterFlag' 判断当前的 Letter 是否与前一个不同。 This approach doesn't throw an error and produces correct results, but this seems like it isn't the most efficient way to do this:这种方法不会抛出错误并产生正确的结果,但这似乎不是最有效的方法:

import numpy as np
import pandas as pd

d={'Letters':['A','A','A','B','B','B'],'Numbers':[1,2,3,4,5,6]}

df=pd.DataFrame(data=d)

LetterFlag=[]
Number=df.Numbers
Letter=df.Letters

for (previousL,currentL) in zip(Letter,Letter[1:]):
    if previousL!=currentL:
        LetterFlag.append(1)
    else:
        LetterFlag.append(0)
    
LetterFlag=[1]+LetterFlag
df['LetterFlag']=LetterFlag
LetterFlag=df.LetterFlag

NumberOutput=[]
for index,(x,y) in enumerate(zip(LetterFlag,Number)):
    if x==1:
        NumberOutput.append(y/0.5)
    else:
        NumberOutput.append((NumberOutput[index-1]*0.5)+y)

Is there a better way that I could have done this?有没有更好的方法可以做到这一点? Thank you in advance for any guidance.预先感谢您提供任何指导。

If I understand the question correctly, you could do this:-如果我正确理解了这个问题,你可以这样做:-

letters = ['A', 'A', 'A', 'B', 'B', 'B']
numbers = [1, 2, 3, 4, 5, 6]
assert len(letters) == len(numbers)

numout = [numbers[0]*0.5]

for i, L in enumerate(letters[1:], 1):
    if L != letters[i-1]:
        numout.append(numbers[i]*0.5)
        
print(*numout)

With these values the output will be:- 0.5 2.0使用这些值,output 将是:- 0.5 2.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM