Python-Ref > Data structures > Sets > Set querying and consumption
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Set querying and consumption

Expand/Shrink
21
46
27
3
28
42
2
49
27
44
8
8
30
13
42
3
21
8
39
4
38
22
38
17
32
10
2
39
46
29
3
48
38
1
25
41
42
49
26
20
47
30
41
30
24
30
4
42
39
38
17
30
37
47
20
30
19
8
27
5
1
30
24
1
26
3
15
32
33
10
1
21
15
15
15
18
28
28
24
33
24
2
16
35
14
26
15
0
43
5
24
3
8
6
14
17
33
36
32
44
Zdroj: (sets4-1.py)
  1   f = file( "100numbers.txt", "r")
  2   
  3   numbers = set()
  4   for line in f:
  5     numbers.add( int( line.strip()))
  6   
  7   print 41 in numbers
  8   if 13 in numbers:
  9     print "ok"
 10   print 7 in numbers
stdout:
True
ok
False
Doba běhu: 19.5 ms
Expand/Shrink
21
46
27
3
28
42
2
49
27
44
8
8
30
13
42
3
21
8
39
4
38
22
38
17
32
10
2
39
46
29
3
48
38
1
25
41
42
49
26
20
47
30
41
30
24
30
4
42
39
38
17
30
37
47
20
30
19
8
27
5
1
30
24
1
26
3
15
32
33
10
1
21
15
15
15
18
28
28
24
33
24
2
16
35
14
26
15
0
43
5
24
3
8
6
14
17
33
36
32
44
Zdroj: (sets4-2.py)
  1   f = file( "100numbers.txt", "r")
  2   
  3   numbers = set()
  4   for line in f:
  5     numbers.add( int( line.strip()))
  6   
  7   # cannot ask about a particular index
  8   print numbers[0]
stderr:
Traceback (most recent call last):
  File "sets4-2.py", line 8, in <module>
    print numbers[0]
TypeError: 'set' object is unindexable
Doba běhu: 22.0 ms
Expand/Shrink
21
46
27
3
28
42
2
49
27
44
8
8
30
13
42
3
21
8
39
4
38
22
38
17
32
10
2
39
46
29
3
48
38
1
25
41
42
49
26
20
47
30
41
30
24
30
4
42
39
38
17
30
37
47
20
30
19
8
27
5
1
30
24
1
26
3
15
32
33
10
1
21
15
15
15
18
28
28
24
33
24
2
16
35
14
26
15
0
43
5
24
3
8
6
14
17
33
36
32
44
Zdroj: (sets4-3.py)
  1   f = file( "100numbers.txt", "r")
  2   
  3   numbers = set()
  4   for line in f:
  5     numbers.add( int( line.strip()))
  6   
  7   # cannot ask about index either
  8   print numbers.index( 13)
stderr:
Traceback (most recent call last):
  File "sets4-3.py", line 8, in <module>
    print numbers.index( 13)
AttributeError: 'set' object has no attribute 'index'
Doba běhu: 18.6 ms
Expand/Shrink
21
46
27
3
28
42
2
49
27
44
8
8
30
13
42
3
21
8
39
4
38
22
38
17
32
10
2
39
46
29
3
48
38
1
25
41
42
49
26
20
47
30
41
30
24
30
4
42
39
38
17
30
37
47
20
30
19
8
27
5
1
30
24
1
26
3
15
32
33
10
1
21
15
15
15
18
28
28
24
33
24
2
16
35
14
26
15
0
43
5
24
3
8
6
14
17
33
36
32
44
Zdroj: (sets4-4.py)
  1   f = file( "100numbers.txt", "r")
  2   
  3   numbers = set()
  4   for line in f:
  5     numbers.add( int( line.strip()))
  6   
  7   # we can destructively consume the set
  8   print len( numbers)
  9   num1 = numbers.pop()
 10   print num1
 11   print len( numbers)
 12   print "----------"
 13   for i in range( 10):
 14     print numbers.pop(), len( numbers)
stdout:
41
0
40
----------
1 39
2 38
3 37
4 36
5 35
6 34
8 33
10 32
13 31
14 30
Doba běhu: 23.6 ms