I am working with lists containing purely numerical data, and I wish to compare one list to another in terms of their numerical differences:
primary_seq = [0, 2, 4, 5, 7, 8, 10]
...Code code code
primary_pattern = 2^2^1^2^1^2
sub_seq = [0, 2, 3, 5, 6]
...Code code code
sub_pattern = 2^1^2^1
Given this, I would like the program to compare the sub_pattern with the primary_pattern:
>>> primary_pattern >= sub_pattern
>>> True
So, basically,
221212 <--- Above seq.
2121 <--- This is a section of the above seq.
You can make a class to calculate the pattern 1^2^1^2 and define the sense of >= (which seems to be the in of strings).
class pattern:
pattern=''
def __init__(self,lst):
pat=str([lst[i+1]-lst[i] for i in range(len(lst)-1)])[1:-1]
self.pattern=pat.replace(', ','^')
def __repr__(self): return self.pattern
def __ge__(self,other): return (other.pattern in self.pattern)
>>>primary_pattern =pattern(primary_seq)
>>>sub_pattern = pattern(sub_seq)
>>>sub_pattern
2^1^2^1
>>>primary_pattern >= sub_pattern
True
Assuming you mean the sum of differences is greater in one list than another to mean >= then you can zip up the list with itself and sum the differences...
>>> primary_pattern = [abs(a-b) for a,b in zip(primary_seq[1:], primary_seq[:-1])]
>>> sub_pattern = [abs(a-b) for a,b in zip(sub_seq[1:], sub_seq[:-1])]
>>> sum(primary_pattern[1:-1]) >= sum(sub_pattern)
True
Note: If sequences are strictly ordered then you don't need the abs()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.