૵ (set) ¤×Í ªØ´¢Í§¢éÍÁÙÅã¹áºº·ÕèäÁè͹ØÒµ ãËé«éӡѹ áÅÐäÁèàÃÕ§ÅӴѺ ¨Ö§ÁÕ¡ÒÃãªé૵ÁÒªèÇÂ㹡ÒõÃǨÊͺ¡ÒëéӢͧ¢éÍÁÙÅ
µÑÇÍÂèÒ§¢Í§à«µ ¤×Í Çѹã¹ÊÑ»´ÒËì à´×͹ã¹Ë¹Öè§»Õ »Õ¹Ñ¡Éѵà à¾È µÓá˹觷ҧÇÔªÒ¡Òà ÃÒª×èÍÍÓàÀÍ㹨ѧËÇÑ´ ÍÒªÕ¾ àª×éÍªÒµÔ ÊѪҵÔ
// Set in Java Language // you can test this code in http://www.ideone.png Set<Integer> numbers = new TreeSet<Integer>(); numbers.add(2); numbers.add(5); System.out.println(numbers); // "[2, 5]" System.out.println(numbers.contains(7)); // "false" System.out.println(numbers.contains(5)); // "true" System.out.println(numbers.add(5)); // "false" System.out.println(numbers.size()); // "2" int sum = 0; for (int n : numbers) { sum += n; } System.out.println("Sum = " + sum); // "Sum = 7" numbers.addAll(Arrays.asList(1,2,3,4,5)); System.out.println(numbers); // "[1, 2, 3, 4, 5]" numbers.removeAll(Arrays.asList(4,5,6,7)); System.out.println(numbers); // "[1, 2, 3]" numbers.retainAll(Arrays.asList(2,3,4,5)); System.out.println(numbers); // "[2, 3]"
<!-- Set in Java Language you can test this code in http://www.w3schools.com/js/tryit.asp?filename=tryjs_myfirst code from https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Set --> <html><head> <script> function myset() { var out = ""; var mySet mySet = new Set(); mySet.add(1); mySet.add(5); mySet.add("some text"); mySet.add(6); out += mySet.has(1); // true out += mySet.has(3); // false, 3 has not been added to the set out += mySet.has(5); // true out += mySet.has(Math.sqrt(25)); // true out += mySet.has("Some Text".toLowerCase()); // true out += mySet.has(6); // true out += mySet.size; // 4 mySet.delete(5); // removes 5 from the set out += mySet.has(5); // false, 5 has been removed out += mySet.size; // 3, we just removed one value out += "<br/>"; for (let item of mySet) { out += (item); } return out; } </script> </head><body> <button type="button" onclick="document.getElementById('demo').innerHTML = myset()">Click me</button> <p id="demo"></p> </body> </html>
# set of integers my_set = {1, 2, 3} print(my_set) # set of mixed datatypes my_set = {1.0, "Hello", (1, 2, 3)} print(my_set) # initialize a with {} a = {} # check data type of a # Output: <class 'dict'> print(type(a)) # initialize a with set() a = set() # check data type of a # Output: <class 'set'> print(type(a)) # code from programiz.com