Python基础操作- Updating...


Python四大金刚:strings, set, dictionary, list - 增删查改

STRINGS

  • str.split(str=’’, num=string.count(str))
  • str.replace(old, new, count )
  • str.isdigit()
# method1
'animals'.isdigit()
# method2
str.isdigit('animals')

输出为True or False
注意:
1.使用的是str,而不是string
2.由于负数的符号-为string,应该去掉符号再操作

int('-12345') => int("12345")
  • print()
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

sep: seperated by ’ ’
end: the things prints out at end, ‘\n’ is default
file: the position to print out, may be a file name
(not sure what happens when file does not exist, will it create one automatically?)

DICTIONARY

SET

LIST

  • .join()

返回值是一个新的string

str.join(sequence)

注:元素为数字的时候无法join,需转为字符
进阶:与列表解析组合使用

' x '.join(str(n) for n in function(var))
  • list.reverse

注意:.reverse反转的是原始列表,返回值为None,不能直接使用返回值,目的是节省空间,.sort同理

  • list.pop()
    pop出list最后一项并且返回

BASIC OPERATIONS

  • max()
  • sum()
  • min()

LOGIC

  • if else

… if … else …

result = 'valid' if x == 1 else result = 'invalid'

if: … else: …
… if else …

  • For … in …
if x == 1:
	result = 'valid'
else:
	result = 'invalid'
  • enumerate()
enumerate(sequence, [start=0])

sequence – 一个序列、迭代器或其他支持迭代对象。
start – 下标起始位置。

list(enumerate(seasons, start=1))
################## output #####################

[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
  • reduce(function, iterable[, initializer])
def add(x, y) :      
    return x + y
sum1 = reduce(add, [1,2,3,4,5])
sum2 = reduce(lambda x, y: x+y, [1,2,3,4,5]) 
print(sum1)
print(sum2)
################################################
# output
15
15
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值