Categories
Python Answers

How to modify a Python dict while iterating over it?

Sometimes, we want to modify a Python dict while iterating over it.

In this article, we’ll look at how to modify a Python dict while iterating over it.

How to modify a Python dict while iterating over it?

To modify a Python dict while iterating over it, we can use the items method to get the key and value.

For instance, we write

prefix = 'item_'
t = {'f1': 'ffw', 'f2': 'fca'}
t2 = dict() 
for k,v in t.items():
    t2[k] = prefix + v

to loop through the key value pairs in t with t.items() and the for loop.

In it, we set t2[k] to the prefix + v where v is the value in the t dict.

t2 is a new dict so we can modify it in the for loop.

Conclusion

To modify a Python dict while iterating over it, we can use the items method to get the key and value.

Categories
Python Answers

How to fix function not changing global variable with Python?

Sometimes, we want to fix function not changing global variable with Python.

In this article, we’ll look at how to fix function not changing global variable with Python.

How to fix function not changing global variable with Python?

To fix function not changing global variable with Python, we add the global keyword in front of the letter we want to change.

For instance, we write

def function():
    global done
    for loop:
        code
        if not comply:
            done = True

to define the done variable with global.

Then in the if block, we set the outside done variable to True since we have global in front of done.

Conclusion

To fix function not changing global variable with Python, we add the global keyword in front of the letter we want to change.

Categories
Python Answers

How to create a Caesar Cipher function in Python?

Sometimes, we want to create a Caesar Cipher function in Python.

In this article, we’ll look at how to create a Caesar Cipher function in Python.

How to create a Caesar Cipher function in Python?

To create a Caesar Cipher function in Python, we can use the string slicing feature.

For instance, we write

def caesar(plaintext, shift):
    alphabet = string.ascii_lowercase
    shifted_alphabet = alphabet[shift:] + alphabet[:shift]
    table = string.maketrans(alphabet, shifted_alphabet)
    return plaintext.translate(table)

to define the caesar function that takes the plaintext string and the number of positions to shift the characters in plaintext.

In it, we get the lowercase alpgabets in a string with

alphabet = string.ascii_lowercase

Then we shift the alphabet with

shifted_alphabet = alphabet[shift:] + alphabet[:shift]

Then we call maketrans to create a dict that maps the letters in alphabet to the letters in shifted_alphabet.

And then we call plaintext.translate with table to return a new string that maps the letters in the keys of table to the corresponding letter value.

Conclusion

To create a Caesar Cipher function in Python, we can use the string slicing feature.

Categories
Python Answers

How to write a connection string when password contains special characters with Python SQLalchemy?

Sometimes, we want to write a connection string when password contains special characters with Python SQLalchemy.

In this article, we’ll look at how to write a connection string when password contains special characters with Python SQLalchemy.

How to write a connection string when password contains special characters with Python SQLalchemy?

To write a connection string when password contains special characters with Python SQLalchemy, we should URL encode our string.

For instance, we write

from urllib import quote_plus as urlquote
from sqlalchemy.engine import create_engine

engine = create_engine('postgres://user:%s@host/database' % urlquote('badpass'))

to call create_engine with a connection string that we create by call urlquote on the password string to URL encode the password.

And then we interpolate that into the connection string.

Conclusion

To write a connection string when password contains special characters with Python SQLalchemy, we should URL encode our string.

Categories
Python Answers

How to store different datatypes in one NumPy array with Python?

Sometimes, we want to store different datatypes in one NumPy array with Python.

In this article, we’ll look at how to store different datatypes in one NumPy array with Python.

How to store different datatypes in one NumPy array with Python?

To store different datatypes in one NumPy array with Python, we can store the values in a record array.

For instance, we write

a = numpy.array(['a', 'b', 'c', 'd', 'e'])
b = numpy.arange(5)
records = numpy.rec.fromarrays((a, b), names=('keys', 'data'))

to call numpr.rec.fromarrays with (a, b) and the names argument set to a tuple with the column names.

Then we get a NumPy record array that has a list of items with the tuples for each item at the given position in each tuple.

We can then get the values with the keys like

keys = records['keys']

to get the values in a.

Conclusion

To store different datatypes in one NumPy array with Python, we can store the values in a record array.