Iterate Over Characters of a String in Python

Pavitra
Updated on 01-Jul-2020 07:10:14

2K+ Views

In this article, we will learn about iterating/ traversing over characters of a string in Python 3.x. Or earlier.The string is a collection of characters which may contain spaces, alphabets or integers. They can be accessed using indexes or via references . Some commonly implemented methods are shown below.Method 1 − The direct itertor without indexingExamplestring_inp = "tutorialspoint" # Iterate over the string for value in string_inp:    print(value, end='')Method 2 − The most common way using index based accessExamplestring_inp = "tutorialspoint" # Iterate over the string for value in range(0, len(string_inp)):    print(string_inp[value], end='')Method 3 − The ... Read More

Querying Nested Documents in MongoDB

AmitDiwan
Updated on 01-Jul-2020 06:58:37

1K+ Views

To query on an array of objects for nested documents, use find(). Let us create a collection with documents −> db.demo763.insertOne( ...    { ...       _id:1, ...       CountryName:"US", ...       "studentInformation": [ ...          { ...             StudentName:"Chris", ...          }, ...          { ...             StudentName:"David", ...             StudentAge:22 ...          } ...       ] ...    } ... ); { "acknowledged" : true, "insertedId" : 1 }Display all documents from a collection with the help of find() method −> db.demo763.find();This will produce the following output −{ "_id" : 1, "CountryName" : "US", "studentInformation" : [ { "StudentName" : "Chris" }, { "StudentName" : "David", "StudentAge" : 22 } ] }Following is how to query an array of objects to fetch specific nested documents −> db.demo763.find({}, ... { ...    studentInformation: { ...       $elemMatch: { ...          StudentAge: { ...             $exists: true ...          } ...       } ...    } ... })This will produce the following output −{ "_id" : 1, "studentInformation" : [ { "StudentName" : "David", "StudentAge" : 22 } ] }

MongoDB Aggregation and Projection

AmitDiwan
Updated on 01-Jul-2020 06:57:21

1K+ Views

For this, use $project along with aggregate(). The $project in aggregation passes along the documents with the requested fields to the next stage in the pipeline.Let us create a collection with documents −> db.demo762.insertOne({ ...    "_id" : { ...       "userId":101, ...       "userName":"Chris" ...    }, ..   . "countryName" : "US", ... ...    "details" : [ ...       { ...          "Name" : "Robert", ...          "DueDate" : "2020-04-10" ... ...       }, ... ...       { ...     ... Read More

Check for Duplicates in an Array in MongoDB

AmitDiwan
Updated on 01-Jul-2020 06:46:00

1K+ Views

To check for duplicates in an array, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo756.insertOne({"SubjectName":["MySQL", "MongoDB", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb01e0d5637cd592b2a4add") } > db.demo756.insertOne({"SubjectName":["MongoDB", "MySQL", "MongoDB", "C", "C+", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb01e2b5637cd592b2a4ade") }Display all documents from a collection with the help of find() method −> db.demo756.find();This will produce the following output −{ "_id" : ObjectId("5eb01e0d5637cd592b2a4add"), "SubjectName" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5eb01e2b5637cd592b2a4ade"), "SubjectName" : [ "MongoDB", "MySQL", "MongoDB", "C", "C+", "MySQL" ] }Following is the query to check for ... Read More

Sort MongoDB Field Containing Both Integer and Decimal Values

AmitDiwan
Updated on 01-Jul-2020 06:42:25

421 Views

To sort, use sort() in MongoDB. Let us create a collection with documents −> db.demo755.insertOne({"Value":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e72a930c785c834e572") } > db.demo755.insertOne({"Value":10.5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e75a930c785c834e573") } > db.demo755.insertOne({"Value":9.5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e79a930c785c834e574") } > db.demo755.insertOne({"Value":12.5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e7fa930c785c834e575") } > db.demo755.insertOne({"Value":11.5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e87a930c785c834e576") } > db.demo755.insertOne({"Value":6}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9e97a930c785c834e577") }Display all documents from a collection with the help of find() method −> db.demo755.find();This will produce ... Read More

Convert Birth Date Records to Age with MongoDB

AmitDiwan
Updated on 01-Jul-2020 06:41:02

2K+ Views

Let us create a collection with documents −> db.demo754.insertOne({"DateOfBirth":new Date("2000-05-03")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9b2da930c785c834e56f") } > db.demo754.insertOne({"DateOfBirth":new Date("2010-01-21")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9b34a930c785c834e570") } > db.demo754.insertOne({"DateOfBirth":new Date("2018-05-03")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eae9b3da930c785c834e571") }Display all documents from a collection with the help of find() method −> db.demo754.find();This will produce the following output −{ "_id" : ObjectId("5eae9b2da930c785c834e56f"), "DateOfBirth" : ISODate("2000-05-03T00:00:00Z") } { "_id" : ObjectId("5eae9b34a930c785c834e570"), "DateOfBirth" : ISODate("2010-01-21T00:00:00Z") } { "_id" : ObjectId("5eae9b3da930c785c834e571"), "DateOfBirth" : ISODate("2018-05-03T00:00:00Z") }Following is the query to convert date of birth to age −> db.demo754.aggregate( ... Read More

Importance of the parseBoolean Method in Java

raja
Updated on 01-Jul-2020 06:13:23

219 Views

The parseBoolean() method is an important method of a Boolean class. The parseBoolean() is a static method and can parse the String method argument into a Boolean object. The parseBoolean() method of Boolean class returns the boolean represented by the string argument.Syntaxpublic static boolean parseBoolean(String s)Exampleimport java.util.Scanner; public class ParseBooleanMethodTest {    public static void main(String[] args) {       System.out.print("Are you ready to play cricket(true/false)?");       Scanner scanner = new Scanner(System.in);       String str = scanner.nextLine();       scanner.close();       // Convert the user input into boolean       boolean answer ... Read More

Use SearchView in Android

Azhar
Updated on 01-Jul-2020 06:10:17

2K+ Views

This example demonstrates how do I use SearchView in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new projectStep 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.javaimport android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SearchView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity {    SearchView searchView;    ListView listView;    ArrayList list;    ArrayAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState); ... Read More

Restart an Activity in Android

Azhar
Updated on 01-Jul-2020 06:08:07

3K+ Views

This example demonstrates how do I restart an Activity in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.javaimport android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Random; public class MainActivity extends AppCompatActivity {    TextView textView;    Button button;    Random random = new Random();    @Override    protected void onCreate(Bundle savedInstanceState) {       ... Read More

Calculate Bitonicity of an Array

sudhir sharma
Updated on 01-Jul-2020 06:07:18

139 Views

The bitonicity of an array is defined using the following syntax −To find bitonicity of an array based on its elements is −Bitonicity = 0 , initially arr[0] i from 0 to n Bitonicity = Bitonicity+1 ; if arr[i] > arr[i-1] Bitonicity = Bitonicity-1 ; if arr[i] < arr[i-1] Bitonicity = Bitonicity ; if arr[i] = arr[i-1]ExampleThe code for finding the bitonicity of an array we have used a variable called bitonicity, that changes its based on the comparison of the current and previous elements of the array. The above logic updates the bitonicity of the array and final bitonicity ... Read More

Advertisements