For loop -:';的操作数类型不受支持;str';和';int';使用整数列表时

For loop -:';的操作数类型不受支持;str';和';int';使用整数列表时,for-loop,typeerror,python-3.3,For Loop,Typeerror,Python 3.3,我有一手生成的卡片,并存储在一个排序列表“srt_hand”和“srt_hand1”中。我做了一个函数,可以计算手上跑步的长度。我去测试它,但它一直说'if srt_hand[I]==srt_hand[I+1]-1:'是不受支持的操作数类型。为什么呢?这没有任何意义,因为我正在索引一个整数列表 我以前有过这个,使用相同的排序技术,它工作正常(除了在第一次运行后停止): run1=0 i=0 当i

我有一手生成的卡片,并存储在一个排序列表“srt_hand”和“srt_hand1”中。我做了一个函数,可以计算手上跑步的长度。我去测试它,但它一直说'if srt_hand[I]==srt_hand[I+1]-1:'是不受支持的操作数类型。为什么呢?这没有任何意义,因为我正在索引一个整数列表

我以前有过这个,使用相同的排序技术,它工作正常(除了在第一次运行后停止):

run1=0
i=0
当i
排序的
函数在python3.3中返回一个生成器,因此您不能像在
srt_hand[i]
中那样对其进行索引(这将引发异常)。

您确定这是一个整数列表吗?打印元素的类型。此外,您似乎错过了
for
循环的要点。不要使用未使用的
count
变量并手动递增和重置
i
,您应该只对范围(len(whatever)-1)内的i使用
。哦,非常正确。我已经习惯了循环hahaAlso,我相信这是一个整数列表,手动列表是从一个固定的整数列表生成的。。。所以他们不可能是别的。我看不出使用sorted()对列表进行排序对类型有什么影响。。。因为list.sort()返回None,所以我不应该使用它。打印类型。你确定这是一个整数列表并不重要;无论如何,做这件事是个好主意。此外,当您进行此操作时,完整的堆栈跟踪将非常有用。
def run_counter(card_list, num_cards):
    hand = functions1.hand_sample(card_list, num_cards)
    hand1 = functions1.hand_shuffle(card_list, num_cards)
    srt_hand = sorted(hand)
    srt_hand1 = sorted(hand1)
    prop_hand = functions1.proper_hand(srt_hand)
    prop_hand1 = functions1.proper_hand(srt_hand1)
    run1 = 0
    i = 0
    for count in range(len(srt_hand)-1):
        if srt_hand[i] == srt_hand[i+1] - 1:
            run1 += 1
    run2 = 0
    i = 0
    for count in range(len(srt_hand1)-1):
        if srt_hand1[i] == srt_hand1[i+1] - 1:
            run2 += 1
    return run1+1, run2+1, prop_hand, prop_hand1
run1 = 0
i = 0
while i < len(srt_hand)-1 and run1 < 2:
    while i < len(srt_hand)-1 and srt_hand[i] == srt_hand[i+1] - 1:
        if srt_hand[i] == srt_hand[i+1]:
            i += 1
        run1 += 1
        i += 1
    i += 1
run2 = 0
i = 0
while i < len(srt_hand1)-1 and run2 < 2:
    while i < len(srt_hand1)-1 and srt_hand1[i] == srt_hand1[i+1] - 1:
        if srt_hand[i] == srt_hand[i+1]:
            i += 1
        run2 += 1
        i += 1
    i += 1
return run1+1, run2+1, prop_hand, prop_hand1