Why do I get this many iterations when adding to and removing from a set while iterating over it?

后端 未结 4 1602
忘了有多久
忘了有多久 2021-02-03 16:29

Trying to understand the Python for-loop, I thought this would give the result {1} for one iteration, or just get stuck in an infinite loop, depending on if it does

4条回答
  •  礼貌的吻别
    2021-02-03 17:16

    Python set an unordered collection which do not record element position or order of insertion. There is no index attached to any element in a python set. So they do not support any indexing or slicing operation.

    So don't expect your for loop will work in a defined order.

    Why does it do 16 iterations?

    user2357112 supports Monica already explains the main cause. Here, is another way of thinking.

    s = {0}
    for i in s:
         s.add(i + 1)
         print(s)
         s.remove(i)
    print(s)
    

    When you run this code it gives you output this :

    {0, 1}                                                                                                                               
    {1, 2}                                                                                                                               
    {2, 3}                                                                                                                               
    {3, 4}                                                                                                                               
    {4, 5}                                                                                                                               
    {5, 6}                                                                                                                               
    {6, 7}                                                                                                                               
    {7, 8}
    {8, 9}                                                                                                                               
    {9, 10}                                                                                                                              
    {10, 11}                                                                                                                             
    {11, 12}                                                                                                                             
    {12, 13}                                                                                                                             
    {13, 14}                                                                                                                             
    {14, 15}                                                                                                                             
    {16, 15}                                                                                                                             
    {16}       
    

    When we access all the elements together like loop or printing the set, there must be a predefined order for it to traverse the whole set. So, in last iteration you will see order is changed like from {i,i+1} to {i+1,i}.

    After the last iteration it happened that i+1 is already traversed so loop exit.

    Interesting Fact: Use any value less than 16 except 6 and 7 will always gives you result 16.

提交回复
热议问题