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
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
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['']
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
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]
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
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]
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP