Tutorial by Examples: co

from collections import Counter c = Counter(["a", "b", "c", "d", "a", "b", "a", "c", "d"]) c # Out: Counter({'a': 3, 'b': 2, 'c': 2, 'd': 2}) c["a"] # Out: 3 c[7] # not in the list (7 oc...
Counting the keys of a Mapping isn't possible with collections.Counter but we can count the values: from collections import Counter adict = {'a': 5, 'b': 3, 'c': 5, 'd': 2, 'e':2, 'q': 5} Counter(adict.values()) # Out: Counter({2: 2, 3: 1, 5: 3}) The most common elements are avaiable by the m...
alist = [1, 2, 3, 4, 1, 2, 1, 3, 4] alist.count(1) # Out: 3 atuple = ('bear', 'weasel', 'bear', 'frog') atuple.count('bear') # Out: 2 atuple.count('fox') # Out: 0
astring = 'thisisashorttext' astring.count('t') # Out: 4 This works even for substrings longer than one character: astring.count('th') # Out: 1 astring.count('is') # Out: 2 astring.count('text') # Out: 1 which would not be possible with collections.Counter which only counts single char...
var favoriteColors: Set = ["Red", "Blue", "Green"] //favoriteColors = {"Blue", "Green", "Red"} You can use the contains(_:) method to check whether a set contains a value. It will return true if the set contains that value. if favorite...
Convert to String var date1 = new Date(); date1.toString(); Returns: "Fri Apr 15 2016 07:48:48 GMT-0400 (Eastern Daylight Time)" Convert to Time String var date1 = new Date(); date1.toTimeString(); Returns: "07:48:48 GMT-0400 (Eastern Daylight Time)" Conve...
# Set the repository for the scope "myscope" npm config set @myscope:registry http://registry.corporation.com # Login at a repository and associate it with the scope "myscope" npm adduser --registry=http://registry.corporation.com --scope=@myscope # Install a package &quo...
You can concatenate std::strings using the overloaded + and += operators. Using the + operator: std::string hello = "Hello"; std::string world = "world"; std::string helloworld = hello + world; // "Helloworld" Using the += operator: std::string hello = "Hel...
const fs = require('fs'); // Read the contents of the directory /usr/local/bin asynchronously. // The callback will be invoked once the operation has either completed // or failed. fs.readdir('/usr/local/bin', (err, files) => { // On error, show it and return if(err) return console.er...
Use Case CASE can be used in conjunction with SUM to return a count of only those items matching a pre-defined condition. (This is similar to COUNTIF in Excel.) The trick is to return binary results indicating matches, so the "1"s returned for matching entries can be summed for a count o...
in myapp/context_processors.py: from django.conf import settings def debug(request): return {'DEBUG': settings.DEBUG} in settings.py: TEMPLATES = [ { ... 'OPTIONS': { 'context_processors': [ ... 'myapp.context_processor...
Counter is a dict sub class that allows you to easily count objects. It has utility methods for working with the frequencies of the objects that you are counting. import collections counts = collections.Counter([1,2,3]) the above code creates an object, counts, which has the frequencies of all ...
collections.defaultdict(default_factory) returns a subclass of dict that has a default value for missing keys. The argument should be a function that returns the default value when called with no arguments. If there is nothing passed, it defaults to None. >>> state_capitals = collections.d...
A cookie is set using the setcookie() function. Since cookies are part of the HTTP header, you must set any cookies before sending any output to the browser. Example: setcookie("user", "Tom", time() + 86400, "/"); // check syntax for function params Description: ...
Retrieve and Output a Cookie Named user The value of a cookie can be retrieved using the global variable $_COOKIE. example if we have a cookie named user we can retrieve it like this echo $_COOKIE['user'];
The value of a cookie can be modified by resetting the cookie setcookie("user", "John", time() + 86400, "/"); // assuming there is a "user" cookie already Cookies are part of the HTTP header, so setcookie() must be called before any output is sent to the...
Use the isset() function upon the superglobal $_COOKIE variable to check if a cookie is set. Example: // PHP <7.0 if (isset($_COOKIE['user'])) { // true, cookie is set echo 'User is ' . $_COOKIE['user']; else { // false, cookie is not set echo 'User is not logged in'; } ...
//Swift button.setTitleColor(color, forControlState: controlState) //Objective-C [button setTitleColor:(nullable UIColor *) forState:(UIControlState)]; To set the title color to blue //Swift button.setTitleColor(.blue, for: .normal) //Objective-C [button setTitleColor:[UIColor blueColo...
Swift //Align contents to the left of the frame button.contentHorizontalAlignment = .left //Align contents to the right of the frame button.contentHorizontalAlignment = .right //Align contents to the center of the frame button.contentHorizontalAlignment = .center //Make contents fill th...
You can use the nil coalescing operator to unwrap a value if it is non-nil, otherwise provide a different value: func fallbackIfNil(str: String?) -> String { return str ?? "Fallback String" } print(fallbackIfNil("Hi")) // Prints "Hi" print(fallbackIfNil(nil)...

Page 10 of 248