How to clone or copy a set in Python?
问题 For copying a list: shallow_copy_of_list = old_list[:] . For copying a dict: shallow_copy_of_dict = dict(old_dict) . But for a set , I was worried that a similar thing wouldn't work, because saying new_set = set(old_set) would give a set of a set? But it does work. So I'm posting the question and answer here for reference. In case anyone else has the same confusion. 回答1: Both of these will give a duplicate of a set: shallow_copy_of_set = set(old_set) Or: shallow_copy_of_set = old_set.copy()