Categories
Python Answers

How to redirect stdout to a file in Python?

Sometimes, we want to redirect stdout to a file in Python.

In this article, we’ll look at how to redirect stdout to a file in Python.

How to redirect stdout to a file in Python?

To redirect stdout to a file in Python, we can set sys.stdout to a file.

Then when we want to send stdout to the screen again, we call sys.stdout.close.

For instance, we write:

import sys
sys.stdout = open('file', 'w')
print('test')
sys.stdout.close()

We set sys.stdout to a file with path file.

We open file with write permission.

And then we call print to print some text, which will be written to file.

Finally, we call sys.stdout.close to close the file.

Conclusion

To redirect stdout to a file in Python, we can set sys.stdout to a file.

Categories
Python Answers

How to get a list all primes below N with Python?

Sometimes, we want to get a list all primes below N with Python.

In this article, we’ll look at how to get a list all primes below N with Python.

How to get a list all primes below N with Python?

To get a list all primes below N with Python, we can use the sympy library.

For instance, we write:

from sympy import sieve
primes = list(sieve.primerange(1, 10**2))
print(primes)

We import the sieve module from sympy.

Then we call the sieve.primerange method with the min and max of the range of which want to get the prime numbers for.

So we get all the primes between 1 and 100.

Therefore, primes is [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97].

Conclusion

To get a list all primes below N with Python, we can use the sympy library.

Categories
Python Answers

How to disable output buffering with Python?

Sometimes, we want to disable output buffering with Python.

In this article, we’ll look at how to disable output buffering with Python.

How to disable output buffering with Python?

To disable output buffering with Python, we can set the flush parameter to True.

For instance, we write:

print('Hello World!', flush=True)

then output buffering is disabled.

Conclusion

To disable output buffering with Python, we can set the flush parameter to True.

Categories
Python Answers

How to get the cartesian product of a series of lists with Python?

Sometimes, we want to get the cartesian product of a series of lists with Python.

In this article, we’ll look at how to get the cartesian product of a series of lists with Python.

How to get the cartesian product of a series of lists with Python?

To get the cartesian product of a series of lists with Python, we can use the itertools.product method.

For instance, we write:

import itertools

some_lists = [
   [1, 2, 3],
   ['a', 'b'],
   [4, 5]
]
for element in itertools.product(*some_lists):
    print(element)

We call itertools.product with the lists in some_lists spread into it as arguments.

Then we print all the tuples that are in the cartesian products list.

Therefore, we get:

(1, 'a', 4)
(1, 'a', 5)
(1, 'b', 4)
(1, 'b', 5)
(2, 'a', 4)
(2, 'a', 5)
(2, 'b', 4)
(2, 'b', 5)
(3, 'a', 4)
(3, 'a', 5)
(3, 'b', 4)
(3, 'b', 5)

printed.

Conclusion

To get the cartesian product of a series of lists with Python, we can use the itertools.product method.

Categories
JavaScript Answers

How to remove item from array using its name / value with JavaScript?

Sometimes, we want to remove item from array using its name / value with JavaScript.

In this article, we’ll look at how to remove item from array using its name / value with JavaScript.

How to remove item from array using its name / value with JavaScript?

To remove item from array using its name / value with JavaScript, we can use the JavaScript array’s findIndex and splice methods.

For instance, we write:

const countries = [{
    id: 'AF',
    name: 'Afghanistan'
  },
  {
    id: 'AL',
    name: 'Albania'
  },
  {
    id: 'DZ',
    name: 'Algeria'
  }
];

const index = countries.findIndex(c => c.id === 'AF')
countries.splice(index, 1)
console.log(countries)

We call countries.findIndex with a callback that finds the entry with id set to 'AF'.

Then we call splice with the index found and 1 to remove the entry with the given index.

Therefore, countries is now:

[
  {
    "id": "AL",
    "name": "Albania"
  },
  {
    "id": "DZ",
    "name": "Algeria"
  }
]

according to the console log.

Conclusion

To remove item from array using its name / value with JavaScript, we can use the JavaScript array’s findIndex and splice methods.