To‘plamlar bilan ishlash
To‘plamdan nusxa olish, to‘plamni tozalash, kalitini aniqlash uchun metodlardan foydalaniladi.
Metodlar
|
Tavsifi
|
A.copy()
|
To‘plam nusxasini oladi.
|
A.add(item)
|
To‘plamga element qo‘shadi.
|
A.remove(item)
|
To‘plamdan elementni o‘chiradi. Agar element mavjud bo‘lmasa, KeyError xatoligini qaytaradi.
|
A.discard(item)
|
To‘plamdan elementni o‘chiradi. Agar element mavjud bo‘lmasa, hech qanday xatolik qaytarmaydi. Shu sababli “remove” ga nisbatan tezroq ishlaydi.
|
A.pop()
|
To‘plamdan elementni kalit orqali o‘chiradi.
|
A.clear()
|
To‘plamni tozalaydi.
|
A.isdisjoint(B)
|
A to‘plami bilan B to‘plami umumiy elementga ega bo‘lmasa, True qiymatini qaytaradi
|
A==B
|
A to‘plamining barcha elementlarini B to‘plam oladi, B to‘plamining barcha elementlarini esa A to‘plam oladi.
|
len(A)
|
To‘plam uzunligi, ya’ni elementlar sonini qaytaradi.
|
item in A
|
item elementi A to‘plamga tegishli yoki tegishli emasligini aniqlaydi.
|
Dastur kodi:
01 >>> orig_sets={0, 1, 3, 5, 6, 7}
02 >>> cp_sets=orig_sets.copy()
03 >>> print(cp_sets)
{0, 1, 3, 5, 6, 7}
04 >>> orig_fsets=frozenset({0, 2, 3, 4, 8, 9})
05 >>> cp_fsets=orig_fsets.copy()
06 >>> print(cp_fsets)
frozenset({0, 2, 3, 4, 8, 9})
-------------------------------------------------------------
Dastur kodi:
07 >>> >>> sets={0, 1, 2, 3, 7}
08 >>> >>> sets.add(10)
09 >>> >>> print(sets)
{0, 1, 2, 3, 7, 10}
10 >>> >>> sets.add(5)
11 >>> >>> print(sets)
{0, 1, 2, 3, 5, 7, 10}
12 >>> >>> sets.add(2)
13 >>> >>> print(sets)
{0, 1, 2, 3, 5, 7, 10}
14 >>> >>> sets.add([1, 2])
Traceback (most recent call last):
File "
", line 1, in
sets.add([1, 2])
TypeError: unhashable type: 'list'
15 >>> >>> sets.add((1, 2))
16 >>> >>> print(sets)
{0, 1, 2, 3, (1, 2), 5, 7, 10}
17 >>> >>> sets_f=frozenset({1, 2, 3, 4})
18 >>> >>> sets_f.add(5)
Traceback (most recent call last):
File "
", line 1, in
sets_f.add(5)
AttributeError: 'frozenset' object has no attribute 'add'
19 >>> >>> sets_f.add([5])
Traceback (most recent call last):
File "
", line 1, in
sets_f.add([5])
AttributeError: 'frozenset' object has no attribute 'add'
Bunda:
(12) Metod mavjud elementlarni qo‘shishni e’tiborsiz qoldiradi.
(14) Oʻzgaruvchan obyektlarni qoʻshib boʻlmaydi.
-------------------------------------------------------------
Dastur kodi:
19 >>> sets={0, 1, 2, 3, (1, 2), 5, 7, 10}
20 >>> sets.remove(5)
21 >>> print(sets)
{0, 1, 2, 3, (1, 2), 7, 10}
22 >>> sets.remove((1, 2))
23 >>> print(sets)
{0, 1, 2, 3, 7, 10}
24 >>> sets.remove(4)
Traceback (most recent call last):
File "
", line 1, in
sets.remove(4)
KeyError: 4
25 >>> sets.add(frozenset({'a', 'c'}))
26 >>> print(sets)
{0, 1, 2, 3, frozenset({'c', 'a'}), 7, 10}
27 >>> sets.remove({'c', 'a'})
28 >>> print(sets)
{0, 1, 2, 3, 7, 10}
Bunda:
(24) Element to'plamda mavjud emas.
(27) Ichma-ich joylashgan toʻplamni ham oʻchirish mumkin.
-------------------------------------------------------------
Dastur kodi:
28 >>> sets={0, 1, 2, 3, (1, 2), 5, 7, 10}
29 >>> sets.discard(5)
30 >>> print(sets)
{0, 1, 2, 3, (1, 2), 7, 10}
31 >>> sets.discard((1, 2))
32 >>> print(sets)
{0, 1, 2, 3, 7, 10}
33 >>> sets.discard(4)
34 >>> print(sets)
{0, 1, 2, 3, 7, 10}
35 >>> sets.add(frozenset({'a', 'c'}))
36 >>> print(sets)
{0, 1, 2, 3, frozenset({'c', 'a'}), 7, 10}
37 >>> sets.discard({'c', 'a'})
38 >>> print(sets)
{0, 1, 2, 3, 7, 10}
Bunda:
(33) Element to'plamda mavjud emas. Xatolik yuz bermaydi.
(37) Ichma-ich joylashgan toʻplamni ham oʻchirish mumkin.
-------------------------------------------------------------
Dastur kodi:
39 >>> sets={1, 2, 3, (1, 2), 5}
40 >>> sets.pop()
1
41 >>> print(sets)
{2, (1, 2), 3, 5}
42 >>> sets.pop()
2
43 >>> print(sets)
{(1, 2), 3, 5}
44 >>> sets.pop()
(1, 2)
45 >>> print(sets)
{3, 5}
46 >>> sets.pop()
3
47 >>> print(sets)
{5}
48 >>> sets.pop()
5
49 >>> print(sets)
set()
50 >>> sets.pop()
Traceback (most recent call last):
File "
", line 1, in
sets.pop()
KeyError: 'pop from an empty set'
Bunda:
(50) Agar to‘plam bo‘sh bo‘lsa, KeyError xatolikni beradi.
-------------------------------------------------------------
Dastur kodi:
51 >>> sets={0, 1, 2, 3, (1, 2), 5, 7, 10}
52 >>> sets.clear()
53 >>> print(sets)
set()
Bunda:
(52) To‘plamni tozalaydi va hech qanday natija bermaydi. set.clear() usuli xatolik (istisno) qo‘ymaydi.
-------------------------------------------------------------
Dastur kodi:
54 >>> set_x={0, 1, 2, 3, 4}
55 >>> list_y=[5, 6, 7, 8, 9]
56 >>> set_x.isdisjoint(list_y)
True
57 >>> list_y[0]=4
58 >>> print(list_y)
[4, 6, 7, 8, 9]
59 >>> set_x.isdisjoint(list_y)
False
60 >>> fset=frozenset(['march', 'dec', 'feb', 'may'])
61 >>> tuple_y=('july', 'aug', 'june', 'jan', 'may')
62 >>> fset.isdisjoint(tuple_y)
False
63 >>> fset=frozenset(['march', 'dec', 'feb'])
64 >>> fset.isdisjoint(tuple_y)
True
-------------------------------------------------------------
Dastur kodi:
65 >>> A={0, 1, 2, 3, 4}
66 >>> B={5, 6, 7, 8, 9}
67 >>> A==B
False
68 >>> print(A)
{0, 1, 2, 3, 4}
69 >>> print(B)
{5, 6, 7, 8, 9}
-------------------------------------------------------------
Dastur kodi:
70 >>> sets={0, 1, 2, 3}
71 >>> len(sets)
4
72 >>> sets={'march', 'feb', 'june'}
73 >>> len(sets)
3
74 >>> sets={0, 1, (1, 2), 3}
75 >>> len(sets)
4
-------------------------------------------------------------
Dastur kodi:
76 >>> sets={0, 1, 2, 3}
77 >>> 1 in sets
True
78 >>> 5 in sets
False
79 >>> sets={'july', 'aug', 'june', 'jan', 'may'}
80 >>> 'aug' in sets
True
81 >>> 'march' in sets
False
-------------------------------------------------------------
Do'stlaringiz bilan baham: |