现在的位置: 首页 > 综合 > 正文

python学习之路二(字符串,字典,序列和元组)

2014年01月19日 ⁄ 综合 ⁄ 共 3053字 ⁄ 字号 评论关闭
# -*- coding: utf-8 -*
'''
Created on 2013-7-26

@author: lixingle
'''
#!/usr/bin/python
import math#导入数学函数
import codecs
print "hello"
print type (2)
#type 类型转换函数
print int('2')

print str(32)
#数学函数使用
print math
print math.log10(2)
print math.pi
print math.sqrt(2)#开方

#键盘输入
#myinput=raw_input('请输入你的姓名\n')
#print myinput

#字符串。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

fruit='banana'
print fruit[1]
length=len(fruit)
print length,'\n'
#遍历
for char in fruit:
    print char
#字符串切片
numberstr='12345'
print numberstr[:]#12345
print numberstr[:3]#123
print numberstr[3:]#45
numberstr='abcde'
print numberstr.upper()#ABCDE
index=numberstr.find('c')
print 'the index is :',index#the index is : 2
#在3,4中找
index=numberstr.find('c',3,4)
print 'the index is :',index
#文件读取。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
fin=open('G://aa.txt').read().decode("gbk")
#print fin

#定义一个函数。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
def ifAinB(aword,bword):
    for letter in aword:
        if letter not in bword:
            return False 
    return True                  

print ifAinB('123','12345')
#列表。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
charlist=['a','g','c','f','e']
charlist2=['a','g']
print charlist
charlist.sort()#排序
print charlist
charlist.extend(charlist2)#添加
print charlist
#删除元素 (四种方法)
charlist.pop(1) #要删除的下标
print '删除后: ',charlist#['a', 'e', 'f', 'g', 'a', 'g']

charlist.pop() #不传参数,默认删除最后一个
print '删除后: ',charlist# ['a', 'e', 'g', 'a']

del charlist[2:3]#删除第三个
print '3删除后: ',charlist# ['a', 'e', 'g', 'a']
charlist.remove('a') #找到第一个匹配结果即停止
print '4删除后: ',charlist# ['e', 'g', 'a']

#列表和字符串。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
stra='welcome to yantai univercity'
print stra
liststra=list(stra)
print liststra#['w', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 't', 'o', ' ', 'y', 'a', 'n', 't', 'a', 'i', ' ', 'u', 'n', 'i', 'v', 'e', 'r', 'c', 'i', 't', 'y']

t=stra.split()#分割单词
print t#['welcome', 'to', 'yantai', 'univercity']
stra='welcome-to-yantai-univercity'
t=stra.split('-')#分割单词
print t#['welcome', 'to', 'yantai', 'univercity']

#字典。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
mydict={1:'lele',2:'lixingle',3:'wildcat'}
print mydict#{1: 'lele', 2: 'lixingle', 3: 'wildcat'}
print mydict[2]#lixingle
mydict['4']='haha'
print '添加后:',mydict#添加后: {1: 'lele', 2: 'lixingle', 3: 'wildcat', '4': 'haha'}

print 2 in mydict#判断是否含键2:True
print 'lixingle' in mydict#:False   in只能判断键是否在其中
#查看值
values=mydict.values()
print 'lixingle' in values#True


stringstr='abcdacft'
#定义一个求字符串中字符和个数的函数
def thecharAndNumber(s):
    chardict=dict()#创建一个空的字典
    for lt in s:
        if lt not in chardict:
            chardict[lt]=1
        else:
            chardict[lt]+=1
    return chardict
#调用函数
print thecharAndNumber(stringstr)    #{'a': 2, 'c': 2, 'b': 1, 'd': 1, 'f': 1, 't': 1}    

#元组  是不可变的。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#元组一班用()
array='a',#创建一个元素的元组不要忘了最后的','否则不是元组
print array
arrayb=(1,2,3,5,6)
print arrayb
#使用tuple创建一个元祖
arrc=tuple('hello')
print arrc#('h', 'e', 'l', 'l', 'o')
print arrc[1]#e
print arrc[2:4]#('l', 'l')

arraye=('ll','ee','ss','dd','aa')
print arrayb
print arraye

a,b=arrayb,arraye

print '交换后'
print a,b#(1, 2, 3, 5, 6) ('ll', 'ee', 'ss', 'dd', 'aa')
#内建函数zip
print zip([1,2,3],'abc')#生成一个元祖的列表[(1, 'a'), (2, 'b'), (3, 'c')]

#字典和元组
dictll={1: 'lele', 2: 'lixingle', 3: 'wildcat', '4': 'haha'}
print dictll.items() #把字典转换为元祖:[(1, 'lele'), (2, 'lixingle'), (3, 'wildcat'), ('4', 'haha')]





















抱歉!评论已关闭.