简体   繁体   中英

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. 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. list is the name of the builtin list class, which is an instance of 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]) . list1 is the name you bound to your list object created by createlist(10000) .

The trick to finding these sort of bugs is to look at the error message Python gave you. 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.

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.

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)
  2. List comprehensions to create lists, instead of repeated calls to 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.

使用列表理解(它的方式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):

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

List comprehension is fast in Python because it is executed by underlying C code. Moreover, using xrange is more memory-efficient.

Note: By convention, when the loop-control variable is not used in Python, it is named _ :

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

First, you don't put a paren by while unless you're evaluating complex expressions.

Next, you used a semicolon to return your blist . Not necessary.

Finally...why not use a for loop?

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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