python备忘

一些小技巧,方便做题的时候用。网上的博客讲得实在太烂了。

懒得分开了,贴以前写的代码到时候复制粘贴好了。

 

 

遍历:

class Solution:
    def twoSum(self, nums, target):
        
        d=dict()
        result=list()
        
        for i in range(len(nums)):
            d[nums[i]]=i
            
        for i in range(len(nums)):
            temp = target-nums[i];
            if( (temp in d)and(d[temp]!=i)):
                result.append(i)
                result.append(d[temp])
                return result

        

 

 

负数除法:

 b=-1
 b/10 = -1

地板除
floor(x) 返回的是 小于等于 x 的最大整数
ceil(x) 返回的是 大于等于 x 的最小整数
而 int(x) 仅仅是将小数部分去掉,留下整数部分

答案是-0.1,-1<-0.1<0,所以地板除结果为-1

 

seq = ('name', 'age', 'sex')

dict = dict.fromkeys(seq)
print ("New Dictionary : %s" %  str(dict))

dict = dict.fromkeys(seq, 10)
print ("New Dictionary : %s" %  str(dict))
Python

当运行上面的程序,它产生以下结果 -

New Dictionary : {'age': None, 'name': None, 'sex': None}
New Dictionary : {'age': 10, 'name': 10, 'sex': 10}

 

posted @ 2017-06-18 15:31  wzb的QQ空间  阅读(179)  评论(0编辑  收藏  举报