Reputation: 21
Iterating through a list of tuples [('a', 4), ('b', 5), ('c', 1), ('d', 3), ('e', 2), ('f',6)]
attempting to get the matching pair as output
Any guidance would be greatly appreciated.
here is what I have so far:
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)
This where I am getting stuck
Upvotes: 1
Views: 6167
Reputation: 19947
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)])
Upvotes: 0
Reputation: 853
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))
Upvotes: 0
Reputation: 174624
Here is a simple way to do it, which you can expand on later:
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)
Upvotes: 2