gpt4 book ai didi

python - 迭代元组列表

转载 作者:行者123 更新时间:2023-12-01 03:05:20 24 4
gpt4 key购买 nike

迭代元组列表[('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)

关于python - 迭代元组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43486590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com