首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用整数列表时,不支持-的操作数类型:'str‘和'int’

使用整数列表时,不支持-的操作数类型:'str‘和'int’
EN

Stack Overflow用户
提问于 2013-11-15 10:03:11
回答 1查看 130关注 0票数 0
代码语言:javascript
复制
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

我有自己生成的一手牌,并存储在排序列表'srt_hand‘和'srt_hand1’中。我做了一个函数来计算手中游程的长度。我去测试它,但它总是说'if srt_handi == srt_handi+1 - 1:‘是一个不支持的操作数类型。为什么会这样呢?这没有任何意义,因为我索引的是一个整数列表...

我以前用过同样的排序技术,它起作用了(除了它在第一次运行后停止):

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2013-11-15 10:07:28

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19992192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档