You need to change the order of the for
s and if
s:
>>> [v
for k, v in d.items() if v is not None
for t in u '{}'.format(v.lower())
]
['BB', 'BB'] >>>
It can be written as follows:
x = {
'A': 'This is a Line to Be tokenized',
'B': None
}
for k, v in x.items():
if v is not None:
pat = [{
'LOWER': str(t)
}
for t in tokenizer(u '{}'.format(v.lower()))
]
res = [
[{
'LOWER': str(t)
}
for t in tokenizer(u '{}'.format(v.lower()))
]
for k, v in x.items() if v is not None
]
Output:
[
[{
'LOWER': 'this'
},
{
'LOWER': 'is'
},
{
'LOWER': 'a'
},
{
'LOWER': 'line'
},
{
'LOWER': 'to'
},
{
'LOWER': 'be'
},
{
'LOWER': 'tokenized'
}
]
]
So, originally my for comprehension to anycodings_list-comprehension generate that output is ,The if statement has to be before the anycodings_spacy location where it's gonna cause an anycodings_spacy error.,Why is v.lower() being called even with the anycodings_list-comprehension guards?,So, basically I want to convert that loop anycodings_list-comprehension into a comprehension.
Lets say I have a dict:
d = {
'AA': 'BB',
'BB': None
}
And this for comprehension:
[v
for t in u '{}'.format(v.lower()) for k, v in d.items()
]
Its obvious that its going to fail with anycodings_list-comprehension 'NoneType' object has no attribute 'lower', anycodings_list-comprehension so lets try this one:
[v
for t in u '{}'.format(v.lower()) for k, v in d.items() if v is not None
]
However, this works:
for k, v in d.items():
if v is not None: [v
for t in u '{}'.format(v.lower())
]
This is the actual code that is giving me anycodings_list-comprehension the problem, The above code was to simplify anycodings_list-comprehension the example, but given that the answer anycodings_list-comprehension provided below, I think I will post the anycodings_list-comprehension actual code:
x = {
'A': 'This is a Line to Be tokenized'
}
for k, v in x.items():
if v is not None:
pat = [{
'LOWER': str(t)
}
for t in tokenizer(u '{}'.format(v.lower()))
]
You need to change the order of the fors anycodings_spacy and ifs:
>>> [v
for k, v in d.items() if v is not None
for t in u '{}'.format(v.lower())
]
['BB', 'BB'] >>>
It can be written as follows:
x = {
'A': 'This is a Line to Be tokenized',
'B': None
}
for k, v in x.items():
if v is not None:
pat = [{
'LOWER': str(t)
}
for t in tokenizer(u '{}'.format(v.lower()))
]
res = [
[{
'LOWER': str(t)
}
for t in tokenizer(u '{}'.format(v.lower()))
]
for k, v in x.items() if v is not None
]
Output:
[
[{
'LOWER': 'this'
},
{
'LOWER': 'is'
},
{
'LOWER': 'a'
},
{
'LOWER': 'line'
},
{
'LOWER': 'to'
},
{
'LOWER': 'be'
},
{
'LOWER': 'tokenized'
}
]
]
It is equivalent to multiple for-loop. The standard syntax of nested list comprehension is shown below,Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource. ,patrick6 August 2019 at 01:33Nice post!It may get KeyError since the comprehension can be translated intof = []for i in mylist: if i['a'] > 1: if 'a' in i: f.append(i['a'])If mylist = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6}, {'b': 7} ]. The comprehension will generate a KeyError. So it is better to swap the two ifs position.ReplyDeleteRepliesReply,map( ) applies the lambda function to each item of iterable (list). Wrap it in list( ) to generate list as output
[i ** 2 for i in range(2, 10) ]
sqr = [] for i in range(2, 10): sqr.append(i ** 2) sqr
list(map(lambda i: i ** 2, range(2, 10)))
[i ** 2 for i in range(2, 10) ]
sqr = [] for i in range(2, 10): sqr.append(i ** 2) sqr
list(map(lambda i: i ** 2, range(2, 10)))
l0 = list(map(lambda x: x ** 2, filter(lambda x: x % 2 == 0, range(1, 10 ** 7)))) # Processing Time: 5.32 seconds
IF
d = {
'a': [1, 2, 1],
'b': [3, 4, 1],
'c': [5, 6, 2]
}
l1 = [x ** 2 for x in range(1, 10 ** 7) if x % 2 == 0 ] # Processing Time: 3.96 seconds
sqr = [] for x in range(1, 10 ** 7): if x % 2 == 0: sqr.append(x ** 2) # Processing Time: 5.46 seconds
Another feature of list comprehensions is guards, which also act as filters. Guards are Boolean expressions and appear on the right side of the bar in a list comprehension.,List comprehensions can also draw elements from multiple lists, in which case the result will be the list of every possible combination of the two elements, as if the two lists were processed in the nested fashion. For example,,The let in list comprehensions is recursive, as usual. But generator bindings are not, which enables shadowing:,Haskell has list comprehensions (opens new window), which are a lot like set comprehensions in math and similar implementations in imperative languages such as Python and JavaScript. At their most basic, list comprehensions take the following form.
[x | x < -someList]
[x | x < -[1. .4]]--[1, 2, 3, 4]
[f x | x < -someList]
map f someList
[x + 1 | x < -[1. .4]]--[2, 3, 4, 5]
[x | Just x < -[Just 1, Nothing, Just 3]]--[1, 3]
For large lists with one million elements, filtering lists with list comprehension is 40% faster than the built-in filter() method.,[Spoiler] Which function filters a list faster: filter() vs list comprehension? For large lists with one million elements, filtering lists with list comprehension is 40% faster than the built-in filter() method.,To answer this question, I’ve written a short script that tests the runtime performance of filtering large lists of increasing sizes using the filter() and the list comprehension methods. ,The code compares the runtimes of the filter() function and the list comprehension variant to filter a list. Note that the filter() function returns a filter object, so you need to convert it to a list using the list() constructor.
import time # Compare runtime of both methods list_sizes = [i * 10000 for i in range(100) ] filter_runtimes = [] list_comp_runtimes = [] for size in list_sizes: lst = list(range(size)) # Get time stamps time_0 = time.time() list(filter(lambda x: x % 2, lst)) time_1 = time.time()[x for x in lst if x % 2] time_2 = time.time() # Calculate runtimes filter_runtimes.append((size, time_1 - time_0)) list_comp_runtimes.append((size, time_2 - time_1)) # Plot everything import matplotlib.pyplot as plt import numpy as np f_r = np.array(filter_runtimes) l_r = np.array(list_comp_runtimes) print(filter_runtimes) print(list_comp_runtimes) plt.plot(f_r[: , 0], f_r[: , 1], label = 'filter()') plt.plot(l_r[: , 0], l_r[: , 1], label = 'list comprehension') plt.xlabel('list size') plt.ylabel('runtime (seconds)') plt.legend() plt.savefig('filter_list_comp.jpg') plt.show()
A list comprehension may have an optional associated if clause to filter items out of the result.,List comprehension with if clause can be thought of as analogous to the filter() function as they both skip an iterable’s items for which the if clause is not true. Following example filters a list to exclude odd numbers.,Iterable’s items are skipped for which the if clause is not true.,For example, in [x for x in L], the iteration variable x overwrites any previously defined value of x and is set to the value of the last item, after the resulting list is created.
L = [] L.append(0) L.append(1) L.append(4) L.append(9) L.append(16) print(L) # Prints[0, 1, 4, 9, 16]
L = [] for x in range(5): L.append(x ** 2) print(L) # Prints[0, 1, 4, 9, 16]
L = [x ** 2 for x in range(5) ] print(L) # Prints[0, 1, 4, 9, 16]
L = [x * 3 for x in 'RED' ] print(L) # Prints['RRR', 'EEE', 'DDD']
# Convert list items to absolute values vec = [-4, -2, 0, 2, 4] L = [abs(x) for x in vec] print(L) # Prints[4, 2, 0, 2, 4]
# Remove whitespaces of list items colors = [' red', ' green ', 'blue '] L = [color.strip() for color in colors] print(L) # Prints['red', 'green', 'blue']