python自学笔记之10数,随机数,字典

产生像matlab中[1:10],有序列表,用range(1,11)
help(range)

生成随机数:random模块

random.random()用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。如果a > b,则生成随机数
print random.uniform(10, 20)
print random.uniform(20, 10)

random.randint用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,Python生成随机数
print random.randint(12, 20) #生成的随机数n: 12 <= n <= 20
print random.randint(20, 20) #结果永远是20

下限必须小于上限。

随机整数:

import random
random.randint(0,99)
21

随机选取0到100间的偶数:

import random
random.randrange(0, 101, 2)
42

随机浮点数:

import random
random.random()
0.85415370477785668
random.uniform(1, 10)
5.4221167969800881

随机字符:

import random
random.choice(‘abcdefg&#%^*f’)
‘d’

多个字符中选取特定数量的字符:

import random
random.sample(‘abcdefghij’,3)
[‘a’, ‘d’, ‘b’]

多个字符中选取特定数量的字符组成新字符串:

import random
import string
string.join(random.sample([‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’j’], 3)).r
eplace(” “,”“)
‘fih’

随机选取字符串:

import random
random.choice ( [‘apple’, ‘pear’, ‘peach’, ‘orange’, ‘lemon’] )
‘lemon’

洗牌:

import random
items = [1, 2, 3, 4, 5, 6]
random.shuffle(items)
items
[3, 2, 5, 6, 4, 1]

python中列表全部赋0 :[0]*N

raw_input与input的区别
当输入为纯数字时
• input返回的是数值类型,如int,float
• raw_inpout返回的是字符串类型,string类型
当输入字符串时, input会计算在字符串中的数字表达式,而raw_input不会。
如输入 “57 + 3”:
• input会得到整数60
• raw_input会得到字符串”57 + 3”

input 使用居多

将ASCII字符转换为对应的数值即‘a’–>65,使用ord函数,ord(‘a’)反正,使用chr函数,将数值转换为对应的ASCII字符,chr(65)

python中的dict是否可以使用输入key的方式获取key对应的值?
name = d[a] #name = ‘su’
name = d.get(a) #name = ‘su’

python3.0以上,print函数应为print(),不存在dict.iteritems()这个函数。
在python中写中文注释会报错,这时只要在头部加上# coding=gbk即可

dict = {“a” : “apple”, “b” : “banana”, “g” : “grape”, “o” : “orange”}
dict[“w”] = “watermelon”
del(dict[“a”])
dict[“g”] = “grapefruit”
print dict.pop(“b”)
print dict
dict.clear()
print dict
#字典的遍历
dict = {“a” : “apple”, “b” : “banana”, “g” : “grape”, “o” : “orange”}
for k in dict:
print “dict[%s] =” % k,dict[k]
#字典items()的使用
dict = {“a” : “apple”, “b” : “banana”, “c” : “grape”, “d” : “orange”}
#每个元素是一个key和value组成的元组,以列表的方式输出
print dict.items()
#调用items()实现字典的遍历
dict = {“a” : “apple”, “b” : “banana”, “g” : “grape”, “o” : “orange”}
for (k, v) in dict.items():
print “dict[%s] =” % k, v
#调用iteritems()实现字典的遍历
dict = {“a” : “apple”, “b” : “banana”, “c” : “grape”, “d” : “orange”}
print dict.iteritems()
for k, v in dict.iteritems():
print “dict[%s] =” % k, v
for (k, v) in zip(dict.iterkeys(), dict.itervalues()):
print “dict[%s] =” % k, v

#使用列表、字典作为字典的值
dict = {“a” : (“apple”,), “bo” : {“b” : “banana”, “o” : “orange”}, “g” : [“grape”,”grapefruit”]}
print dict[“a”]
print dict[“a”][0]
print dict[“bo”]
print dict[“bo”][“o”]
print dict[“g”]
print dict[“g”][1]

dict = {“a” : “apple”, “b” : “banana”, “c” : “grape”, “d” : “orange”}
#输出key的列表
print dict.keys()
#输出value的列表
print dict.values()
#每个元素是一个key和value组成的元组,以列表的方式输出
print dict.items()
dict = {“a” : “apple”, “b” : “banana”, “c” : “grape”, “d” : “orange”}
it = dict.iteritems()
print it
#字典中元素的获取方法
dict = {“a” : “apple”, “b” : “banana”, “c” : “grape”, “d” : “orange”}
print dict
print dict.get(“c”, “apple”)
print dict.get(“e”, “apple”)
#get()的等价语句
D = {“key1” : “value1”, “key2” : “value2”}
if “key1” in D:
print D[“key1”]
else:
print “None”
#字典的更新
dict = {“a” : “apple”, “b” : “banana”}
print dict
dict2 = {“c” : “grape”, “d” : “orange”}
dict.update(dict2)
print dict
#udpate()的等价语句
D = {“key1” : “value1”, “key2” : “value2”}
E = {“key3” : “value3”, “key4” : “value4”}
for k in E:
D[k] = E[k]
print D
#字典E中含有字典D中的key
D = {“key1” : “value1”, “key2” : “value2”}
E = {“key2” : “value3”, “key4” : “value4”}
for k in E:
D[k] = E[k]
print D
#设置默认值
dict = {}
dict.setdefault(“a”)
print dict
dict[“a”] = “apple”
dict.setdefault(“a”,”default”)
print dict
#调用sorted()排序
dict = {“a” : “apple”, “b” : “grape”, “c” : “orange”, “d” : “banana”}
print dict
#按照key排序
print sorted(dict.items(), key=lambda d: d[0])
#按照value排序
print sorted(dict.items(), key=lambda d: d[1])
#字典的浅拷贝
dict = {“a” : “apple”, “b” : “grape”}
dict2 = {“c” : “orange”, “d” : “banana”}
dict2 = dict.copy()
print dict2

#字典的深拷贝
import copy
dict = {“a” : “apple”, “b” : {“g” : “grape”,”o” : “orange”}}
dict2 = copy.deepcopy(dict)
dict3 = copy.copy(dict)
dict2[“b”][“g”] = “orange”
print dict
dict3[“b”][“g”] = “orange”
print dict

常用方法

Python代码
d = {“name”:”nico”, “age”:23}
d[“name”] = “aaaa”
d[“address”] = “abcdefg….”
print d #{‘age’: 23, ‘name’: ‘aaaa’, ‘address’: ‘abcdefg….’}

获取dict值
Python代码
print d[“name”] #nico
print d.get(“name”) #nico

如果key不在dict中,返回default,没有为None
Python代码
print d.get(“namex”, “aaa”) #aaa
print d.get(“namex”) #None

排序sorted()
Python代码
d = {“name”:”nico”, “age”:23}
for key in sorted(d):
print “key=%s, value=%s” % (key, d[key])
#key=age, value=23
#key=name, value=nico

删除del
Python代码
d = {“name”:”nico”, “age”:23}
Python代码
del d[“name”]
#如果key不在dict中,抛出KeyError
del d[“names”]
Python代码
Traceback (most recent call last):
File “F:\workspace\project\pydev\src\ddd\ddddd.py”, line 64, in
del d[“names”]
KeyError: ‘names’

清空clear()
Python代码
d = {“name”:”nico”, “age”:23}
d.clear()
print d #{}

copy()
Python代码
d1 = d.copy() #{‘age’: 23, ‘name’: ‘nico’}
#使用返回view对象
d2 = d1.viewitems() #dict_items([(‘age’, 23), (‘name’, ‘nico’)])
#修改字典d1,新增元素
d1[“cc”] = “aaaaaa”
print d2
#dict_items([(‘cc’, ‘aaaaaa’), (‘age’, 23), (‘name’, ‘nico’)])

pop(key[, default])
如果key在dict中,返回,不在返回default
Python代码
#如果key在dict中,返回,不在返回default
print d.pop(“name”, “niccco”) #nico
print d.pop(“namezzz”, “niccco”) #niccco
#key不在dict中,且default值也没有,抛出KeyError
print d.pop(“namezzz”) #此处抛出KeyError

popitem()
删除并返回dict中任意的一个(key,value)队,如果字典为空会抛出KeyError
Python代码
d = {“name”:”nico”, “age”:23}
print d.popitem() #(‘age’, 23)
print d.popitem() #(‘name’, ‘nico’)
#此时字典d已为空
print d.popitem() #此处会抛出KeyError

update([other])
将字典other中的元素加到dict中,key重复时将用other中的值覆盖
Python代码
d = {“name”:”nico”, “age”:23}
d2 = {“name”:”jack”, “abcd”:123}
d.update(d2)
print d #{‘abcd’: 123, ‘age’: 23, ‘name’: ‘jack’}
鸣谢:
http://www.cnblogs.com/yangyongzhi/archive/2012/09/17/2688326.html

解决安装包库的问题:
1. 直接下载.whl文件,window
下载地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/然后在shell中:pip install XXX.whl举例如下安装opencv,我的python是3,.5的,window 64位,那么就下载opencv_python-3.1.0-cp35-cp35m-win_amd64.whl注意其中cp35就是指的3.5版本的python,然后 pip install E:\Anaconda3\opencv_python-3.1.0-cp35-cp35m-win_amd64.whl(.whl前面是我的wheel文件存放路径)
2. 用conda install library,鸣谢:http://blog.csdn.net/robertchenguangzhi/article/details/49103421
3. 使用国内镜像:http://e.pypi.python.org/simple
4. 安装不上PIL尝试:pip install PIL –allow-external PIL –allow-unverified PIL

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Z_shsf

来包瓜子嘛,谢谢客官~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值