呼啦一阵风
您提到您能够完成第一部分,因此您基本上只询问第二部分。以下示例应该可以帮助您指明正确的方向:>>> 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)]
陪伴而非守候
这是解决方案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 ordernew_pl=Planets.copy()new_pl.sort(key=lambda x:x[2], reverse=True) # sorting on position valueprint(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 presentvowel = ['a','e','i','o','u']# in vowelsdef count(name): vowel = ['a','e','i','o','u'] val=0 for i in name.lower(): if i in vowel: val+=1 return valnew_pl_2=Planets.copy()new_pl_2.sort(key=lambda x:count(x[0])) #sorting on count of vowelsprint(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)]"""