python列表排序推导_Python 数据排序和列表迭代和列表推导应用

1.In-place sorting 原地排序

data=[6,4,5,2,3,1]

print ('before sort', data)

data.sort()

print ('after sort BIF:', data)

=========== RESTART: C:/Users/eric/Documents/Python/kelly/sort.py ===========

before sort [6, 4, 5, 2, 3, 1]

after sort BIF: [1, 2, 3, 4, 5, 6]

2. copied sorting 复制排序

test=[6,4,5,2,3,1]

print ('before sorted', test)

test2=sorted(test)

print ('after sorted BIF, test', test)

print ('after sorted BIF, test2',test2)

=========== RESTART: C:/Users/eric/Documents/Python/kelly/sort.py ===========

before sorted [6, 4, 5, 2, 3, 1]

after sorted BIF, test [6, 4, 5, 2, 3, 1]

after sorted BIF, test2 [1, 2, 3, 4, 5, 6]

3. use senitize func 列表迭代处理各个选手的列表数据,将清理过的值追加到适当新列表

def sanitize(time_string):

if '-' in time_string:

splitter = '-'

elif ':' in time_string:

splitter = ':'

else:

return (time_string)

(mins, secs)=time_string.split(splitter)

return(mins + '.' + secs)

with open ('james.txt') as jas: data = jas.readline()

james=data.strip().split(',')

with open('julie.txt') as jue: data=jue.readline()

julie=data.strip().split(',')

with open('mikey.txt') as miy: data=miy.readline()

mikey=data.strip().split(',')

with open('sarah.txt') as sah: data=sah.readline()

sarah=data.strip().split(',')

print ('before sort and clean data' ,james,julie,mikey,sarah)

clean_james=[]

clean_julie=[]

clean_mikey=[]

clean_sarah=[]

for each_t in james:

clean_james.append(sanitize(each_t))

for each_t in julie:

clean_julie.append(sanitize(each_t))

for each_t in mikey:

clean_mikey.append(sanitize(each_t))

for each_t in sarah:

clean_sarah.append(sanitize(each_t))

print('after clean and sorted james is :',sorted(clean_james))

print('after clean and sorted julie is :',sorted(clean_julie))

print('after clean and sorted mikey is :',sorted(clean_mikey))

print('after clean and sorted sarah is :',sorted(clean_sarah))

=========== RESTART: C:\Users\eric\Documents\Python\kelly\kelly.py ===========

before sort and clean data ['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22'] ['2.59', '2.11', '2:11', '2:23', '3-10', '2-23', '3:10', '3.21', '3-21'] ['2:22', '3.01', '3:01', '3.02', '3:02', '3.02', '3:22', '2.49', '2:38'] ['2:58', '2.58', '2:39', '2-25', '2-55', '2:54', '2.18', '2:55', '2:55']

after clean and sorted james is : ['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']

after clean and sorted julie is : ['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']

after clean and sorted mikey is : ['2.22', '2.38', '2.49', '3.01', '3.01', '3.02', '3.02', '3.02', '3.22']

after clean and sorted sarah is : ['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']

4.list comprehension 运用 “列表推导”减少代码,达到同样效果

def sanitize(time_string):

if '-' in time_string:

splitter = '-'

elif ':' in time_string:

splitter = ':'

else:

return (time_string)

(mins, secs)=time_string.split(splitter)

return(mins + '.' + secs)

with open ('james.txt') as jas: data = jas.readline()

james=data.strip().split(',')

with open('julie.txt') as jue: data=jue.readline()

julie=data.strip().split(',')

with open('mikey.txt') as miy: data=miy.readline()

mikey=data.strip().split(',')

with open('sarah.txt') as sah: data=sah.readline()

sarah=data.strip().split(',')

print ('before sort and clean data' ,james,julie,mikey,sarah)

clean_james=[sanitize(each_t) for each_t in james]

clean_julie=[sanitize(each_t) for each_t in julie]

clean_mikey=[sanitize(each_t) for each_t in mikey]

clean_sarah=[sanitize(each_t) for each_t in sarah]

print('after clean and sorted james is :',sorted(clean_james))

print('after clean and sorted julie is :',sorted(clean_julie))

print('after clean and sorted mikey is :',sorted(clean_mikey))

print('after clean and sorted sarah is :',sorted(clean_sarah))

>>>

=========== RESTART: C:\Users\eric\Documents\Python\kelly\kelly.py ===========

before sort and clean data ['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22'] ['2.59', '2.11', '2:11', '2:23', '3-10', '2-23', '3:10', '3.21', '3-21'] ['2:22', '3.01', '3:01', '3.02', '3:02', '3.02', '3:22', '2.49', '2:38'] ['2:58', '2.58', '2:39', '2-25', '2-55', '2:54', '2.18', '2:55', '2:55']

after clean and sorted james is : ['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']

after clean and sorted julie is : ['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']

after clean and sorted mikey is : ['2.22', '2.38', '2.49', '3.01', '3.01', '3.02', '3.02', '3.02', '3.22']

after clean and sorted sarah is : ['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值