Python set traversal

Set is a set of numbers, disorder, content can not be repeated, by calling the set() method to create, then how to set set set traversal?

1. Simple set:

s1 = set(['111', '222', '333'])

For s1, it's a set of numbers. There are several ways to traverse:

function1: Traverse set sets directly in.

Function 2: iter() iterator to traverse the set

Looking at the first two methods, some people may wonder why the order of writing s1 above is different. Don't worry. Look down.

function3: This method can only output index, but not value, because set does not support the way set['cols'] is read.

function4: you can export index and value at the same time. We should understand why function1 and function2 output are 333 and 222 in turn, because 333 of index is 1, while 222 of index is 2, and output is output from subscript to small to large.

#function1
for value in s1:
    print (x)
--------Result---------
111
333
222

#function2
for value in iter(s1):
    print (value)
--------Result---------
111
333
222

#function3
for index in range(len(s1)):
    print (index)
--------Result---------
0
1
2

#function4
for index, value in enumerate(s1):
    print ('index:',index,'value:',value)
--------Result---------
index: 0 value: 111
index: 1 value: 333
index: 2 value: 222

2. complex set

s2 = set([('Xiao Ming', 149), ('Xiaolan', 120), ('Xiaohong', 140)])

As you can see, this set is a bit like a dictionary, with the feeling of key and value, so how does this set traverse? Actually, it's the same as the above method. Let's take a look at the effect.

As you can see from the following results, the order of output is actually different from what we write, which is also the characteristic of set.

Note: The element of set is tuple, so when function2 and function4, the variables of the for loop are assigned to tuple in turn.

function1 reads each set of elements and is therefore the type of data itself.

#function1
for row in s2:
    print ('index:',row[0],'value:',row[1])
    print('type:',type(row[0]),'type:',type(row[1]))
------------Result-------------
index: Xiaolan value: 120
type: <class 'str'> type: <class 'int'>
index: Xiao Ming value: 149
type: <class 'str'> type: <class 'int'>
index: Xiaohong value: 140
type: <class 'str'> type: <class 'int'>


#function2
for value in iter(s2):
    print (value)
    print('type:',type(value))
------------Result-------------
('Xiaolan', 120)
type: <class 'tuple'>
('Xiao Ming', 149)
type: <class 'tuple'>
('Xiaohong', 140)
type: <class 'tuple'>


#function3
for index in range(len(s2)):
    print (index)
------------Result-------------
0
1
2

#function4
for index, value in enumerate(s2):
    print ('index:',index,'value:',value)
    print('type:',type(index),'type:',type(value))
------------Result-------------
index: 0 value: ('Xiaolan', 120)
type: <class 'int'> type: <class 'tuple'>
index: 1 value: ('Xiao Ming', 149)
type: <class 'int'> type: <class 'tuple'>
index: 2 value: ('Xiaohong', 140)
type: <class 'int'> type: <class 'tuple'>

These are two forms of set traversal, there may be a better way, you are welcome to exchange at any time.

 

Posted by Maugrim_The_Reaper on Sun, 06 Oct 2019 07:59:39 -0700