Compute list difference
If the order does not matter, you can simply calculate the set difference:
>>> set([1,2,3,4]) - set([2,5])
set([1, 4, 3])
>>> set([2,5]) - set([1,2,3,4])
set([5])
Get difference between two lists
To get elements which are in temp1
but not in temp2
:
In [5]: list(set(temp1) - set(temp2))
Out[5]: ['Four', 'Three']
Beware that it is asymmetric :
In [5]: set([1, 2]) - set([2, 3])
Out[5]: set([1])
where you might expect/want it to equal set([1, 3])
. If you do want set([1, 3])
as your answer, you can use set([1, 2]).symmetric_difference(set([2, 3]))
.
Python: Finding differences between elements of a list
>>> t
[1, 3, 6]
>>> [j-i for i, j in zip(t[:-1], t[1:])] # or use itertools.izip in py2k
[2, 3]
Compute the differences between the elements of two lists in python
Problem
Most Pythonic way to efficiently calculate the cartesian product of iterables and subtract the results.
Solution
The simplest and most pythonic approach:
[ abs(i-j) for i,j in itertools.product(list1, list2) ]
Depending on how large your dataset is, you may need to use a generator expression instead.
( abs(i-j) for i,j in itertools.product(list1, list2) )
Note that you don't need to explicitly define l1 and l2 since itertools.product takes n
iterables. You can simply do:
l = [[1], [3,4]]
( abs(i-j) for i,j in itertools.product(*l) )
References
itertools.product: https://docs.python.org/3/library/itertools.html#itertools.product
generator expressions: https://www.python.org/dev/peps/pep-0289/
Trying to compute difference in a list from the first value of the list
First, a list in python is written with square brackets, like that:
l = [1, 3, 5, 5, 2, 4, 6]
To get your result there are many ways. One way would be list comprehensions, for example with
[x - l[0] for x in l]
You can use map:
map(lambda x: x-l[0], l)
You could use a loop of many other ways.
Calculate difference between adjacent items in a python list
The straight approach here is the best:
x = s[1] - s[0]
for i in range(2, len(s)):
if s[i] - s[i-1] != x: break
else:
#do some work here...
python find difference between two lists
You can convert the lists to sets and run the usual set operations such as difference or symmetric difference. For example, set(b) - set(a)
evaluates to set([7, 8, 9])
.
Python, compute array difference in exact amount of elements
from collections import Counter
temp1 = ['A', 'A', 'A', 'B', 'C', 'C', 'C']
temp2 = ['A', 'B', 'C', 'C']
result = []
counts = Counter(temp2)
for item in temp1:
if item in counts and counts[item]:
counts[item] -= 1
else:
result.append(item)
print(result)
Output:
['A', 'A', 'C']
Scales O(n) and does not rely on sorted input.
This answer relies on the fact that Counter
is just a subclass of dict
, so we can use the instance as a mutable object in which to store the number of occurrences in temp2
that we still need to exclude from the result during the iteration over temp1
. The documentation states explicitly that "Counter
is a dict
subclass" and that "Counter
objects have a dictionary interface", which is a pretty good guarantee that item assignment will be supported, and that it is not necessary to treat it as a read-only object that must first be copied into a plain dict
.
Related Topics
What Does the 'U' Symbol Mean in Front of String Values
Fast Haversine Approximation (Python/Pandas)
How to Bind Self Events in Tkinter Text Widget After It Will Binded by Text Widget
How to Get Variable Data from a Class
Nested Arguments Not Compiling
How to Lowercase a String in Python
Directory-Tree Listing in Python
How to Use Stringio in Python3
Perform Commands Over Ssh with Python
Execute Code When Django Starts Once Only
Replace Console Output in Python
Why Do You Need Explicitly Have the "Self" Argument in a Python Method
Using Os.Walk() to Recursively Traverse Directories in Python
"Python" Not Recognized as a Command