6

我有一个元组列表。例如,我有以下内容:

a=[('jamy', 'k'), ('Park', 'h'), ('kick', 'p'), ('an', 'o'),('an',
'o'),('an', 'o'),('an', 'r'), ('car', 'k'), ('rock', 'h'), ('pig',
'p')]

另一个清单,

b = ['k','h','p']

我想从列表 a 的第二个元组元素中找到列表 b 中的模式。

在上面的例子中,输出应该返回,

[('jamy','Park','kick'),('car','rock','pig')]

有人可以帮助我实现我的目标吗?

4

5 回答 5

1
c = [(a[x][0], a[x+1][0], a[x+2][0])
                        for x, _ in enumerate(a) 
                                 if a[x][1] == b[0] and 
                                    a[x+1][1] == b[1] and
                                    a[x+2][1] == b[2]]
于 2013-10-26T21:18:25.793 回答
1
a = [('jamy', 'k'), ('Park', 'h'), ('kick', 'p'),
     ('an', 'o'), ('an', 'o'), ('an', 'o'), ('an', 'r'),
     ('car', 'k'), ('rock', 'h'), ('pig', 'p')]

b = ['k', 'h', 'p']

filtered = [ele for ele in a if ele[1] in b]

def split_list(_list, idx_range):
    _t = []
    _temp = []
    _d = {idx + 1: ele for idx, ele in enumerate(_list)}
    for k in _d:
        if k % idx_range == 0:
            _t.append(_d[k])
            _temp.append(_t)
            _t = []
        else:
            _t.append(_d[k])
    return _temp

_nested = split_list(filtered, len(b))

_l1 = []
for outer in _nested:
    _s = ''
    for inner in outer:
        _s += inner[1]
    _l1.append(_s)

_l2 = [_nested[idx] for idx, ele in enumerate(_l1) if ''.join(b) == ele]

final = []
for ele in _l2:
    tup = [e[0] for e in ele]
    final.append(tuple(tup))

print final

输出:

[('jamy', 'Park', 'kick'), ('car', 'rock', 'pig')]
于 2013-10-26T22:50:41.410 回答
1

试试这个片段。

list_of_values = [
    ('jamy', 'k'), ('Park', 'h'), ('kick', 'p'), ('an', 'o'), ('an', 'o'),
    ('an', 'o'), ('an', 'r'), ('car', 'k'), ('rock', 'h'), ('pig', 'p')
]
pattern = ('k','h','p')

# Important part
matches = [
    values for values, keys in (
        zip(*list_of_values[i:i + len(pattern)])
        for i in range(len(list_of_values) - len(pattern) + 1)
    ) if keys == pattern
]

print(matches)
>> [('jamy', 'Park', 'kick'), ('car', 'rock', 'pig')]
于 2013-10-26T21:49:20.263 回答
1

假设 的长度b为 3,即使acontains'k','h','p'使得它们并不总是正确地相互跟随,以下代码也将起作用,如下所示:

a=[('test', 'k'), ('jamy', 'k'), ('Park', 'h'), ('kick', 'p'), ('car', 'k'),
('an', 'r'),('rock', 'h'), ('see','k'), ('it','h'),('an', 'o'),('works', 'p')]

b = ['k','h','p']

将产生:

[('jamy', 'Park', 'kick'), ('see', 'it', 'works')]

代码:

letters_a = "".join(str(tup[1]) for tup in a)
letters_b = "".join(str(letter) for letter in b)
regex = re.compile(r'(%s)[^%s]*(%s)[^%s]*(%s)' 
                  % (letters_b[0],letters_b[:2],letters_b[1],letters_b,letters_b[2]))
#for this example, the above line translates to:
#regex = re.compile(r'(k)[^kh]*(h)[^khp]*(p)')
match = re.finditer(regex, letters_a)

results=[]
for m in match:
    first,second,third = m.start(1), m.start(2), m.start(3)
    results.append((a[first][0],a[second][0],a[third][0]))

print results
于 2013-10-26T21:28:51.280 回答
0

鉴于:

a = [
    ('jamy', 'k'), ('Park', 'h'), ('kick', 'p'), ('an', 'o'), ('an', 'o'),
    ('an', 'o'), ('an', 'r'), ('car', 'k'), ('rock', 'h'), ('pig', 'p')
]
b = ('k','h','p')

如果您的目标是从列表中收集a基于第二个元素的元组组,该第二个元素的顺序与您的序列相同,b您可以这样做:

result=[]
for sl in [a[i:i+len(b)] for i in range(0,len(a))]:
    if tuple([tp[1] for tp in sl])==b:
        result.append(tuple(tp[0] for tp in sl))

print result        
# [('jamy', 'Park', 'kick'), ('car', 'rock', 'pig')]
于 2013-10-27T00:39:52.107 回答