[Moved from my other blog page]
This
time, I'd like to discuss list comprehension. This is another tool used
in Python and Jython. It's a very concise way of stepping through a
list or a string, and processing information. You can build list
comprehensions from the very basic to a complicated expression.
In an earlier post, I demonstrated a list comprehension at work with the dir() function, in returning information.
Here is a basic list comprehendion and they are enclosed in square brackets:
[expression for variable in sequence [if test-expression]]
test-expression is optional.
Let's see it in action:
>>>[x for x in ['one', 'two', 'three']]
['one', 'two', 'three']
To
make it clear in my mind, I think of the first expression as the result
I want, and build a list comprehension from that. Like so:
>>> [x.upper() for x in ['one', 'two', 'three']]
['ONE', 'TWO', 'THREE']
How about this?
>>> [x.startswith('t') for x in ['one', 'two', 'thre']]
[False, True, True]
It even does boolean expressions.
>>> a = 'ABC'
>>> word = ['Apple', 'Bird', 'Cat']
>>> [(x, y) for x in a for y in word if y.startswith(x)]
[('A', 'Apple'), ('B', 'Bird'), ('C', 'Cat')]
The
above is an example of a nested list comprehension with an if
test-expression. You noticed the result was list of tuples. You can put
the result into a variable and print only what you need.
>>> result = [(x, y) for x in a for y in word if y.startswith(x)]
>>> result[0][1] <<< here I indexed the tuple
'Apple'
>>> result
[('A', 'Apple'), ('B', 'Bird'), ('C', 'Cat')]
Or this:
>>> [x for (x, y) in result]
['A', 'B', 'C']
List comprehension can process files.
>>> [x for x in open('/home/patrick/script2.py')]
['import sys\n', 'print(sys.path)\n', 'x = 2\n', 'print(x ** 32)\n', '\n']
If you are using list comprehension in a script, and you want to show the results on the screen, just put the print before the list comprehension.
>>>print [x for x in open('/home/patrick/script2.py')]
['import sys\n', 'print(sys.path)\n', 'x = 2\n', 'print(x ** 32)\n', '\n']
Can you make a complex list comprehension? You can, with some work.
I
was working on the Java class GregorianCalendar. I wanted to see if my
system could disply other languages. So, I used the
getAvailableLocales() method. However, this method returned a list of
objects, not strings. I wanted to search for a particular county, by
searching a partial string, but coudln't as U got an error saying x was
not in the list.
As
a workaround, I had to convert it to a string to search and have it
return the index of the list element.. So, my original loop went like
this:
for x in range(len(locale)):
t = str(locale[x])
if t.endswith('US'):
subloc.append(locale[x])
After 3 hours or so, I was able to build a list comprehension that worked exactly as my loop:
subloc = [locale[x[0]] for x in enumerate(map(str, locale)) if x[1].endswith('US')]
Can you see how I did it? I will discuss enumerate() another day.
Is
this really the Pythonic way? Maybe not, but list comprehension, when
successful does marvel at the conciseness and simplicity of this
powerful language.
List
comprehensions allow Python/Jython do the work for you. This minimizes
development time and is a great benefit of this language.
If you would like to see the full script, email me at psykiatris@gmail.com.
Till next time. Happy coding!
This is not an official Python page. It is simply a series of posts demonstrating code and other things to help other newbies to the Python language. Because I've worked with Java before, I'm drawn to the Java implementation of Python, Jython, so my posts will be more about that. But any non-Java related code should work in Python, especially if I discuss built-ins. Hope what I say is helpful. If there are mistakes, or I'm not clear on something, please let me know. Happy coding!
Showing posts with label list comprehension. Show all posts
Showing posts with label list comprehension. Show all posts
Tuesday, August 21, 2018
Jython: Comprehending list comprehanesions
Dictionaries and list comprehension in Jython/Python
[Moved from my other blog page]
I know I'm getting carried away with this list comprehension topic, but I'm finding it a lot of fun to see what else I can do with list comprehension. It also matches my other favorite topic (for another day) of slicking and indexing. You will see some of it here. What I'm doing here is exploring more ways of doing list comprehension. I''m sure most of you know how to work with dictionaries.
Create a dictionary:
>>> D = {'a': 'aardvark', 'b': 'bear', 'c': 'cat', 'd': 'dog', 'e': 'elephant', 'f': 'fox', 'g': 'giraffe', 'h': 'hippo', 'i': 'insect'}
List comprehensions on a dictionary are a little more complicated than a list, but it's fairly easy.
>>> [x for x in D]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
>>> [x for x in D.values()]
['aardvark', 'bear', 'cat', 'dog', 'elephant', 'fox', 'giraffe', 'hippo', 'insect']
Edited later:
>>> [x for x in D.items()]
[('a', 'apple'), ('b', 'balloon'), ('c', 'cat'), ('d', 'dog'), ('e', 'elephant'), ('f', ['fox'])]
>>> [x for x in D.items() if x[1] == 'elephant']
[('e', 'elephant')] # In this case, it sliced by element. Use x[0] to scan the keys.
You can use either .items() or .iteritems() to get key, value pairs,
Back to original post:
Say I want to get the last letter of each item in my dictionary values:
>>> [x[-1:] for x in D.values()]
['k', 'r', 't', 'g', 't', 'x', 'e', 'o', 't']
How about filtering:
# Get the elements where the last letter matches 't'.
>>> [x for x in D.values() if x[-1:] == 't']
['cat', 'elephant', 'insect']
As you see, slicing comes in handy with coding list comprehensions.
An earlier post, I eexplored how the dir() function can really be useful. Here's a little expansion on that topic:
>>> import os
>>> dir(os)
>>> len(dir(os))
110
I realized you really don't need a .startswith() or .endswith() call in a list comprehension to find matching elements. Just slice away!
Filter by first letter:
>>> [x for x in dir(os) if x[0] == 'e']
['environ', 'errno', 'error', 'extsep']
Outside of a list comprehensiuon, you would have to do this to get the first letter of an element.
>>> dir(os)[41]
'environ'
>>> dir(os)[41][0]
'e'
In the list comprehension, I didn't have to. Why? Because x already indexes the list, so I'm slicing each element to return any string beginning with 'e'.
If you need to know the index numbers to process later, you could:
>>> [dir(os).index(x) for x in dir(os) if x[0] == 'e']
[41, 42, 43, 44]
>>> [(dir(os).index(x),x) for x in dir(os) if x[0] == 'e']
[(41, 'environ'), (42, 'errno'), (43, 'error'), (44, 'extsep')]
As I've mentioned before, thinking a list comprehension through is simply a matter of putting the result you want first, then building the list comprehension to reach your result, as you can see in the example above. Now you have index intergers for all the elements beginning with 'e' from the list.
It's not necessary to use the keyword print inside the interactive session, but if you put a list comprehension in a script and need to show your result, put print before the list comprehension (e.g. print [x for ...])
I know there are programmers out there who are smareter and more experienced than me. As you see, I'm a quick learner and I relish the opportunity to continue learning.
I'm not shoinw off at all. Instead, I view this as a way to prod you and your ideas, so you can achieve your own success and sense of satisfaction at having done it. I am limited in my hearing and eyesight, but it doesn't stop me from doing what I can and stay busy in what I really enjoy.
Until later, happy coding!
I know I'm getting carried away with this list comprehension topic, but I'm finding it a lot of fun to see what else I can do with list comprehension. It also matches my other favorite topic (for another day) of slicking and indexing. You will see some of it here. What I'm doing here is exploring more ways of doing list comprehension. I''m sure most of you know how to work with dictionaries.
Create a dictionary:
>>> D = {'a': 'aardvark', 'b': 'bear', 'c': 'cat', 'd': 'dog', 'e': 'elephant', 'f': 'fox', 'g': 'giraffe', 'h': 'hippo', 'i': 'insect'}
List comprehensions on a dictionary are a little more complicated than a list, but it's fairly easy.
>>> [x for x in D]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
>>> [x for x in D.values()]
['aardvark', 'bear', 'cat', 'dog', 'elephant', 'fox', 'giraffe', 'hippo', 'insect']
Edited later:
>>> [x for x in D.items()]
[('a', 'apple'), ('b', 'balloon'), ('c', 'cat'), ('d', 'dog'), ('e', 'elephant'), ('f', ['fox'])]
>>> [x for x in D.items() if x[1] == 'elephant']
[('e', 'elephant')] # In this case, it sliced by element. Use x[0] to scan the keys.
You can use either .items() or .iteritems() to get key, value pairs,
Back to original post:
Say I want to get the last letter of each item in my dictionary values:
>>> [x[-1:] for x in D.values()]
['k', 'r', 't', 'g', 't', 'x', 'e', 'o', 't']
How about filtering:
# Get the elements where the last letter matches 't'.
>>> [x for x in D.values() if x[-1:] == 't']
['cat', 'elephant', 'insect']
As you see, slicing comes in handy with coding list comprehensions.
An earlier post, I eexplored how the dir() function can really be useful. Here's a little expansion on that topic:
>>> import os
>>> dir(os)
>>> len(dir(os))
110
I realized you really don't need a .startswith() or .endswith() call in a list comprehension to find matching elements. Just slice away!
Filter by first letter:
>>> [x for x in dir(os) if x[0] == 'e']
['environ', 'errno', 'error', 'extsep']
Outside of a list comprehensiuon, you would have to do this to get the first letter of an element.
>>> dir(os)[41]
'environ'
>>> dir(os)[41][0]
'e'
In the list comprehension, I didn't have to. Why? Because x already indexes the list, so I'm slicing each element to return any string beginning with 'e'.
If you need to know the index numbers to process later, you could:
>>> [dir(os).index(x) for x in dir(os) if x[0] == 'e']
[41, 42, 43, 44]
>>> [(dir(os).index(x),x) for x in dir(os) if x[0] == 'e']
[(41, 'environ'), (42, 'errno'), (43, 'error'), (44, 'extsep')]
As I've mentioned before, thinking a list comprehension through is simply a matter of putting the result you want first, then building the list comprehension to reach your result, as you can see in the example above. Now you have index intergers for all the elements beginning with 'e' from the list.
It's not necessary to use the keyword print inside the interactive session, but if you put a list comprehension in a script and need to show your result, put print before the list comprehension (e.g. print [x for ...])
I know there are programmers out there who are smareter and more experienced than me. As you see, I'm a quick learner and I relish the opportunity to continue learning.
I'm not shoinw off at all. Instead, I view this as a way to prod you and your ideas, so you can achieve your own success and sense of satisfaction at having done it. I am limited in my hearing and eyesight, but it doesn't stop me from doing what I can and stay busy in what I really enjoy.
Until later, happy coding!
Subscribe to:
Posts (Atom)