简体   繁体   English

根据字典中的键和值修改循环

[英]Modify a loop based on key and value in dictionary

Im new to python I wrote the code below, to search in a dictionary, do something, clear old items in dictionary and update dictionary with new key and values and break while there is noting to add to dictionary (it is empty), how can I modify my code to do this process?我是 python 的新手,我写了下面的代码,在字典中搜索,做一些事情,清除字典中的旧项目并用新的键和值更新字典,并在没有注意添加到字典时中断(它是空的),怎么能我修改我的代码来做这个过程?

#since_id - Returns results with an ID greater than 
#(that is, more recent than) the specified ID. There are limits to the 
#number of Tweets which can be accessed through the API.
# If the limit of Tweets has occured since the since_id,
# the since_id will be forced to the oldest ID available. 
# max_id - Returns results with an ID less than (that is, older than) 
#or equal to the specified ID.

Dict2 = dict({'@TweeetLorraine':1392217841680764931})
d2 = {}
rep=[] 
from tqdm import tqdm
for key, value in tqdm(Dict2.items()):
  for i in tweepy.Cursor(api.search,
                     q='to:{} -filter:retweets"'.format(key),lang="en"
                     ,since_id=value,tweet_mode='extended',
                     wait_on_rate_limit=True,
                     wait_on_rate_limit_notify=True).items(50):
                     if (i.in_reply_to_status_id == value):
                       rep.append(i)


                       from pandas.io.json import json_normalize
                       dfflat = pd.DataFrame()
                       for tweet in rep:
                         df_for_tweet = json_normalize(tweet._json)
                         dfflat=dfflat.append(df_for_tweet,ignore_index=True,sort=True)

                         d2.update(zip(dfflat["user.screen_name"].tolist(), dfflat["id"].tolist()))





d2 ```

You can use a while loop for that:您可以为此使用while循环:

#since_id - Returns results with an ID greater than 
#(that is, more recent than) the specified ID. There are limits to the 
#number of Tweets which can be accessed through the API.
# If the limit of Tweets has occured since the since_id,
# the since_id will be forced to the oldest ID available. 
# max_id - Returns results with an ID less than (that is, older than) 
#or equal to the specified ID.

Dict2 = dict({'@TweeetLorraine':1392217841680764931})
d2 = {}
rep=[] 
from tqdm import tqdm
for key, value in tqdm(Dict2.items()):
  for i in tweepy.Cursor(api.search,
                     q='to:{} -filter:retweets"'.format(key),lang="en"
                     ,since_id=value,tweet_mode='extended',
                     wait_on_rate_limit=True,
                     wait_on_rate_limit_notify=True).items(50):
                     if (i.in_reply_to_status_id == value):
                       rep.append(i)


                       from pandas.io.json import json_normalize
                       dfflat = pd.DataFrame()
                       for tweet in rep:
                         df_for_tweet = json_normalize(tweet._json)
                         dfflat=dfflat.append(df_for_tweet,ignore_index=True,sort=True)

                         d2.update(zip(dfflat["user.screen_name"].tolist(), dfflat["id"].tolist()))





d2

For your use case, here is roughly the code that does what you describe, there is better ways to do that using map, I let you search for it if you want to know more.对于您的用例,这里大致是执行您描述的代码,使用 map 有更好的方法,如果您想了解更多信息,我让您搜索它。

Also, I'm not sure whether you want to completely clear the dict or only clear the current "i", but I think you can modify the following snippet to your true needs另外,我不确定您是要完全清除 dict 还是只清除当前的“i”,但我认为您可以根据自己的实际需要修改以下代码段

mydict = initial_dict
# while there is something in the dictionary
while mydict:
    value_searched = None
    for key, value in mydict.items():
        for i in tweepy.Cursor(api.search,
                     q='to:{} -filter:retweets"'.format(key),lang="en"
                     ,since_id=value,tweet_mode='extended',
                     wait_on_rate_limit=True,
                     wait_on_rate_limit_notify=True).items(50):
                     if (i.in_reply_to_status_id == value):
                       replies3.append(i)
                       value_searched = i
                       break
         break

    # create new dict from value retrieved
    mydict = {"@" +value_searched.user.screen_name : value_searched.id_str} 

Edit2: Using recursivity Edit2:使用递归

def tweepy_stub(key, value):
    if key == "TweeetLorraine" and value == 1392217841680764931:
        return [
            ("AlexBC997", 1392385334155956226),
            ("ChapinDolores", 1392432099945238529),
        ]
    elif key == "AlexBC997" and value == 1392385334155956226:
        return [("test", 139238533415595852)]
    elif ("ChapinDolores", 1392432099945238529):
        return []


def recursive(list_values, nb_recursion):
    mydict = {}
    if list_values == None or nb_recursion == 0:
        return mydict
    else:
        for name_user, tweet_id in list_values:
            mydict[(name_user, tweet_id)] = recursive(
                retrieve_direct_reply_stub(name_user, tweet_id), nb_recursion - 1
            )
        return mydict

class stub_tweepy_answer:
    def __init__(self, status_id) -> None:
        self.in_reply_to_status_id = status_id

def retrieve_direct_reply_stub(name_user, tweepy_id):
    rep = []
    d2 = []
    return tweepy_stub(name_user, tweepy_id)

def retrieve_direct_reply(name_user, tweet_id):
    rep = []
    d2 = []
    for i in tweepy_stub(name_user, tweet_id):
        val = i
        if (i.in_reply_to_status_id == tweet_id):
            rep.append(i)
            from pandas.io.json import json_normalize
            dfflat = pd.DataFrame()
            for tweet in rep:
                df_for_tweet = json_normalize(tweet._json)
                dfflat=dfflat.append(df_for_tweet,ignore_index=True,sort=True)

                d2.append(zip(dfflat["user.screen_name"].tolist(), dfflat["id"].tolist()))
    return d2

#print(retrieve_direct_reply_stub("TweeetLorraine", 1392217841680764931))

elem = [("TweeetLorraine", 1392217841680764931)]
print(recursive(elem, 3))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM