python入门第一课练习题_python 基础练习第一课笔记

第1题max 取一组数的最大值>>> max([5,3,2,1])5>>> max(8,4,5)8列表排序 list.sort>>> a= [3, 6, 1.1, 4]>>> a.sort()>>> a[1.1, 3, 4, 6]>>> a[-1]6sort作用在原序列上。sort返回值为空...
摘要由CSDN通过智能技术生成

第1题

max 取一组数的最大值

>>> max([5,3,2,1])

5

>>> max(8,4,5)

8

列表排序 list.sort

>>> a= [3, 6, 1.1, 4]

>>> a.sort()

>>> a

[1.1, 3, 4, 6]

>>> a[-1]

6

sort作用在原序列上。

sort返回值为空。

列表排序 sorted()

It is not possible to sort a dict, only to get a representation of a dict that is sorted. Dicts are inherently orderless, but other types, such as lists and tuples, are not. So you need a sorted representation, which will be a list—probably a list of tuples.

For instance,

import operator

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}

# sort on values

sorted_x = sorted(x.items(), key=operator.itemgetter(1))

result

[(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]

sorted_x will be a list of tuples sorted by the second element in each tuple. dict(sorted_x) == x.

And for those wishing to sort on keys instead of values:

import operator

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}

# sort on keys

sorted_x = sorted(x.items(), key=operator.itemgetter(0))

result

[(0, 0), (1, 2), (2, 1), (3, 4), (4, 3)]

# sort a dict by value

for key, value in sorted(mydict.iteritems(), key=lambda (k,v): (v,k)):

print "%s: %s" % (key, value)

operator.itemgetter 的用法

>>> import operator

>>> operator.itemgetter(1)('ABCDEFG')

'B'

>>> operator.itemgetter(1,3,5)('ABCDEFG')

('B', 'D', 'F')

using itemgetter() to retrieve specific fields from a dict:

>>> from operator import itemgetter as itemgetter

>>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]

>>> getcount = itemgetter(1)

>>> map(getcount, inventory)

[3, 2, 5, 1]

>>> sorted(inventory, key=getcount)

[('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]

第2题

range 函数

>>> range(5)

[0, 1, 2, 3, 4]

>>> range(1,5)

[1, 2, 3, 4]

注意右边界的值

第3题

列表切片, 取最后n个数据: [-n: ]

>>> a = [1,2,3,4,5]

>>> a[-3:]

[3, 4, 5]

>>> a[-1:]

[5]

每隔n个取值:[::n]

#python3.6

>>> a = range(1,10)

>>> list(a)

[1, 2, 3, 4, 5, 6, 7, 8, 9]

每隔2个数取一个值

>>> list(a[::2])

[1, 3, 5, 7, 9]

每隔3个数取一个值

>>> list(a[::3])

[1, 4, 7]

第4题

math.log 计算对数

>>> import math

>>> math.log10(100)

2.0

>>> math.log(4,2)

2.0

自定义底

>>> math.log(2,1.2)

3.8017840169239308

>>> math.log(2,1.02)

35.0027887811465

>>> math.log(2,1.03)

23.449772250437736

第5题

找出10000之内的所有完数

for i in range(1,10000):

n=0

list5=[]

for j in range(1,i):

if i%j==0:

n+=j

list5.append(j)

if i==n:

print(i,list5)

1也是完数,但由于range(1,1)返回空列表 [ ] , 无法完成计算

修改如下:

for i in range(1,10000):

n=1

list5=[1]

for j in range(2, i/2+1):

if i%j==0:

n+=j

list5.append(j)

if i==n:

print(i,list5)

i = 1 时,不进入for循环,直接进入if分支判断

range(2, i/2+1) 使得内层for循环只需要判断2到i/2+1之间的数是否为其因子

运行结果

#

1=1

6=1 +2 +3

28=1 +2 +4 +7 +14

496=1 +2 +4 +8 +16 +31 +62 +124 +248

8128=1 +2 +4 +8 +16 +32 +64 +127 +254 +508 +1016 +2032 +4064

第6题

re.findall 的用法

>>>import re

>>> article = r'''you are a good student it is my honor to present it, habit, young, You're my'''

>>> re.findall('\w+', article)

['you', 'are', 'a', 'good', 'student', &#

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值