我正试图让名字从z-a下降。 我正在尝试按反向字母顺序对列表进行排序,然后遍历整个列表以打印每个名人的姓名
def main():
print('Celebrities known by one name:')
drake = ['Drake']
BE = ['Beyonce']
RI = ['Rihanna']
BO = ['Bono']
print('Drake','Beyonce','Rihanna', 'Bono')
a = input('Enter another one name celebrity ')
b = input('Enter another one name celebrity ')
c = input('Enter another one name celebrity ')
d = [a,b,c,drake,BE,RI,BO]
f = reversed(d)
print('Celebrities in Reverse Alpha Order')
for d in f:
print(d)
main()
到目前为止,我得到的是这个
['Bono']
['Rihanna']
['Beyonce']
['Drake']
Eminem
Adele
Pink
它假设看起来像
Celebrities known by one name:
Drake Beyonce Rihanna Bono
Enter another one name celebrity Adele
Enter another one name celebrity Eminem
Enter another one name celebrity Pink
Celebrities in Reverse Alpha Order
Rihanna
Pink
Eminem
Drake
Bono
Beyonce
Adele
答案 0 :(得分:0)
pythons内置排序函数有一个reverse
参数,请参阅https://wiki.python.org/moin/HowTo/Sorting:
升序和降序
list.sort()
和sorted()
都接受带有的反向参数 布尔值。这用于标记降序排序。例如,到 以反向年龄顺序获取学生数据:
sorted(student_tuples, key=itemgetter(2), reverse=True) [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
sorted(student_objects, key=attrgetter('age'), reverse=True) [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
Python reverse alphabetical order中的是代码示例
答案 1 :(得分:0)
你可以试试这个:
l = ['Bono', 'Rihanna', 'Beyonce', 'Drake', 'eminem', 'adele', 'pink']
import string
final_list = sorted(l, key=lambda x:26 - string.ascii_lowercase.index(x[0].lower()))
输出:
['Rihanna', 'pink', 'eminem', 'Drake', 'Bono', 'Beyonce', 'adele']
答案 2 :(得分:0)
您可以按doc
用户<?php
$videoId = "EJOnwF8mgXc";
parse_str(file_get_contents("http://youtube.com/get_video_info?video_id=" .
$videoId), $info);
$streams = $info['url_encoded_fmt_stream_map'];
$streams = explode(',', $streams);
foreach ($streams as $stream) {
parse_str($stream, $data);
if($data['itag'] == 18){
echo $data['url'];
}
}
sorted..reverse
答案 3 :(得分:0)
我们可以使用:
names = sorted(names, key=str.lower,reverse=True)
完整示例:
def main():
print('Celebrities known by one name:')
names = ['Drake','Beyonce','Rihanna','Bono']
print(" ".join(names))
for _ in range(3):
names.append(input('Enter another one name celebrity '))
names = sorted(names, key=str.lower,reverse=True)
print('Celebrities in Reverse Alpha Order')
for name in names:
print(name)
main()
答案 4 :(得分:-1)
def names(name_list):
name_list.sort()
**name_list.reverse()**
return(name_list)