简体   繁体   中英

How do I deal with a dictionary of objects that is constantly changing in Python?

The current situation: I'm using Discord's API to retrieve a dictionary of member objects in my server. This dictionary is constantly changing in size as new members join and old members leave.

I currently have a program that has a run time of around 30 minutes and accesses this dictionary of member objects so it's guaranteed that this dictionary changes size as I iterate over it in my program; this causes an error in my for loop. I also can't seem to deepcopy this dictionary;

TypeError: can't pickle dict_values objects

Any ideas of how I can work around this problem?

Code:
for i in members: do something; <--- while this is happening members changes in size
Trying this also doesn't work:
temp = copy.deepcopy(members)
This is what the dict is: dict_values([ <discord.member.Member object at 0x1094b3268 >, <discord.member.Member object at 0x1094b32f0 >, etc

这是我最终解决问题的方式:
temp = copy.deepcopy(list(members))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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