GreyMamba

Thinking Allowed … (under construction)

Thinking Allowed … (under construction)

Nothing is interesting if you're not interested
Helen Clark MacInnes
Python Stuff

I tend to spend too much time mucking around with Python code - as you can see from the bits of code scattered around this site. So, I've decided to add a page specifically for stuff related to just Python. It'll include snippets of code, interesting applications, algorithms or anything else Python related. Code that is purely there as a means to demonstrate or calculate something specific will remain as it is, in the most relevant post. Most of this stuff will be migrating to my sister pages -

- in the near future.

Sorry if you were hoping to see something on the Pythonidae family of snakes - interesting as they are.

Tips for Python List Comprehensions

Just the HTML from a Jupiter Notebook
Tips for Python List Comprehensions

List Comprehensions

These are useful Python constructs of the form:

[expression for item in iterable (if condition) ]

and will return a new list.

They have a number of uses and can make code much neater (and more understandable IF you understand them). We'll look at some use-cases by example. This is based on a Medium post by Yang Zhou.

As a for-loop

In [6]:
txt = 'Betelgeuse is a Red Giant star'
chars = [char for char in txt]
print(chars)


my_array = [[ 1,3,515,2],
           [-1,66,4,99],
           [199,3,4,5]]
max_in_row = [max(row) for row in my_array]
print(max_in_row)
['B', 'e', 't', 'e', 'l', 'g', 'e', 'u', 's', 'e', ' ', 'i', 's', ' ', 'a', ' ', 'R', 'e', 'd', ' ', 'G', 'i', 'a', 'n', 't', ' ', 's', 't', 'a', 'r']
[515, 99, 199]

Using an if statement

In [7]:
names = ['Tom', 'Dick', 'harry', 'Jill', 'fred', 'Freda', 'foxtrot']

print([name for name in names if name.startswith('F')])
print([name for name in names if name.capitalize().startswith('F')])
print([name for name in names if len(name) > 4])
print([name for name in names if len(name) > 4 and name.islower()])
['Freda']
['fred', 'Freda', 'foxtrot']
['harry', 'Freda', 'foxtrot']
['harry', 'foxtrot']

More complex expressions

In [8]:
names = ['Tom', 'Dick', 'harry', 'Jill', 'fred', 'Freda', 'foxtrot']
print([name.capitalize() for name in names])

print([name if name.capitalize().startswith('F') else 'Not F' for name in names])
['Tom', 'Dick', 'Harry', 'Jill', 'Fred', 'Freda', 'Foxtrot']
['Not F', 'Not F', 'Not F', 'Not F', 'fred', 'Freda', 'foxtrot']

Applying a function to a list - alternative to map()

In [9]:
def calc_radial_vel(f_delta):
    c = 2.99979e8
    f0 = 1420e6
    return (-c*f_delta/(f_delta+f0))

f_deltas=[-800000, -400000, 0, 200000, 600000]
radial_vels =[calc_radial_vel(v) for v in f_deltas]
print(f'Radial velocities are: {radial_vels} km/s')
Radial velocities are: [169097.51972942502, 84524.93660185968, -0.0, -42244.6134347275, -126698.15570885541] km/s

Or, how about this neat way of printing out a list? (The [None, None, None, None] is an 'artifact' of Jupyter Notebook printing out the last result (4 print()s returning 'None')

In [10]:
[print(f'Velocities {a/1000:0.1f} km/s') for a in radial_vels]
Velocities 169.1 km/s
Velocities 84.5 km/s
Velocities -0.0 km/s
Velocities -42.2 km/s
Velocities -126.7 km/s
Out[10]:
[None, None, None, None, None]
RapidWeaver Icon

Made in RapidWeaver

Back
RapidWeaver Icon

Made in RapidWeaver