Python 正在尝试筛选嵌套元组
我试图将一个元组列表排序为一个只包含具有相同数量的偶数和奇数整数的元组的列表,这些整数不是按顺序排列的。以下是到目前为止的代码(很抱歉,如果它很混乱,我刚刚开始学习python) 这是我得到的输出:Python 正在尝试筛选嵌套元组,python,list,dictionary,filter,tuples,Python,List,Dictionary,Filter,Tuples,我试图将一个元组列表排序为一个只包含具有相同数量的偶数和奇数整数的元组的列表,这些整数不是按顺序排列的。以下是到目前为止的代码(很抱歉,如果它很混乱,我刚刚开始学习python) 这是我得到的输出: Number of unsorted combos: 141999312 run test: ((1, 2, 3, 4, 5), (1,)) ((1, 2, 3, 4, 5), (2,)) 1 2 3 4 5 1 {} done Number of combos = 0 需要帮助找出它为什
Number of unsorted combos: 141999312
run test:
((1, 2, 3, 4, 5), (1,))
((1, 2, 3, 4, 5), (2,))
1
2
3
4
5
1
{}
done
Number of combos = 0
需要帮助找出它为什么不返回任何结果,我假设它在过滤器定义中
谢谢你的帮助,谢谢 在创建
偶数
列表之前,您正在将组合重新定义为空列表
combos =[]
even_odd =[
{(t[0][0][0], t[0][0][1], t[0][0][2], t[0][0][3], t[0][0][4]), (t[0][1][0])}
for t in combos
if (t[0][0][0] % 2 == 0 and t[0][0][1] % 2 == 0 and t[0][0][2] % 2 == 0 and t[0][0][3] % 2 != 0 and t[0][0][4] % 2 != 0) and (t[0][1][0] % 2 != 0)
]
这意味着组合中t的
将循环零次,因此偶数
也将是空的。啊,好吧,这对我来说太愚蠢了哈哈。删除该行会给我一个“int”不是可下标的错误。想法?一个错误是在偶数
和序列
中,您想用一个元素创建元组。这只是一个int:(1)
,这是一个包含一个元素的元组:(1,)
combos =[]
even_odd =[
{(t[0][0][0], t[0][0][1], t[0][0][2], t[0][0][3], t[0][0][4]), (t[0][1][0])}
for t in combos
if (t[0][0][0] % 2 == 0 and t[0][0][1] % 2 == 0 and t[0][0][2] % 2 == 0 and t[0][0][3] % 2 != 0 and t[0][0][4] % 2 != 0) and (t[0][1][0] % 2 != 0)
]