【问题标题】:Create a list from a loop using each element of a list使用列表的每个元素从循环创建列表
【发布时间】:2020-11-14 13:53:29
【问题描述】:

我想为 usr 列表中的每个用户 ID 创建一个 friends_list。这是我到目前为止所尝试的。我不知道如何在一个列表中为每个人或什至全部人调用friends_list。

 def first_surface(friends):
     usr = ['298970145', '11922952', '17455195']

     for i in usr: 
    
         friends_list = []
    
         for user in tweepy.Cursor(api.friends, user_id = usr).items(15):
             friends_list.append(user.id)

  return

【问题讨论】:

    标签: python list loops arraylist


    【解决方案1】:

    这行得通吗?它创建一个朋友列表的列表

     def first_surface(friends):
         usr = ['298970145', '11922952', '17455195']
         list_of_friends = []
    
         for i in usr:         
             friends_list = []        
             for user in tweepy.Cursor(api.friends, user_id = usr).items(15):
                 friends_list.append(user.id)
             list_of_friends.append(friends_list)
    
      return list_of_friends
    

    如果您想知道第一个用户的好友列表,只需list_of_friends[0]

    【讨论】:

    • 感谢您的回复,这不是我想要的。 :)
    【解决方案2】:

    我用下面的代码得到了我想要的:

    friends_list = []
    
    usr_all = ['298970145', '11922952', '17455195'];
    
    for usr in usr_all:
    
      for user in tweepy.Cursor(api.friends, user_id = usr).items(15):
        friends_list.append(user.id)
    

    但是,例如,我仍然不知道如何检索列表中用户 1 的friend_list。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 2015-01-31
      • 2013-07-16
      相关资源
      最近更新 更多