如果我输入"apple Pie is Yummy"
我想要:['Pie','Yummy'] ['apple','is']
我得到:[] ['apple', 'Pie', 'is', 'Yummy']
。
如果我输入"Apple Pie is Yummy"
我想要:['Apple','Pie','Yummy'] ['is']
我得到:['Apple', 'Pie', 'is', 'Yummy'] []
它的行为就像我的条件运算符在 for 循环的第一次迭代期间只读取一次,然后额外的迭代不会评估条件。
str = input("Please enter a sentence: ")
chunks = str.split()
# create tuple for use with startswith string method
AtoZ = ('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z')
# create empty lists to hold data
list1 = []
list2 = []
for tidbit in chunks:
list1.append(tidbit) if (str.startswith(AtoZ)) else list2.append(tidbit)
print(list1)
print(list2)