Found 33676 Articles for Programming

Compare and return True if two string arrays are equal in Numpy

AmitDiwan
Updated on 22-Feb-2022 06:36:55

592 Views

To compare and return True if two string arrays are equal, use the numpy.char.equal() method in Python Numpy. The arr1 and arr2 are the two input string arrays of the same shape. Unlike numpy.equal, this comparison is performed by first stripping whitespace characters from the end of the string. This behavior is provided for backward-compatibility with numarrayThe numpy.char module provides a set of vectorized string operations for arrays of type numpy.str_ or numpy.bytes_StepsAt first, import the required library −import numpy as npCreate two One-Dimensional arrays of string −arr1 = np.array(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad']) arr2 = np.array(['Cio', 'Tom', 'Cena', ... Read More

Return the numeric string left-filled with zeros in Numpy

AmitDiwan
Updated on 22-Feb-2022 06:34:35

191 Views

To return the numeric string left-filled with zeros, use the numpy.char.zfill() method in Python Numpy. Here, The 1st parameter is the inout arrayThe 2nd parameter is the "width" i.e. the width of string to left-fill elements in arrayThe numpy.char module provides a set of vectorized string operations for arrays of type numpy.str_ or numpy.bytes_.StepsAt first, import the required library −import numpy as npCreate a One-Dimensional array of string −arr = np.array(['Tom', 'John', 'Kate', 'Amy', 'Brad']) Displaying our array −print("Array...", arr)Get the datatype −print("Array datatype...", arr.dtype) Get the dimensions of the Array −print("Array Dimensions...", arr.ndim)Get the shape of the Array −print("Our ... Read More

How to check if a file exists in Golang?

Mukul Latiyan
Updated on 01-Nov-2023 02:12:56

42K+ Views

In order to check if a particular file exists inside a given directory in Golang, we can use the Stat() and the isNotExists() function that the os package of Go's standard library provides us with.The Stat() function is used to return the file info structure describing the file. Let's first use the Stat() function only and see whether it will be enough to detect the presence of a file in Go.Example 1Consider the code shown below.package main import(    "fmt"    "os" ) func main() {    if _, err := os.Stat("sample.txt"); err == nil {       fmt.Printf("File ... Read More

How to handle errors within WaitGroups in Golang?

Mukul Latiyan
Updated on 22-Feb-2022 06:58:49

4K+ Views

There are chances that we might get some panic while running multiple goroutines. To deal with such a scenario, we can use a combination of channel and waitgroups to handle the error successfully and not to exit the process.Let's suppose there's a function that when invoked returns a panic, which will automatically kill the execution of the program, as when panic gets called it internally calls os.Exit() function. We want to make sure that this panic doesn't close the program, and for that, we will create a channel that will store the error and then we can use that later ... Read More

How to wait for a goroutine to finish in Golang?

Mukul Latiyan
Updated on 22-Feb-2022 05:46:39

8K+ Views

We know that goroutines can be a bit tricky at first, and often we find cases where the main goroutines will exit without giving a chance to the inside goroutines to execute.In order to be able to run the goroutines until the finish, we can either make use of a channel that will act as a blocker or we can use waitGroups that Go's sync package provides us with.Let's first explore a case where we have a single goroutines that we want to finish and then do some other work.Example 1Consider the code shown below.package main import (    "fmt" ... Read More

How to concatenate two slices in Golang?

Mukul Latiyan
Updated on 22-Feb-2022 05:43:33

3K+ Views

Whenever we talk about appending elements to a slice, we know that we need to use the append() function that takes a slice as the first argument and the values that we want to append as the next argument.The syntax looks something like this.sl = append(sl, 1)Instead of appending a single number to the slice "sl", we can append multiple values in the same command as well.Consider the snippet shown below.sl = append(sl, 1, 2, 3, 4)The above code will work fine in Go.When it comes to appending a slice to another slice, we need to use the variadic function ... Read More

Sorting in Golang with sort Package

Mukul Latiyan
Updated on 22-Feb-2022 05:40:50

626 Views

The standard library of Golang provides a package that we can use if we want to sort arrays, slices, or even custom types. In this article, we will discover three main functions that we can use if we want to sort a slice in Golang. We will also see how we can create a custom sort function and custom comparator.Let's first check how we can sort a slice of integer, float64 and string values.ExampleConsider the code shown below.package main import (    "fmt"    "sort" ) func main() {    integerSlice := []int{3, 2, 14, 9, 11}    sort.Ints(integerSlice)   ... Read More

How to use iota in Golang?

Mukul Latiyan
Updated on 22-Feb-2022 05:32:09

5K+ Views

Iota in Go is used to represent constant increasing sequences. When repeated in a constant, its value gets incremented after each specification. In this article, we will explore the different ways in which we can use iota in Go.Let's first consider a very basic example, where we will declare multiple constants and use iota.Example 1Consider the code shown belowpackage main import (    "fmt" ) const (    first = iota    second = iota    third = iota ) func main() {    fmt.Println(first, second, third) }OutputIf we run the command go run main.go, then we will get the ... Read More

How to parse Date strings in Golang?

Mukul Latiyan
Updated on 22-Feb-2022 05:28:17

14K+ Views

When it comes to parsing Date strings in Go, we can use the Parse function that is provided by the time package. In Go, we don't use codes like most other languages to represent the component parts of a date/time string. Instead, Go uses the mnemonic device - standard time as a reference.For example, the reference time can either look like this −Mon Jan 2 14:10:05 MST 2020 (MST is GMT-0700)Or, it can look like this as well.01/02 03:04:10PM '20 -0700SyntaxThe syntax of the Parse() function is shown below.func Parse(layout, value string) (Time, error)The Parse function takes a layout and ... Read More

Anonymous goroutines in Golang

Mukul Latiyan
Updated on 22-Feb-2022 05:22:37

4K+ Views

In order to be able to understand the anonymous goroutines, we must be aware of the existence of anonymous functions and goroutines. We will first explore the anonymous functions that are the real reason behind the motivation of anonymous goroutines and then we will learn a little about what goroutines are, before finally checking a few examples of anonymous goroutines.Anonymous functionsIn Golang, anonymous functions are those functions that don't have any name. Simply put, anonymous functions don't use any variables as a name when they are declared.We know that we declare a function with a similar syntax as shown below.func ... Read More

Advertisements