forked from Jayhello/python_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_ope.py
More file actions
132 lines (106 loc) · 2.33 KB
/
Copy pathset_ope.py
File metadata and controls
132 lines (106 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding:utf-8 -*-
"""
python unique list -> set usage
"""
def define_set():
"""2 ways of defining set"""
set_1 = set([1, 2, 3])
print type(set_1)
print set_1
set_2 = {2, 3, 2}
print type(set_2)
# <type 'set'>
print set_2
# set([2, 3])
a = set((1, 2, 3, 4))
b = set([3, 4, 5, 6])
print a | b # Union
# {1, 2, 3, 4, 5, 6}
print a & b # Intersection
# {3, 4}
print a < b # Subset
# False
print a - b # Difference
# {1, 2}
print a ^ b # Symmetric Difference
# {1, 2, 5, 6}
def set_basic_usage():
s1 = set()
s1.add('abc')
s1.add('abc')
s1.add(123)
s1.add(777)
print (s1)
if 123 in s1:
print ' find it and remove it'
s1.remove(123)
print s1
def dict_val_set():
dic_val_set = {}
dic_val_set['abc'] = set([123])
dic_val_set['abc'].add(456)
dic_val_set['abc'].add(123)
print dic_val_set
# {'abc': set([456, 123])}
dic_val_set['ddd'] = set()
dic_val_set['ddd'].add(123)
for k in dic_val_set.keys():
if 123 in dic_val_set[k]:
print dic_val_set[k]
def set_remove():
# s_src = {1, 3, 5, 7}
s_src = {1}
# s2 = {1, 3, 2}
s2 = [1, 3, 2]
# raise error
# print s_src.remove(*s2)
try:
s_src.remove(*s2)
except Exception as e:
print e
# print s_src
print s_src - s2
# print s_src | s2
# set([1, 2, 3, 5, 7])
# print s_src & s2
# set([1, 3])
def set_lst():
s1 = {1, 2, 3}
lst_1 = []
# set to list
lst_1 += s1
print s1
print lst_1
def dict_key_to_set():
d = {'111': 1, 'aaa': 111}
s1 = set(d.keys())
print s1
# set(['111', 'aaa'])
s2 = {'111', 111}
print s1 & s2
def set_diff():
s1 = {1, 3}
s2 = {1, 2, 4}
print s1 - s2
print s1.difference(s2)
# set([3])
def set_hash():
lst = [1, 555, 372, 6, 6, 372, 222]
h_set = set(lst)
print h_set # unordered
# set([1, 555, 372, 222, 6])
if __name__ == '__main__':
set_hash()
# set_diff()
# dict_key_to_set()
# set_lst()
# set_remove()
# define_set()
# dict_val_set()
# set_basic_usage()
# print min(3, 4, -1)
# import time
# import random
# timestamp = int(time.time())
# print random.randint(0, 1000000) + timestamp
pass