Py学习  »  Python

python错误:“type”类型的“type error”参数不可iterable

gbenyu • 5 年前 • 1838 次点击  

我正在做一个学校项目,我必须生成一个没有重复的数字列表。扭曲是我不允许使用 random.sample() random.shuffle() . 我想我已经找到了一种方法来处理我的代码,除了我得到错误 TypeError "argument of type 'type' not iterable . 我没能避开这件事,所以我需要一些帮助。谢谢你的帮助 代码如下:

import random
lis=[]

for i in range(5):
    rand=random.randint(1,10)
    if rand not in list: lis.append(rand)

print (lis)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46118
 
1838 次点击  
文章 [ 3 ]  |  最新文章 5 年前
U10-Forward
Reply   •   1 楼
U10-Forward    6 年前

变化 if rand not in list: lis.append(rand) if rand not in lis: lis.append(rand)

告诉你为什么?

  • 检查 in 关键字 list (这不是一个iterable对象)

  • 键入:-)

所以你一定想登记 lis 名单。

Mark
Reply   •   2 楼
Mark    6 年前

这是个打字错误。替换 if rand not in list: lis.append(rand) 具有 if rand not in lis: lis.append(rand)

便笺列表->lis

Evgeny A. Mamonov
Reply   •   3 楼
Evgeny A. Mamonov    6 年前

拼错 if rand not in list: ,应该是如果 rand not in lis:

以下是工作代码:

import random
lis=[]

for i in range(5):
    rand=random.randint(1,10)
    if rand not in lis:
        lis.append(rand)

print (lis)