Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.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_Python 3.x_Python 2.7_Loops_For Loop - Fatal编程技术网

Python 如何在代码中的列表中添加列表

Python 如何在代码中的列表中添加列表,python,python-3.x,python-2.7,loops,for-loop,Python,Python 3.x,Python 2.7,Loops,For Loop,我曾试图刮Instagram,我已经实现了刮的目标,但我得到的结果是完美的,但我希望它存储在列表中的列表中 代码:- Post links = ['https://www.instagram.com/p/BesW08pHfUt', 'https://www.instagram.com/p/BQZyTtej4yj'] for post_link in post_links: _ = API.getMediaComments(get_media_id(post_link

我曾试图刮Instagram,我已经实现了刮的目标,但我得到的结果是完美的,但我希望它存储在列表中的列表中

代码:-

Post links = ['https://www.instagram.com/p/BesW08pHfUt', 'https://www.instagram.com/p/BQZyTtej4yj']

      for post_link in post_links:
        _ = API.getMediaComments(get_media_id(post_link), max_id = 100)
        for c in reversed(API.LastJson['comments']):
            comment.append(c["user"]["username"])
我从Instagram的每个帖子链接中得到的评论

'https://www.instagram.com/p/BesW08pHfUt':-  'headhotel', 'famegalore', 'motivationpoem', 'malicioussatan'

'https://www.instagram.com/p/BQZyTtej4yj':- 'monarch_motivation', 'headhotel', 'motivationpoem'
我得到的输出

['headhotel', 'famegalore', 'motivationpoem', 'malicioussatan', 'monarch_motivation', 'headhotel', 'motivationpoem']
我想要的输出

[['headhotel', 'famegalore', 'motivationpoem', 'malicioussatan'], ['monarch_motivation', 'headhotel', 'motivationpoem']]

我知道这有点容易,但我已经在2天内编码了这个刮刀,所以我有点困惑

我不熟悉该API,但我认为您希望执行以下操作:

for post_link in post_links:
    _ = API.getMediaComments(get_media_id(post_link), max_id = 100)
    sublist = []
    for c in reversed(API.LastJson['comments']):
        sublist.append(c["user"]["username"])
    comment.append(sublist)

这将在外部循环的每次迭代中创建一个新的子列表,内部循环将填充该子列表,然后我们将该子列表附加到主
注释
列表中。

我们不知道您提到的注释或输出来自何处。您的代码片段描述不足。
=API.getMediaComments(获取媒体id(post链接),max\u id=100)
做什么?更准确地说,为什么您要将该调用的返回值存储在
\uu
中,然后不使用它?@PM2Ring it从InstagramIf的post链接获取注释如果您不需要调用的返回值,那么就不分配它。只需执行
API.getMediaComments(获取媒体id(post\u链接),max\u id=100)
。该代码在后续代码中需要,与我的问题完全无关