简体   繁体   English

创建一个包含 100 个整数的列表,其值等于它们的索引

[英]Create a list of 100 integers whose values equal their indexes

Create a list of 100 integers whose value and index are the same, eg创建一个包含 100 个值和索引相同的整数的列表,例如

mylist[0] = 0, mylist[1] = 1, mylist[2] = 2, ...

Here is my code.这是我的代码。

x_list=[]

def list_append(x_list):
    for i in 100:
        x_list.append(i)

        return(list_append())
    print(x_list)

由于没有其他人意识到您正在使用 Python 3,我将指出您应该执行list(range(100))以获得所需的行为。

Use range() for generating such a list使用 range() 生成这样的列表

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(10)[5]
5

for i in 100 doesn't do what you think it does. for i in 100不会做你认为它会做的事情。 int objects are not iterable, so this won't work. int对象不可迭代,所以这行不通。 The for-loop tries to iterate through the object given. for 循环尝试遍历给定的对象。

If you want to get a list of numbers between 0-100, use range() :如果您想获取 0-100 之间的数字列表,请使用range()

for i in range(100):
    dostuff()

The answer to your question is pretty much range(100) anyway:无论如何,您问题的答案几乎是range(100)

>>> range(100)[0]
0
>>> range(100)[64]
64

You can use range(100) , but it seems that you are probably looking to make the list from scratch, so there you can use while :您可以使用range(100) ,但似乎您可能希望从头开始制作列表,因此您可以使用while

x_list=[]
i = 0
while i<100:
    x_list.append(i)
    i += 1

Or you could do this recursively:或者您可以递归地执行此操作:

def list_append(i, L):
    L.append(i)
    if i==99:
        return L
    list_append(i+1, L)

x_list = []
list_append(0, x_list)
print x_list

也可以使用列表理解,比如

[x for x in range(100)]

If you want to import numpy you could do something like this:如果你想导入numpy你可以做这样的事情:

import numpy as np

x_list = np.arange(0, 100).tolist()

Should work in python2.7 and python3.x应该在 python2.7 和 python3.x 中工作

import random
data1=[]
def f(x):
    return(random.randrange(0,1000))
for x in range (0,100):
    data1.append(f(x))

data1

暂无
暂无

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

相关问题 从列表创建等值索引的子列表 - Create sublists of indexes of equal values from list 如何创建一个列表,该列表包含名称为串联的100个字符串 - how to create a list containing of 100 number of strings whose names are in series 创建100个随机整数的列表,返回最大值 - Create List of 100 Random Integers, Return Max Value 遍历 DataFrame 列名列表,仅将值为整数或浮点数的列名添加到新列表中 - Iterate through a list of DataFrame column names and add only the column names whose values are integers or floats to a new list 从列表中创建一个键等于值的字典 - From a list create a dictionary with Keys equal to values 如何将列表列表转换为键为整数且值为 integer 所属的子列表的索引的字典? - How can I convert a list of lists to a dictionary whose keys are integers and values are the index of the sublist to which the integer belongs? 获取等于100的百分比列表 - get list of percentages to equal 100 错误列表索引必须是整数,而不是列表。 将一个数组的值用作索引以从另一个数组中删除值 - error list indices must be integers, not list. Take values of one array use them as indexes to delete values from another array 迭代列表的索引以查看它们是否相等 - Iterating over indexes of a list to see if they are equal 如何比较列表中的5个随机整数,如果3个或更多相等,则返回不同的值 - How can I compare 5 random integers in a list and return different values if any 3 or more are equal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM