Python-3.3-组织列表- reading notes

3.3.1方法sort()  对列表进行永久性排序

#!/usr/bin/python
# -*- coding: latin-1 -*-
import os, sys
#字母顺序,修改是永久性的
cars = ['bmw','audi','toyota','subaru']
cars.sort()
print(cars)

output:
['audi', 'bmw', 'subaru', 'toyota']

#字母反序,修改是永久性的
cars.sort(reverse=True)
print(cars)

output:
['toyota', 'subaru', 'bmw', 'audi']


#临时排序,可按照特定顺序排列,同时不影响原始排列顺序
cars = ['bmw','audi','toyota','subaru']
print("Here is the original list:")
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
#cars.sorted(reverse=True)
#print(cars)
print("\nHere is the original list again:")
print(cars)

output:
Here is the original list:
['bmw', 'audi', 'toyota', 'subaru']

Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyota']

Here is the original list again:
['bmw', 'audi', 'toyota', 'subaru']


#倒着打印列表,反转列表元素排列顺序
cars = ['bmw','audi','toyota','subaru']
print(cars)
cars.reverse()
print(cars)
#reverse 永久性修改元素排列顺序,再次调用reverse可恢复至原始顺序
print("\nThis is the reversed list:")
print(cars)
cars.reverse()
print("\nThis is the reversed twice list:")
print(cars)

output:
['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']

This is the reversed list:
['subaru', 'toyota', 'audi', 'bmw']

This is the reversed twice list:
['bmw', 'audi', 'toyota', 'subaru']

#确定列表的长度,计数时从1开始
cars = ['bmw','audi','toyota','subaru']
print(len(cars))

output:
4

quiz_3-8

#!/usr/bin/python
# -*- coding:latin-1 -*-
import os, sys
city = ['Beijing','New York','London','Paris','Massachusetts']
print(city)

output:
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']

#sorted方法临时更改顺序
print("\nThis is the original list:")
print(city)
print("\nThis is the sorted list:")
print(sorted(city))
print("\nThis is the original list again: ")
print(city)

output:
This is the original list:
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']

This is the sorted list:
['Beijing', 'London', 'Massachusetts', 'New York', 'Paris']

This is the original list again:
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']


#sorted()方法,字母顺序相反的顺序输出列表
print(sorted(city,reverse=True))
print(city)

output:
['Paris', 'New York', 'Massachusetts', 'London', 'Beijing']
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']

#reverse反转列表元素排列顺序
print("\nThis is the original list:")
print(city)
city.reverse()
print("\nThis is the reversed once list:")
print(city)
print("\nThis is the reversed twice list:")
city.reverse()
print(city)

output:
This is the original list:
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']

This is the reversed once list:
['Massachusetts', 'Paris', 'London', 'New York', 'Beijing']

This is the reversed twice list:
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']



#sort方法,字母顺序与字母顺序相反输出列表
print("\nThis is the original list:")
print(city)
city.sort()
print("\nThis is the sort list:")
print(city)
city.sort(reverse=True)
print("\nThis is the sort reverse true list:")
print(city)

output:
This is the original list:
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']

This is the sort list:
['Beijing', 'London', 'Massachusetts', 'New York', 'Paris']

This is the sort reverse true list:
['Paris', 'New York', 'Massachusetts', 'London', 'Beijing']


#晚餐嘉宾
guest = ['Washington','Adams','Jefferson','Madison','Bush','Obama']
message = " persons invited by me will enjoy the dinner with us students. "
num = len(guest)
print(str(num)+message)

output:
6 persons invited by me will enjoy the dinner with us students.




 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值