def oddTuples(aTup):
# Your Code Here
tup = ();
for t in aTup:
if t%2 != 0:
tup = tup + (t,);
return tup;
print(oddTuples(((1), (2), (2), (23), (3), (4))))
我得到了输出(1,23,3)但是我不想要这个输出我尝试了很多次但我没有得到
我想打印奇数索引就像输出(1,2,3)
一样答案 0 :(得分:2)
试试这个:
def oddTuples(aTup):
return aTup[::2]
print(oddTuples(((1), (2), (2), (23), (3), (4))))
# (1, 2, 3)
<强>解释强>
答案 1 :(得分:1)
def oddTuples(aTup):
return aTup[0::2]
print(oddTuples(((1), (2), (2), (23), (3), (4))))
它会打印你想要的东西:
(1, 2, 3)
答案 2 :(得分:0)
这会有效!
def oddTuples(aTup):
# Your Code Here
tup = ();
for i in range(0,len(aTup)):
if i%2 == 0:
tup = tup + (aTup[i],);
return tup;
print(oddTuples(((1), (2), (2), (23), (3), (4))))