简体   繁体   English

创建列表时语法无效

[英]Invalid syntax when creating lists

So i am attempting to create 3 different lists one of 1k, one of 10k, and one of 100k items long populated with random numbers from 1 to 10million but i cant seem to figure out why it keeps saying its invalid syntax. 因此,我试图创建3个不同的列表之一,1k,10k之一,以及100k项目中的一个,长度填充了1到1000万的随机数,但我似乎无法弄清楚为什么它一直说它无效的语法。 So far i have: 到目前为止,我有:

edit: okay it seems to have fixed the invalid syntax problem with some tweaking but still gives me: 编辑:好吧它似乎修复了无效的语法问题与一些调整但仍然给了我:

Traceback (most recent call last):
  File "C:/Python32/funtime.py", line 16, in <module>
    print (list[1])
TypeError: 'type' object is not subscriptable

this is exactly what i have typed in: 这正是我输入的内容:

import random

def createlist(a):

    blist =[]
    count=0
    while count <= a:

        blist.append(random.randint(1,10000000))
        count= count+1
    return blist;


list1= createlist(10000);

print (list[1])
Traceback (most recent call last):
  File "C:/Python32/funtime.py", line 16, in <module>
    print (list[1])
TypeError: 'type' object is not subscriptable

Objects of type type are indeed not subscriptable. 类型type对象确实不是可订阅的。 list is the name of the builtin list class, which is an instance of type . list是内置列表类的名称,它是type的实例。 It doesn't make any sense to subscript it, so that's clearly not what you intended to do. 下标它没有任何意义,因此显然不是你打算做的。

You meant print (list1[1]) . 你的意思是print (list1[1]) list1 is the name you bound to your list object created by createlist(10000) . list1是绑定到createlist(10000)创建的列表对象的名称。

The trick to finding these sort of bugs is to look at the error message Python gave you. 找到这些错误的诀窍是查看Python给你的错误消息。 It's telling you exactly the problem line, and exactly why it's a problem. 它告诉你确切的问题,以及它为什么是一个问题。 All that was missing was the realisation that list is not the name of the object you wanted to subscript. 所有缺失的是实现list不是您想要下标的对象的名称。

The following works fine on my system. 以下在我的系统上工作正常。

import random

def createlist(a):

    blist =[]
    count= 0
    while count <= a:
        blist.append(random.randint(1,1000000))
        count=count+1
    return blist

list1= createlist(10000)

print list1[1]

http://ideone.com/SL2bL <-- try it here. http://ideone.com/SL2bL < - 在这里试试吧。

I'd probably do it like this 我可能会这样做

import random

def createlist(a):

    return [random.randint(1,1000000) for i in xrange(a)]


list1= createlist(10000)

print list1[1]

Or probably just skip the function.. 或者可能只是跳过这个功能..

list1 = [random.randint(1,1000000) for i in xrange(a)]
print list1[1]

A much shorter version would be: 更短的版本是:

>>> import random
>>> def create_list(length):
...     return [random.randint(1,1000000) for _ in range(length)]

This demonstrates a couple of things: 这表明了一些事情:

  1. A much more compact way to loop x times (the for _ in range(length) idiom) 循环x次的更紧凑的方法( for _ in range(length) idiom)
  2. List comprehensions to create lists, instead of repeated calls to append . 列表推导创建列表,而不是重复调用append
  3. The use of _ as a variable name when you need the variable, but not the actual data in it. 当您需要变量时,使用_作为变量名,而不是其中的实际数据。 This is a somewhat common convention that crops up most often in list comprehensions. 这是一种常见的惯例,在列表推导中最常出现。 It isn't a problem not to use it, but it crops up often enough that it pays to be aware of it. 不使用它不是一个问题,但它经常出现足以让人意识到它。

Hat-tip to @mgilson for his comment on another answer that reminded me the _ convention. 给@mgilson提示他对另一个答案的评论,这个答案让我想起了_惯例。

使用列表理解(它的方式betta,让你看起来亲,哈哈)

[random.randint(1, 1000000) for i in range(10000)]

As some other answers have stated, you could easily use list comprehension for something like this (the so-called Pythonic way): 正如其他一些答案所述,你可以轻松地将列表理解用于这样的事情(所谓的Pythonic方式):

somelist = [random.randint(1, 1000000) for i in xrange(10000)]

List comprehension is fast in Python because it is executed by underlying C code. 列表理解在Python中很快,因为它由底层C代码执行。 Moreover, using xrange is more memory-efficient. 而且,使用xrange可以提高内存效率。

Note: By convention, when the loop-control variable is not used in Python, it is named _ : 注意:按照惯例,当Python中没有使用循环控制变量时,它被命名为_

somelist = [random.randint(1, 1000000) for _ in xrange(10000)]

First, you don't put a paren by while unless you're evaluating complex expressions. 首先,除非您正在评估复杂的表达式while否则您不会使用paren。

Next, you used a semicolon to return your blist . 接下来,您使用分号返回blist Not necessary. 不必要。

Finally...why not use a for loop? 最后......为什么不使用for循环?

import random

def createlist(a): 
    blist =[]

    for x in xrange(a):
        blist.append(random.randint(1,1000000))

    return blist

list1= createlist(10000)

print list1[0]

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

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