[英]How can i get enumerate object inside on screen
我想在 word="WElCMMerc" 中寫屏幕大寫字母和索引號。例如 [(0,W),(1,E),(3,C),(4,M),(5,M)。 ..]
def cap(word):
w=list(enumerate(i) for i in word if i!=i.lower())
print (w)
print(cap("WElCMMerC"))
您可以遍歷enumerate
的結果,只保留那些有大寫字母的(使用isupper
來檢查),並返回列表w
,不要在 function 內打印:
def cap(word):
w = [i for i in enumerate(word) if i[1].isupper()]
return w
print(cap("WElCMMerC"))
Output:
[(0, 'W'), (1, 'E'), (3, 'C'), (4, 'M'), (5, 'M'), (8, 'C')]
您制作了一個enumerate
對象列表。 閱讀文檔: enumerate
是一個迭代器,很像range
。 相反,您需要使用枚舉。
return [(idx, letter)
for idx, letter in enumerate(word)
if letter.isupper()]
用英語講:
Return the pair of index and letter
for each index, letter pair in the word
but only when the letter is upper-case.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.