如果我輸入"apple Pie is Yummy"
我想:['Pie','Yummy'] ['apple','is']
的Python for循環遍歷列表
我得到:[] ['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)
謝謝你,我錯過了明顯。 – ptay 2013-04-04 16:53:51