Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么这段代码好像卡在一个无限循环中?_Python_List_Nested Loops - Fatal编程技术网

Python 为什么这段代码好像卡在一个无限循环中?

Python 为什么这段代码好像卡在一个无限循环中?,python,list,nested-loops,Python,List,Nested Loops,我正在尝试创建条目(1000),我从名称开始。我想出了一些名字,我计划复制添加了数字0-9的条目,以创建更独特的名字。所以我在for循环中使用了for循环。无法将索引更改为字符串并将其添加到列表中项目的末尾 我考虑过增量,因为最近我一直在C++中编写很多代码,但是这不起作用,因为在使用Python的范围函数时不需要增加。我想改变循环的顺序 name = ['event', 'thing going on', 'happening', 'what everyones talkin', 'that

我正在尝试创建条目(1000),我从名称开始。我想出了一些名字,我计划复制添加了数字0-9的条目,以创建更独特的名字。所以我在for循环中使用了for循环。无法将索引更改为字符串并将其添加到列表中项目的末尾

我考虑过增量,因为最近我一直在C++中编写很多代码,但是这不起作用,因为在使用Python的范围函数时不需要增加。我想改变循环的顺序

name = ['event', 'thing going on', 'happening', 'what everyones talkin', 'that thing', 'the game', 'the play', 'outside time', 'social time', 'going out', 'having fun']
我希望它能打印出
['event0','thinggoing1',…'havingfun10']


谢谢大家!

使用一个新列表并附加到那里:

newName = []
for index in range(10): 
    for item in name:
        newName.append(item+str(index))
return newName

使用列表理解

  • -方法将计数器添加到iterable,并以枚举对象的形式返回它
Ex.

name = ['event', 'thing going on', 'happening', 'what everyones talkin', 'that thing', 'the game', 'the play', 'outside time', 'social time', 'going out', 'having fun']
new_list = [x+str(index) for index,x in enumerate(name)]
print(new_list)
O/p:

['event0', 'thing going on1', 'happening2', 'what everyones talkin3', 'that thing4', 'the game5', 'the play6', 'outside time7', 'social time8', 'going out9', 'having fun10']
['event0', 'thing going on1', 'happening2', 'what everyones talkin3', 'that thing4', 'the game5', 'the play6', 'outside time7', 'social time8', 'going out9', 'having fun10']

它将按照您的预期工作:

name = ['event', 'thing going on', 'happening', 'what everyones talkin', 'that thing', 
'the game', 'the play', 'outside time', 'social time', 'going out', 'having fun']
for index in range(10): 
    name[index] = name[index]+str(index)
print (name)
输出:

['event0', 'thing going on1', 'happening2', 'what everyones talkin3', 'that thing4', 'the game5', 'the play6', 'outside time7', 'social time8', 'going out9', 'having fun10']
['event0', 'thing going on1', 'happening2', 'what everyones talkin3', 'that thing4', 'the game5', 'the play6', 'outside time7', 'social time8', 'going out9', 'having fun10']

每次运行循环时,都会向列表中添加更多元素。创建一个新的空列表并将元素附加到该列表中。这样,您的初始列表保持不变

内部for循环正在向数组追加新元素

你只需要

for index in range(10): 
    name[index] = name[index] + str(index)
现在,您的数组包含了预期的输出。顺便说一句,这会更改原始数组。如果希望它保持不变,请执行以下操作

newArray = []
for index in range(10): 
        newArray [index] = name[index] + str(index)

您正在将新项附加到要迭代的列表中。所以迭代将无限期地继续下去。