Topic: https://russell.ballestrini.net/python-image-grabber-pig-py/
hide preview

What's next? verify your email address for reply notifications!

JasonG-rb 12y, 256d ago

You're a python ninja. Here's my favorite line:

filename = img_uri.split('/')[-1]

It intrigues me. With the perl background I'm familiar with the whole split routine...but the [-1]... -1 from what? It's a nice magic :)

remark link
hide preview

What's next? verify your email address for reply notifications!

russell 12y, 256d ago [edited]

In python when you string split() a list (sometimes referred to as array in other languages) is returned. From that list I'm asking for the last index.

Example:

# a string
img_uri = "www.foxhop.net/attachments/pic1.jpg"

# a list of strings
img_uri_parts = img_uri.split('/')

print img_uri_parts
# >>> ['www.foxhop.net', 'attachments', 'pic1.jpg']

# I want the filename, last part of the uri
# In this case img_uri[2]
# This will not always be true.  
# I always need the last element of the list.

print img_uri_parts[-1]
# >>> 'pic1.jpg'

For more information take a look at this page:

http://docs.python.org/tutorial/datastructures.html

hide preview

What's next? verify your email address for reply notifications!