How to Remove Punctuation from a List in Python

If you have a Python list, and want to remove all punctuation, then you can do the following: First get all punctuation by using string.punctuation 1 2 import string print(string.punctuation) Output: 1 !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ Option 1 – Using for 1 2 3 4 5 6 7 8 9 10 11 12 import string words = ["hell'o", "Hi,", "bye bye", "good bye", ""] new_words = [] for word in words: if word == "": words.

How to Normalize a List of Numbers in Python

If you need to normalize a list of numbers in Python, then you can do the following: Option 1 – Using Native Python 1 2 3 4 5 6 7 list = [6,1,0,2,7,3,8,1,5] print('Original List:',list) xmin = min(list) xmax=max(list) for i, x in enumerate(list): list[i] = (x-xmin) / (xmax-xmin) print('Normalized List:',list) Option 2 – Using MinMaxScaler from sklearn 1 2 3 4 5 6 7 import numpy as np from sklearn import preprocessing list = np.

How to Multiply a List by a Scalar in Python

If you need to multiply a list by a scalar in Python, then you can do one of the following: Option 1 – Using List Comprehensions 1 2 3 4 li = [1,2,3,4] multiple = 2.5 li = [x*multiple for x in li] print(li) Output: [2.5, 5.0, 7.5, 10.0] Option 2 – Using map() 1 2 3 4 5 6 7 li = [1,2,3,4] multiple = 2.5 def multiply(le): return le*multiple li = list(map(multiply,li)) print(li) Output: [2.

How to Find the Index of the Minimum Element in a List in Python

If you need to find the index of the minimum element in a list, you can do one of the following: Option 1 – Using min() and index() 1 2 3 lst = [8,6,9,-1,2,0] m = min(lst) print(lst.index(m)) Output: 3 Option 2 – Using min() and for 1 2 3 4 5 6 lst = [8,6,9,-1,2,0] m = min(lst) for i in range(len(lst)): if(lst[i]==m): print(i) break Output: 3 Option 3 – Using min() and enumerate() 1 2 3 lst = [8,6,9,-1,2,0] a,i = min((a,i) for (i,a) in enumerate(lst)) print(i) Output: 3

How to Convert a Set to a String in Python

If you need to convert a set to a string in Python, then you can do one of the following: Option 1 – Using map() and join() 1 str_new = ', '.join(list(map(str, se))) You can confirm this worked as follows: 1 2 3 4 5 print(str_new) print(type(str_new)) # '1, 2, 3' # <class 'str'> Option 2 – Using repr() 1 r_str = repr(se) You can confirm this worked as follows:

How to Decrement a Loop in Python

If you need to decrement a loop in Python, then you can do the following: How to Decrement a loop in Python using -1 The optional third argument you can pass to the range function is the order. The default order is to count up / increment, while the -1 value, is to count down / decrement. 1 2 for i in range(3, 0, -1): print(i) Output: 3 2 1

How to Create Zip Archive of Directory in Python

If you need to create a zip of a directory using Python, then you can do the following: Create a Zip using shutil in Python 1 2 3 4 5 6 7 8 import os import shutil filename = "compressed_archive" format = "zip" directory = os.getcwd() shutil.make_archive(filename, format, directory)

How to Remove the Last Character of a String in PHP

If you need to remove the last character of a string in PHP, then you can do the following: Option 1 – Using rtrim() Syntax: rtrim($string, $character) 1 2 3 4 5 6 7 $mystring = "This is a PHP program!"; echo("Before Removal: $mystring\n"); # Before Removal: This is a PHP program! $newstring = rtrim($mystring, ". "); echo("After Removal: $newstring"); # After Removal: This is a PHP program Option 2 – Using substr() Syntax: substr($string, $start, $length)

How to Create Function with Multiple Returns in PHP

If you need to create a PHP function that returns multiple values, then you can do one of the following. Option 1 – Returning an array 1 2 3 4 5 6 function returnArray() { return array("one", "two"); } // call the function and print the response var_dump(returnArray()); Option 2 – Returning a conditional 1 2 3 4 5 6 7 function returnConditional($x = true) { if ($x) return "one"; else return "two"; } // call the function with a value and print the response var_dump(returnConditional(false)); Option 3 – Using generator to yield values 1 2 3 4 5 6 7 8 9 function multipleValues() { yield "one"; yield "two"; } $res = multipleValues(); foreach($res as $r) { echo $r; // first val=="one", second val=="two" }

How to Download a File in NodeJS without any Third Party Libraries

If you need to download a file in NodeJS without using any third party libraries, then you can do the following. The NodeJS ecosystem comes with a fs module, this is to denote the FileSystem built in library. First declare your imports: 1 2 const http = require('https'); const fs = require('fs'); Now you can use the https module to download a file, and write it to a stream using fs.

How to Execute a Shell Script in NodeJS

If you need to execute a shell script in NodeJS, then you can use the exec keyword. Syntax: exec(command [, options] [, callback] 1 2 const shell = require('shelljs') shell.exec("npm --version")

How to Print Multiple Arguments in Python

If you need to print out multiple arguments using Python, then you can do one of the following: Option 1 – Using print 1 print("Something", "Else", "Please") Output: Something Else Please Option 2 – Using String Formatting 1 print("Something {} {}".format("Else", "Please")) Output: Something Else Please Or with explicit ordering: 1 print("Something {1} {0}".format("Else", "Please")) Output: Something Please Else Option 3 – Using F-String Formatting 1 2 3 4 5 one = "Something" two = "Else" three = "Please" print(f"{one} {two} {three}") Output: Something Else Please

How to Delete all Resources Except One in Terraform

If you need to delete all resources created by Terraform, except for a single, specific one, then you can do the following: Step 1 – Get the current state list 1 terraform state list Step 2 – Remove the exception resource Remove the specific resource that you don’t want Terraform to track anymore. 1 terraform state rm <resource_to_be_removed> Step 3 – Destroy the resources 1 terraform destroy

How to Copy Files between Two Nodes using Ansible

If you need to copy files between two (2) nodes, using Ansible, then you can do the following: This solution uses the synchronize module, specifically using the delegate_to:source-server keywords. 1 2 3 4 5 - hosts: serverB tasks: - name: Copy Remote-To-Remote (from serverA to serverB) synchronize: src=/copy/from_serverA dest=/copy/to_serverB delegate_to: serverA

How to Copy Multiple Files with Ansible

If you need to copy multiple files using Ansible, then you can do the following: How to Copy Multiple Files with Ansible Look into using the with_fileglob loop as follows: 1 2 3 4 5 6 7 - copy: src: "{{ item }}" dest: /etc/fooapp/ owner: root mode: 600 with_fileglob: - /playbooks/files/fooapp/* If you would like to do it as a task, then this could help: 1 2 3 4 5 6 - name: Your copy task copy: src={{ item.

How to Write Multiline Shell Scripts in Ansible

If you need to write a shell script in Ansible, you probably have something like this: 1 2 3 - name: iterate user groups shell: groupmod -o -g {{ item['guid'] }} {{ item['username'] }} with_items: "{{ users }}" But how do you write multiline shell scripts with this format? How to write Multiline shell scripts 1 2 3 4 5 6 - name: iterate user groups shell: | groupmod -o -g {{ item['guid'] }} {{ item['username'] }} do_some_stuff_here and_some_other_stuff with_items: "{{ users }}" Just note that Ansible can do some strange things with manipulations of arguments, so you may want to follow something like this:

How to Pass Variables to Ansible Playbook CLI

If you need to pass a variable to Ansible playbook, using the command line, then you can do the following: Option 1 – Specifying command line arguments 1 ansible-playbook release.yml --extra-vars "version=1.23.45 other_variable=foo" N.B. --extra-vars specified variables will override any variables with the same name defined inside the playbook. You can also read up on Passing Variables On The Command Line (Wayback Machine link to maintain versioning) Option 2 – Specify a YML file You can also specify a .

How to Create a Directory using Ansible

If you need to create a directory using Ansible, then you can do the following: Create a Directory in Ansible You will need the file module, then to create a directory you simply specify the option state=directory: 1 2 3 4 - name: Creates a directory file: path: /src/www state: directory Note that with state=directory, all the immediate subdirectories will be created if they don’t already exist. Extending the file module 1 2 3 4 5 6 7 - name: Creates a directory file: path: /src/www state: directory owner: www-data group: www-data mode: 0775 Create the Directories Recursively 1 2 3 4 5 6 7 8 - name: Creates directory file: path: /src/www state: directory owner: www-data group: www-data mode: 0775 recurse: yes This is similar to the recursive argument used with mkdir -p

How to Disable Screensaver on Mac using the CLI

If you want to disable the Screensaver on your Mac, by using the Command Line Interface (CLI), then you can do the following: Step 1 – See what is currently set 1 defaults read com.apple.screensaver Step 2 – Set the idleTime 1 defaults -currentHost write com.apple.screensaver idleTime 0 Step 3 – (Optional) – Undo the change 1 defaults -currentHost delete com.apple.screensaver idleTime