迭代元组列表 [(' 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)
这是我遇到的问题
答案 0 :(得分:2)
这是一种简单的方法,您可以在以后进行扩展:
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)
答案 1 :(得分:0)
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))
答案 2 :(得分:0)
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)])