Lakshmi Srinivas

Lakshmi Srinivas

233 Articles Published

Articles by Lakshmi Srinivas

Page 18 of 24

How to Humanize numbers with Python?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 05-Mar-2020 388 Views

If you want something that converts integers to words like 99 to ninety-nine, you have to use an external package or build one yourself. The pynum2word module is pretty good at this task. You can install it using −$ pip install pynum2wordThen use it in the following way −>>> import num2word >>> num2word.to_card(16) 'sixteen' >>> num2word.to_card(23) 'twenty-three' >>> num2word.to_card(1223)'one thousand, two hundred and twenty-three'If you want to get results like 1.23 million for 1, 230, 000, you can use the humanize library to do so. You can install it using −$ pip install humanizeThen use it in the following way ...

Read More

How to create a lambda inside a Python loop?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 05-Mar-2020 1K+ Views

You can create a list of lambdas in a python loop using the following syntax −Syntaxdef square(x): return lambda : x*x listOfLambdas = [square(i) for i in [1,2,3,4,5]] for f in listOfLambdas: print f()OutputThis will give the output −1 4 9 16 25You can also achieve this using a functional programming construct called currying. examplelistOfLambdas = [lambda i=i: i*i for i in range(1, 6)] for f in listOfLambdas: print f()OutputThis will give the output −1 4 9 16 25

Read More

How to overload python ternary operator?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 05-Mar-2020 437 Views

The ternary operator cannot be overloaded. Though you can wrap it up in a lambda/function and use it. For exampleresult = lambda x: 1 if x < 3 else 10 print(result(2)) print(result(1000))OutputThis will give the output −1 10

Read More

How to view a list of all Python operators via the interpreter?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 05-Mar-2020 360 Views

The help method in the interpreter is very useful for such operations. It provides a rich set of special inputs that you can give to it to get information about the different aspects of the language. Forgetting operator lists, here are some of the commands you can use:All operators>>> help('SPECIALMETHODS')Basic operators>>> help('BASICMETHODS')Numeric operators>>> help('NUMBERMETHODS')Other than operators you can also get attribute methods, callable methods, etc using −>>> help('MAPPINGMETHODS') >>> help('ATTRIBUTEMETHODS') >>> help('SEQUENCEMETHODS1') >>> help('SEQUENCEMETHODS2') >>> help('CALLABLEMETHODS')

Read More

How to find the average of non-zero values in a Python dictionary?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 05-Mar-2020 3K+ Views

You can do this by iterating over the dictionary and filtering out zero values first. Then take the sum of the filtered values. Finally, divide by the number of these filtered values. examplemy_dict = {"foo": 100, "bar": 0, "baz": 200} filtered_vals = [v for _, v in my_dict.items() if v != 0] average = sum(filtered_vals) / len(filtered_vals) print(average)OutputThis will give the output −150.0You can also use reduce but for a simple task such as this, it is an overkill. And it is also much less readable than using a list comprehension.

Read More

How do we compare two tuples in Python?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 05-Mar-2020 6K+ Views

Tuples are compared position by position: the first item of the first tuple is compared to the first item of the second tuple; if they are not equal, this is the result of the comparison, else the second item is considered, then the third and so on. example>>> a = (1, 2, 3) >>> b = (1, 2, 5) >>> a < b TrueThere is another type of comparison that takes into account similar and different elements. This can be performed using sets. Sets will take the tuples and take only unique values. Then you can perform a & operation that ...

Read More

How do we grep a particular keyword from Python tuple?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 05-Mar-2020 255 Views

If you have a tuple of strings and you want to search for a particular string, You can use the in operator. exampletpl = ("Hello", "world", "Foo", "bar") print("world" in tpl)OutputThis will give the output −TrueExampleIf you want to check if there is a substring present. You can loop over the tuple and find it using:tpl = ("Hello", "world", "Foo", "bar") for i in tpl:    if "orld" in i:       print("Found orld in " + i )OutputThis will give the output −Found orld in world

Read More

Use of SPATIAL function in SAP HANA

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 17-Feb-2020 310 Views

In SAP HANA, geospatial data types are not defined as such and you need to use scalar values like a.ST_X().You can create a table like this −CREATE COLUMN TABLE MYSCHEMA.SpatialShapes_GEOMETRIES (    ShapeID integer,    SHAPE1 ST_Point,    SHAPE2 ST_GEOMETRY );SAP provides a complete guide to handle Geospatial data in applications. You can refer this link for more details:https://help.sap.com/doc/PRODUCTION/9db42d044f8e415180d4a4475873b50a/2.0.00/en-US/SAP_HANA_Spatial_Reference_en.pdf

Read More

How to Find out the source code of a transaction in SAP?

SAP
Lakshmi Srinivas
Lakshmi Srinivas
Updated on 17-Feb-2020 4K+ Views

First, go to System → Status to find the program name. Now use the transaction SE38 or SE80 to view the source code.Alternatively, you can activate the debugging mode before running your transaction by keying in /h.

Read More

What are different types of constants in C++?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 11-Feb-2020 910 Views

There are no types of constants in C++. It's just that you can declare any data type in C++ to be a constant. If a variable is declared as constant using the const keyword, you cannot reassign its value. Example#include using namespace std; int main() { const int i = 5; // Now all of these operations are illegal and // will cause an error: i = 10; i *= 2; i++; i--; //... return 0; }

Read More
Showing 171–180 of 233 articles
« Prev 1 16 17 18 19 20 24 Next »
Advertisements