简体   繁体   English

我怎样才能在屏幕上枚举 object

[英]How can i get enumerate object inside on screen

I want to write screen Capital letters and index numbers in word="WElCMMerC".For example [(0,W),(1,E),(3,C),(4,M),(5,M)...]我想在 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"))

You can loop over the result of enumerate , and keep only those which have an uppercase letter (using isupper to check for that), and return the list w , don't print inside the function:您可以遍历enumerate的结果,只保留那些有大写字母的(使用isupper来检查),并返回列表w ,不要在 function 内打印:

def cap(word):
    w = [i for i in enumerate(word) if i[1].isupper()]
    return w

print(cap("WElCMMerC"))

Output: Output:

[(0, 'W'), (1, 'E'), (3, 'C'), (4, 'M'), (5, 'M'), (8, 'C')]

You made a list of enumerate objects.您制作了一个enumerate对象列表。 Read the documentation: enumerate is an iterator, much like range .阅读文档: enumerate是一个迭代器,很像range Rather, you need to use the enumeration.相反,您需要使用枚举。

return [(idx, letter) 
        for idx, letter in enumerate(word)
            if letter.isupper()]

In English:用英语讲:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM