繁体   English   中英

遍历元组列表

[英]Iterating through a list of tuples

遍历元组列表[('a',4),('b',5),('c',1),('d',3),('e',2),('f ”,6)]

尝试获取匹配对作为输出

任何指导将不胜感激。

这是我到目前为止所拥有的:

data = [('a', 4), ('b', 5), ('c', 1), ('d', 3), ('e', 2), ('f',6)]
new_list = []
vowels = 'aeiou'
consonants = 'bcdfghjklmnpqrstvxw'
consonants = list(consonants)
vowels = list(vowels)
for idx, (a,b) in enumerate(data):
    if (a) in vowels or (a) in consonants and (b) % 3 == 0:
        new_list.append(idx)
print tuple(new_list)

这就是我被困住的地方

这是一种简单的方法,以后可以进行扩展:

nums = [('a', 4), ('b', 5), ('c', 1), ('d', 3), ('e', 2), ('f',6)]
vowels = ['a','e','i','o','u']
results = []
for k,v in enumerate(nums):
    for i,j in enumerate(nums[:]):
        if v == j:
            continue
        if v[0] in vowels and j[0] in vowels:
            if (v[1]+j[1]) % 3 == 0:
                if (i,k) not in results:
                    results.append((k,i))
        if v[0] not in vowels and j[0] not in vowels:
            if (v[1]+j[1]) % 3 == 0:
                if (k,i) not in results:
                    results.append((i,k))
print(results)
data = [('a', 4), ('b', 5), ('c', 1), ('d', 3), ('e', 2), ('f',6)]
new_list = []
vowels = 'aeiou'
for idx,(a,b) in enumerate(data):
     for _idx,(_a,_b) in enumerate( data[idx+1:]):
           if bool(a in vowels)==bool(_a in vowels) and(b+_b)%3==0:
               new_list.append((idx,_idx+idx+1))
a = [('a', 4), ('b', 5), ('c', 1), ('d', 3), ('e', 2), ('f',6)] 
c = np.asarray([[int(e[0] in 'aeiou'), e[1]] for e in a])
e = (c[:,None] + c) % 3
f = np.asarray(np.where((e[...,0]%2==0) & (e[...,1]==0))).T.tolist()
print set([tuple(sorted(i)) for i in f if i[0]!=i[1]])

set([(1L, 2L), (3L, 5L), (0L, 4L)])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM