Get Length of a List of Lists in Python

Malhar Lathkar
Updated on 19-Dec-2019 09:23:22

358 Views

You can employ a nested loop to count number of elements in each sublist of a list>>> a=[[1, 2, 3], [4, 5, 6]] >>> c=0 >>> for x in a:       for y in x:       c=c+1 >>> c 6

Iterate Through a Tuple in Python

Pythonic
Updated on 19-Dec-2019 09:06:37

5K+ Views

There are different ways to iterate through a tuple object. The for statement in Python has a variant which traverses a tuple till it is exhausted. It is equivalent to foreach statement in Java. Its syntax is −for var in tuple: stmt1 stmt2ExampleFollowing script will print all items in the listT = (10, 20, 30, 40, 50) for var in T: print (T.index(var), var)OutputThe output generated is −0 10 1 20 2 30 3 40 4 50Another approach is to iterate over range upto length of tuple, and use it as index of item in tupleExamplefor var in range(len(T)):   ... Read More

Match Tab and Newline Using Python Regular Expression

Rajendra Dharmkar
Updated on 19-Dec-2019 09:03:45

2K+ Views

The following code matches tab and newline but not space from given string using regex.Exampleimport re print re.findall(r"[\t]","""I find     Tutorialspoint useful""")OutputThis gives the output['']

Remove Tabs and Newlines Using Python Regular Expression

Rajendra Dharmkar
Updated on 19-Dec-2019 09:02:07

1K+ Views

The following code removes tabs and newlines from given stringExampleimport re print re.sub(r"\s+", " ", """I find Tutorialspoint helpful""")OutputThis gives outputI find Tutorialspoint helpful

Convert Java 8 Stream to an Array

karthikeya Boyini
Updated on 19-Dec-2019 08:54:20

485 Views

To convert a stream to an Array in Java -Collect the stream to a list using the Collect interface and the Collectors class.Now convert the list to an array using the toArray() method.ExampleLive Demoimport java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class J8StreamToArray {    public static void main(String args[]) {       String[] myArray = { "JavaFX", "OpenCV", "WebGL", "HBase" };       Stream stream = Stream.of(myArray);       List list = stream.collect(Collectors.toList());       String[] str = list.toArray(new String[0]);       System.out.println(Arrays.toString(str));    } }Output[JavaFX, OpenCV, WebGL, HBase]

Write Contents of a File to Byte Array in Java

Sharon Christine
Updated on 19-Dec-2019 08:53:10

975 Views

The FileInputStream class contains a method read(), this method accepts a byte array as a parameter and it reads the data of the file input stream to given byte array.Exampleimport java.io.File; import java.io.FileInputStream; public class FileToByteArray {    public static void main(String args[]) throws Exception {       File file = new File("HelloWorld");       FileInputStream fis = new FileInputStream(file);       byte[] bytesArray = new byte[(int)file.length()];       fis.read(bytesArray);       String s = new String(bytesArray);       System.out.println(s);    } }Output//Class declaration public class SampleProgram {    /* This is my ... Read More

Add Elements to the Midpoint of an Array in Java

Lakshmi Srinivas
Updated on 19-Dec-2019 08:51:41

268 Views

Apache commons provides a library named org.apache.commons.lang3 and, following is the maven dependency to add library to your project.           org.apache.commons       commons-lang3       3.0     This package provides a class named ArrayUtils. You can add an element at a particular position in an array using the add() method of this class.Exampleimport java.util.Arrays; import org.apache.commons.lang3.ArrayUtils; public class AddingElementToMidPoint {    public static void main(String args[]) {       int[] myArray = {23, 93, 30, 56, 92, 39};       int eleToAdd = 40;       int position= myArray.length/2;       int [] result = ArrayUtils.add(myArray, position, eleToAdd);       System.out.println(Arrays.toString(result));    } }Output[23, 93, 30, 40, 56, 92, 39]

Compare Two Strings Using Regex in Python

Rajendra Dharmkar
Updated on 19-Dec-2019 08:49:48

2K+ Views

We can compare given strings using the following codeExampleimport re s1 = 'Pink Forest' s2 = 'Pink Forrest' if bool(re.search(s1,s2))==True:    print 'Strings match' else:    print 'Strings do not match'OutputThis gives the outputStrings do not match

Find If Given Number is Present in Infinite Sequence in C++

Arnab Chakraborty
Updated on 19-Dec-2019 08:15:27

279 Views

Suppose we have three integers a, b and c. Suppose in an infinite sequence, a is the first term, and c is a common difference. We have to check whether b is present in the sequence or not. Suppose the values are like a = 1, b = 7 and c = 3, Then the sequence will be 1, 4, 7, 10, …, so 7 is present in the sequence, so the output will be ‘yes’.To solve this problem, we have to follow these two steps −When c = 0, and a = b, then print yes, and if a ... Read More

Interchange Diagonals of Matrix in C

Ayush Gupta
Updated on 19-Dec-2019 08:12:16

266 Views

In this tutorial, we will be discussing a program to interchange the diagonals of a given matrix.For this, we will be given with a square matrix of the order n*n. Our task is to interchange the elements in the two diagonals of the matrix and then return the new matrix.Example Live Demo#include using namespace std; #define N 3 //interchanging the two diagonals void int_diag(int array[][N]){    for (int i = 0; i < N; ++i)       if (i != N / 2)    swap(array[i][i], array[i][N - i - 1]);    for (int i = 0; i < N; ++i){ ... Read More

Advertisements