我正在使用包含纯数字数据的列表,我希望根据它们的数值差异比较一个列表:
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
鉴于此,我希望该程序将sub_pattern与primary_pattern进行比较:
>>> primary_pattern >= sub_pattern
>>> True
所以,基本上,
221212 <--- Above seq.
2121 <--- This is a section of the above seq.
答案 0 :(得分:0)
你可以创建一个类来计算模式1 ^ 2 ^ 1 ^ 2并定义&gt; =的意义(这似乎是字符串中的内容)。
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
答案 1 :(得分:0)
假设你的意思是一个列表中的差异总和比另一个更大,意味着&gt; =那么你可以用自己压缩列表并总结差异......
>>> 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
注意:如果序列严格排序,则您不需要abs()
。