排除或不包括python中的项目

时间:2017-12-14 14:06:34

标签: python

我是一名学习程序员,我正在接受挑战,但我遇到的挑战是:

再次遍历项目中的每个项目。如果当前项目的索引0处的字符是字母“a”,则继续下一个字符。否则,打印出当前成员。 示例:[“abc”,“xyz”]将只打印“xyz”。

我的代码就是这样:

def loopy(items):
# Code goes here
if item == 'a':
    continue
elif:
    break
for item in times:
    print(item)
它不会通过。请帮助我,我被卡住了,不知道该怎么做。

2 个答案:

答案 0 :(得分:1)

RomamPerekhrest的评论是正确的,但我相信这可能是一个更好的学习程序员的地方。

# for all items in the list, get the single item
for item in items:
    # if the item at index 0 is not 'a'
    if item[0] != 'a':
        # print the item that doesn't start with 'a'
        print(item)

答案 1 :(得分:0)

您可以使用以下代码将项目从["abc", "xyz"]转换为['a', 'x']

items = list(map(lambda x:x[0], items))

然后,您可以检查循环中的每个项目,看它是否等于' a'。