2019/9/2-2019/9/5 Python基础学习之内置数据结构

2019/9/2
1. eval() 函数
eval() 函数用来执行一个字符串表达式,并返回表达式的值。
eval(expression[, globals[, locals]])

x = 7
print eval('3 * x')

2.字符串基本操作
连接、复制、字符串判断

if 'h' in 'hello':
    print 'hello '*2 + 'wang'

索引

a = 'hello world'
print a[1:5:2]
print a[-1:-3:-1]

输出为:
el
dl
3.切割split(sep =None)

line = '1,2,3,4,5'
numbers = line.split(',')
print numbers

输出为:[‘1’, ‘2’, ‘3’, ‘4’, ‘5’]

2019/9/3
1.截取strip

b = '0000aaa00000'
print b.strip('0')

输出:aaa
2.连接join
Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

str = '-'
seq = ("a", "b", "c")
print str.join(seq)

3.隐藏电话号码中间四位

number = raw_input()
print (number[:3]+'****'+number[-4:])

输入:13512212112
输出:135****2112
4.Python 2.7 raw_input和input区别
raw_input()不管是输数字还是字符串,结果都会以字符串的形式展现出来
input()只能接收"数字"的输入,在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )。

a = input()
print type(a)

输入:123
输出:<type ‘int’>
输入:aaa
输出:NameError: name ‘aaa’ is not defined

a = raw_input()
print type(a)

输入:123
输出:<type ‘str’>
5.format

print "{:.2f}".format(3.1415926)
print "{1} {0}".format("hello", "world")
print "名字{name},年龄{age}".format(name='wang',age='18')
print "名字{0},年龄{1}".format('wang','18')
people = {"name": "wang", "age": "18"}
print "名字{name},年龄{age}".format(**people)

2019/9/4
1.dtype不是Python内置的数据对象类型

import numpy as np
a = 10
print type(a)
arr = np.arange(10)
print arr.dtype

输出:
<type ‘int’>
int32
2.使用dtype()函数构造数据类型

print type('i4')
print type(np.dtype('i4'))

输出:
<type ‘str’>
<type ‘numpy.dtype’>
3.组合类型的使用

student = np.dtype([('name', 'S20'), ('age', 'i1'), ('marks', 'f4')])
a = np.array([('wang', 12, 60), ('li', 13, 63)], dtype=student)
print a
print a['name']
print a['age']
print a['marks']

输出:
[(‘wang’, 12, 60.0) (‘li’, 13, 63.0)]
[‘wang’ ‘li’]
[12 13]
[ 60. 63.]
4.使用dtype的astype()方法转换数据类型

arr = np.arange(10)
print arr
print arr.dtype
farr = arr.astype(np.float64)
print farr.dtype
print farr

输出:
[0 1 2 3 4 5 6 7 8 9]
int32
float64
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
5.通过list()函数可以将一个可迭代对象强制转换为列表

print list()
print list("china")

输出:
[]
[‘c’, ‘h’, ‘i’, ‘n’, ‘a’]
2019/9/5
1.列表推导式生成

s = "123abc456def"
s2 = [c for c in s if c.isdigit()]
print s2

输出:
[‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’]
2.列表增加元素append 列表删除元素remove

a = ['a', 'b', 'c']
a.append(5)
print a
a.remove('a')
print a

输出:
[‘a’, ‘b’, ‘c’, 5]
[‘b’, ‘c’, 5]
3.sort排序
sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。
list.sort(cmp=None, key=None, reverse=False)
cmp – 可选参数, 如果指定了该参数会使用该参数的方法进行排序。
key – 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
reverse – 排序规则,reverse = True 降序, reverse = False 升序(默认)。

#获取列表第二个元素
def takeSecond(elem):
    return elem[1]
#列表
random = [(2,3), (3,9), (2,1), (2, 2)]
#指定第二个元素排序,指定升序
random.sort(key = takeSecond, reverse=True)
#输出类别
print "排序列表:", random

输出:
排序列表: [(3, 9), (2, 3), (2, 2), (2, 1)]
4.sorted

random = [(2,3), (3,9), (2,1), (2, 2)]
random2 = sorted(random, reverse=True)
print "random2列表:", random2

输出:
random2列表: [(3, 9), (2, 3), (2, 2), (2, 1)]
5.元祖采用逗号和圆括号(可选)来表示

d = (2)
print type(d)
d = (2,)
print type(d)
d = 2, 3
print type(d)
d, c=2, 3
print type(d)

输出:
<type ‘int’>
<type ‘tuple’>
<type ‘tuple’>
<type ‘int’>
6.元组主要用于表达固定数据项,函数多返回值return,多变量同步赋值,循环遍历

def func(x):
    return x,x**3
print func(3)
a, b = 'dd', 'aa'
print a, b
a, b = (b, a)
print a, b
for x, y in ((1, 0), (2, 5), (3, 8)):
    print x, y

输出:
(3, 27)
dd aa
aa dd
1 0
2 5
3 8
7.集合可删除数据项

st2 = set('hello') - {'h'}
print st2

输出:
set([‘e’, ‘l’, ‘o’])
8.hash运算

print hash("hello")
s = list(st2)
print hash(s)

输出:
-1267296259
TypeError: unhashable type: ‘list’
字典中的键必须是可hash的,可变类型对象如列表和字典是不可hash的,所以不能作为字典的键
9.字典生成方法之推导式生成
示例:有10个学生,成绩在60~100之间。请筛选出成绩在90分以上的学生

import random
stuInfo = {'student' + str(i): random.randint(60, 100) for i in range(10)}
print stuInfo
stu = {name: score for name, score in stuInfo.items() if score > 90}
print stu

输出:
{‘student9’: 92, ‘student8’: 64, ‘student3’: 80, ‘student2’: 83, ‘student1’: 61, ‘student0’: 60, ‘student7’: 85, ‘student6’: 80, ‘student5’: 79, ‘student4’: 63}
{‘student9’: 92}
10.字典的遍历
for <变量名> in <字典名>/keys()/values()/items():语句块

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值