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 - Fatal编程技术网

Python:将元素添加到列表中的问题

Python:将元素添加到列表中的问题,python,list,Python,List,我在将函数打印的结果添加到列表中时遇到了一个小问题。情况是这样的。该函数打印一些结果,如下所示 result1 result2 result3 如何将它们添加到列表中,以便像查看mylist=['result1','result2','result3']那样查看它们?因为,在我的例子中,我使用的是for循环,它使用我编写的函数计算每种情况(并像上面那样打印结果),但使用append函数,它打印如下内容: ['result1'], ['result1', 'result2'], ['resul

我在将函数打印的结果添加到列表中时遇到了一个小问题。情况是这样的。该函数打印一些结果,如下所示

result1
result2
result3
如何将它们添加到列表中,以便像查看mylist=['result1','result2','result3']那样查看它们?因为,在我的例子中,我使用的是for循环,它使用我编写的函数计算每种情况(并像上面那样打印结果),但使用append函数,它打印如下内容:

 ['result1'], ['result1', 'result2'], ['result1', 'result2', 'result3']. 
mylist=['https://myapi.net/file1', 'https://myapi.net/file2', 'https://myapi.net/file3', 'https://myapi.net/file4']
l = []
for i in range(1,4):
    l.append("result {0}".format(i))
    print l
当然,使用最后一个结果,
['result1','result2','result3']
,对我来说应该是完美的,但是我得到了三个类似上面的列表。我该怎么做才能得到最后一个列表,即包含这三个结果的列表

好了,伙计们,我要在这里放一些代码,更具体一些。我有一个很大的字符串(我不打算写在这里,因为它并不重要)。我的函数正在从该字符串中拆分某些特定的url。其功能是:

def fileref(string, file):
    start=string.find(file) + 28
    end=string.find('</ref', start)
    file=string[start:end]
    return file
然后,我使用for循环执行以下操作:

for i in files55_2:
    print fileref(string, i)
它会将这些URL打印给我:

https://myapi.net/file1
https://myapi.net/file2
https://myapi.net/file3
https://myapi.net/file4
现在我需要一个包含所有这些元素的列表,如下所示:

 ['result1'], ['result1', 'result2'], ['result1', 'result2', 'result3']. 
mylist=['https://myapi.net/file1', 'https://myapi.net/file2', 'https://myapi.net/file3', 'https://myapi.net/file4']
l = []
for i in range(1,4):
    l.append("result {0}".format(i))
    print l

我希望我现在更具体一些。谢谢你的回答

在您的函数中,您正在以增量方式构建列表,并在每个步骤后打印出列表,如下所示:

 ['result1'], ['result1', 'result2'], ['result1', 'result2', 'result3']. 
mylist=['https://myapi.net/file1', 'https://myapi.net/file2', 'https://myapi.net/file3', 'https://myapi.net/file4']
l = []
for i in range(1,4):
    l.append("result {0}".format(i))
    print l
输出:

['result 1']
['result 1', 'result 2']
['result 1', 'result 2', 'result 3']
['result 1', 'result 2', 'result 3']
您要做的是在创建完列表后将其打印出来,例如:

l = []
for i in range(1,4):
    l.append("result {0}".format(i))
print l
输出:

['result 1']
['result 1', 'result 2']
['result 1', 'result 2', 'result 3']
['result 1', 'result 2', 'result 3']
编辑:在提供的代码中,更改以下行:

for i in files55_2:
    print fileref(string, i)
致:

在for循环中,您可以使用“else”close,它在循环完成后执行:

for ...
   ...
else
   //here you can add your results to list after loop has finished

最好让函数将结果附加到列表中,然后在函数外部打印

>>> def append():
...   l = []
...   for i in range(3):
...     l.append(str(i))
...   return l
...
>>> l = append()
>>> l
['0', '1', '2']
或者,如果在运行函数之前已经有了列表,则无需返回它

>>> def append(l):
...   for i in range(3):
...     l.append(str(i))
...
>>> l = []
>>> append(l)
>>> l
['0', '1', '2']

你能举一个你正在使用的函数的例子吗?此外,这可能有助于在这方面增加一些差距;)编辑-有人已经在你打印循环中设置了间隙,打印的增量列表让你感觉有三个列表?或者,您实际上得到了三个列表对象?请输入密码。我认为没有人能像你说的那样理解这个问题。最多有人能做的就是猜你的问题。请至少从您的函数和使用它的代码中添加一些代码片段,包括输入和输出。@BioGeek:在发布问题之前,我尝试过这样做,它显示了列表的相同增量。