init_tuple = [(0, 1), (1, 2), (2, 3)]
result = sum(n for _, n in init_tuple)
print(result)
The output for this code is 6. Could someone explain how it worked?
Your code extracts each tuple and sums all values in the second position (i.e. [1]).
If you rewrite it in loops, it may be easier to understand:
init_tuple = [(0, 1), (1, 2), (2, 3)]
result = 0
for (val1, val2) in init_tuple:
result = result + val2
print(result)
The expression (n for _, n in init_tuple) is a generator expression. You can iterate on such an expression to get all the values it generates. In that case it reads as: generate the second component of each tuple of init_tuple.
(Note on _: The _ here stands for the first component of the tuple. It is common in python to use this name when you don't care about the variable it refers to (i.e., if you don't plan to use it) as it is the case here. Another way to write your generator would then be (tup[1] for tup in init_tuple))
You can iterate over a generator expression using for loop. For example:
>>> for x in (n for _, n in init_tuple):
>>> print(x)
1
2
3
And of course, since you can iterate on a generator expression, you can sum it as you have done in your code.
To get better understanding first look at this.
init_tuple = [(0, 1), (1, 2), (2, 3)]
sum = 0
for x,y in init_tuple:
sum = sum + y
print(sum)
Now, you can see that what above code does is that it calculate sum of second elements of tuple, its equivalent to your code as both does same job.
for x,y in init_tuple:
x hold first value of tuple and y hold second of tuple, in first iteration:
x = 0, y = 1,
then in second iteration:
x = 1, y = 2 and so on.
In your case you don't need first element of tuple so you just use _ instead of using variable.
Related
Essentially I am looking for an efficient way to implement custom comparators using heapq.
For instance x = [('a',5),('c',3),('d',2),('e',1)]
I could heapify it heapq.heapify(x) then pop the min value heapq.heappop(x) which would return ('a', 5). How can I make it such that it returns in reverse lexicographical order ('e', 1)?
I know the convention when it comes to numbers is simply multiply the first element of the tuple by -1. Are there simple tricks like that when it comes to strings? I know I could potentially implement a map from a to z ... z to a but it sounds cumbersome.
For numbers you would do something like this:
import heapq
x = [(1, 5), (3, 3), (4, 2), (5, 1)]
x = [(-a, b) for a, b in x]
heapq.heapify(x)
result = heapq.heappop(x)
result = (-result[0], result[1])
Similarly, I would do this with letters:
import heapq
x = [('a',5), ('c',3), ('d',2), ('e',1)]
x = [(-ord(a), b) for a, b in x]
heapq.heapify(x)
result = heapq.heappop(x)
result = (chr(-result[0]), result[1])
You may want to treat similarly also the second element of each tuple
I am very new to python and I wanted to know, how can I assign an integer incrementally to every 3 values in a series. Might be better to explain with an example:
The result should contain the following pattern :
(1,1),(2,1),(3,1),(4,2),(5,2),(6,2),(7,3).......
Alternatively, just using a simple for loop and two variables:
val1 = 1
val2 = 1
for a in range(10): # this number is arbitrarily large
for b in range(3): # this number is not arbitrary
print( (val1, val2) )
val1 += 1
val2 += 1
You can use the floor division operator with a list comprehension:
n = range(1, 10)
res = [(i, idx//3 + 1) for idx, i in enumerate(n)]
print(res)
[(1, 1), (2, 1), (3, 1),
(4, 2), (5, 2), (6, 2),
(7, 3), (8, 3), (9, 3)]
If you want this to be infinite, I'd suggest looking into the utilities in itertools
from itertools import count
def integers_rep(n):
for i in count(1):
for _ in range(n):
yield i
def gen_sequence():
return zip(count(1), integers_rep(3))
count(n) is an iterable which will yield the sequence n, n+1, n+2, ... ad infinitum starting at it's first argument (which defaults to 0 if nothing is passed).
gen_sequence which would create your sequence, uses zip which takes N-iterables and produces a sequence of N-tuples (two sequences => pairs, three => triples, etc.). By taking count and the other sequence, you will be able to then get their pairs.
By using these tools, you would be able to define everything at a high level (which can be useful for mathematical sequences).
I've got a list
a = [(1,2),(1,4),(2,6),(1,8),(3,6),(1,10),(1,6)]
If I say that:
for x in a:
if x[0]==1:
print x
I get the expected result : (1,2) (1,4) (1,8) (1,10) (1,6)
However I want to remove all the occurrences of all the tuples in the format (1,x),So
for x in a:
if x[0]==1:
a.remove(x)
I thought that all the occurences should be removed.However when i say
Print a
I get [(1,4),(2,6),(3,6),(1,6)]
Not all the tuples were removed. How do I do it.??
Thanks
I'd use list comprehension:
def removeTuplesWithOne(lst):
return [x for x in lst if x[0] != 1]
a = removeTuplesWithOne([(1,2),(1,4),(2,6),(1,8),(3,6),(1,10),(1,6)])
For me it's more pythonic than built-in filter function.
P.S. This function does not change your original list, it creates new one. If your original list is huge, i'd probably use generator expression like so:
def removeTuplesWithOne(lst):
return (x for x in lst if x[0] != 1)
This isn't the same approach as yours but should work
a = filter(lambda x: x[0] != 1, a)
You can use list comprehension like this, to filter out the items which have 1 as the first element.
>>> original = [(1, 2), (1, 4), (2, 6), (1, 8), (3, 6), (1, 10), (1, 6)]
>>> [item for item in original if item[0] != 1]
[(2, 6), (3, 6)]
This creates a new list, rather than modifying the existing one. 99% of the time, this will be fine, but if you need to modify the original list, you can do that by assigning back:
original[:] = [item for item in original if item[0] != 1]
Here we use slice assignment, which works by replacing every item from the start to the end of the original list (the [:]) with the items from the list comprehension. If you just used normal assignment, you would just change what the name original pointed to, not actually modify the list itself.
You can do it with a generator expression if you're dealing with huge amounts of data:
a = [(1,2),(1,4),(2,6),(1,8),(3,6),(1,10),(1,6)]
# create a generator
a = ((x,y) for x, y in a if x == 1)
# simply convert it to a list if you need to...
>>> print list(a)
[(1, 2), (1, 4), (1, 8), (1, 10), (1, 6)]
I have the following vectors in a list:
Q= [(0, 0.2815994630432826), (2, 0.678491614691639), (3, 0.678491614691639)]
I tried summing-up only the floating parts
(ie Q= 0.2815994630432826 + 0.678491614691639 + 0.678491614691639) as the first parts are indices which I do not want. See code below:
aba=[]
for doc in corpus_tfidf:
con = round(np.sum(doc),2)
aba.append(con)
print aba
Here is the result I got: (6.64)
My code added up the indices and then the floats. My intention was to add-up only the float and out-put the total. Any ideas?, thanks in advance.
Note:(The for loop is because Q is just one out of hundreds of documents with such vectors).
Something like this? (This requires that the elements you want to sum up are in the second position in the tuples)
>>> Q= [(0, 0.2815994630432826), (2, 0.678491614691639), (3, 0.678491614691639)]
>>> from operator import itemgetter
>>> sum(map(itemgetter(1), Q))
1.6385826924265605
Otherwise, you could just build up a list and sum that up.
>>> sum([val for _, val in Q])
1.6385826924265605
You could use,
reduce(lambda x, y: x + y, map(lambda x : x[1], Q))
for getting the sum of floating points in Q.
Map gets the second part of the tuple alone in a separate list and reduce calculates the sum of the elements of this new list.
Or simply, you could do this too
sum([x[1] for x in Q])
Or this one,
reduce(lambda x, y : (0, x[1] + y[1]), Q)[1]
I think the fastest and easier way to go for you would involve using sum function and list comprehension
>>> Q
[(0, 0.2815994630432826), (2, 0.678491614691639), (3, 0.678491614691639)]
>>> sum([i[1] for i in Q])
1.6385826924265605
>>>
When i executed the following python script
list= (1,2,3,4,1,2,7,8)
for number in list:
item1= number
item2= list[list.index(item1)+2]
couple= item1, item2
print couple
the goal is to link each number with the second following
I obtain this result
(1, 3)
(2, 4)
(3, 1)
(4, 2)
(1, 3)
(2, 4)
(and then the index gets out of range but this is not the problem)
My question is why the number 1 in the fifth line is still coupled to the number 3 and how can i make that it is coupled to the number 7; idem for the number 2 in the sixth line that should be coupled to the number 8.
additional question
what do I do if i only want to make a list of the couples that start with 1: [(1,3), (1,7)]
list.index returns the offset of the first occurrence of the value in the list - thus if you do [1,1,1].index(1), the answer will always be 0, even though 1 and 2 are also valid answers.
Instead, try:
from itertools import islice, izip, ifilter
mylist = [1,2,3,4,1,2,7,8]
for pair in ifilter(lambda x: x[0]==1, izip(mylist, islice(mylist, 2, None))):
print pair
results in
(1, 3)
(1, 7)
xs.index(x) gives you the index of the first occurence of x in xs. So when you get to the second 1, .index gives you the index of the first 1.
If you need the index alongside the value, use enumerate: for i, number in enumerate(numbers): print number, numbers[i+2].
Note that I deliberately didn't use the name list. It's the name of a built-in, you shouldn't overwrite it. Also note that (..., ...) is a tuple (and therefore can't be changed), not a list (which is defined in square brackets [..., ...] and can be changed).
You have duplicates in the list so index always returns the first index.
Start your program with for index in range(len(list) - 1)
You are using .index which returns the first occurrence of number.
consider:
for number in range(len(list)):
item1= list[number]
item2= list[number+2]
couple= item1, item2
print couple
>>> zip(lst, lst[2:])
[(1, 3), (2, 4), (3, 1), (4, 2), (1, 7), (2, 8)]
To get only pairs (1, X):
>>> [(a, b) for (a, b) in zip(lst, lst[2:]) if a == 1]
[(1, 3), (1, 7)]
Recommended reading:
http://docs.python.org/tutorial/datastructures.html
http://docs.python.org/howto/functional.html