亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在列表中顯示元音的數量?

如何在列表中顯示元音的數量?

森欄 2021-12-17 14:44:15
首先,我需要編寫一個程序,按照它們距太陽的位置降序顯示列表中行星的名稱。然后,我應該編寫一個程序,按照行星名稱中的元音數量升序顯示列表行星中的行星名稱。我已經能夠完成第一部分。然而,我不能第二部分。Planets = [("Mercury", 75, 1), ("Venus", 460, 2), ("Mars", 140, 4),           ("Earth", 510, 3), ("Jupiter", 62000, 5), ("Neptune", 7640, 8),           ("Saturn", 42700, 6), ("Uranus", 8100, 7)]def main():    Planets.sort(key=Sort_By_Position,reverse=True)    print("The names of the planets in descending order by their position from the Sun: ")    for i in Planets:        print (i[0])    print(" ")    print("Planets in ascending order by the number of vowels in planet name: ")    Planets.sort(key=vowel_count)    for i in Planets:        print(i[0])def Sort_By_Position(Planets):    return Planets[-1]def vowel_count():main()我希望該程序按行星名稱中的元音數量向我顯示行星的升序。
查看完整描述

3 回答

?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

您提到您能夠完成第一部分,因此您基本上只詢問第二部分。以下示例應該可以幫助您指明正確的方向:


>>> planets = ['Mercury','Venus','Earth','Mars','Neptune','Jupiter','Saturn','Uranus']

>>> vowels = ('a','e','i','o','u')

>>> name_counts = []

>>> for name in planets:

...     count = sum([1 for letter in name if letter.lower() in vowels])

...     name_counts.append((name,count))

... 

>>> print(sorted(name_counts, key=lambda x: x[1]))

[('Mars', 1), ('Mercury', 2), ('Venus', 2), ('Earth', 2), ('Saturn', 2), ('Neptune', 3), ('Jupiter', 3), ('Uranus', 3)]



查看完整回答
反對 回復 2021-12-17
?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

這是解決方案


Planets = [("Mercury", 75, 1), ("Venus", 460, 2), ("Mars", 140, 4), ("Earth", 510, 3), ("Jupiter", 62000, 5), ("Neptune", 7640, 8), ("Saturn", 42700, 6), ("Uranus", 8100, 7)]


# decending order in order

new_pl=Planets.copy()

new_pl.sort(key=lambda x:x[2], reverse=True) # sorting on position value


print(new_pl)


"""

output

[('Neptune', 7640, 8), ('Uranus', 8100, 7), ('Saturn', 42700, 6), ('Jupiter', 62000, 5), ('Mars', 140, 4), ('Earth', 510, 3), ('Venus', 460, 2), ('Mercury', 75, 1)]

"""



# in no of vowels present

vowel = ['a','e','i','o','u']


# in vowels

def count(name):

    vowel = ['a','e','i','o','u']

    val=0

    for i in name.lower():

        if i in vowel:

            val+=1

    return val


new_pl_2=Planets.copy()


new_pl_2.sort(key=lambda x:count(x[0])) #sorting on count of vowels

print(new_pl_2)


"""

output


[('Mars', 140, 4), ('Mercury', 75, 1), ('Venus', 460, 2), ('Earth', 510, 3), ('Saturn', 42700, 6), ('Jupiter', 62000, 5), ('Neptune', 7640, 8), ('Uranus', 8100, 7)]

"""


查看完整回答
反對 回復 2021-12-17
?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

您可以使用列表理解在一行中完成此操作。

def vowel_count(elem): 
return len([x for x in elem[0] if x in ('a', 'e', 'i', 'o', 'u')])

elem[0]包含您正在迭代的行星的名稱。當您遍歷字符串 ( x in elem[0]) 時,它將遍歷該字符串中的每個單獨字符。例如,'Earth'變成['E', 'a', 'r', 't', 'h']

從那里,我們可以簡單地過濾列表推導式以僅包含元音 ( if x.lower() in ('a', 'e', 'i', 'o', 'u')) 并返回推導式的長度,并將其反饋給sort方法。


查看完整回答
反對 回復 2021-12-17
  • 3 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號