Python基础3

本文介绍了Python的基础知识,包括字典的创建、访问、修改和删除,集合的创建、比较、删除、交集、并集等操作,以及文件的读取方法。文章还提供了一个面试题示例,演示如何处理两个文件中相同IP地址的问题,并强调了使用with语句关闭文件的重要性。

dict字典

创建字典

字典由键(key)和对应值(value)成对组成。字典也被称作关联数组或哈希表。基本语法如下:

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

注意:

  • 每个键与值用冒号隔开(:),每对用逗号,每对用逗号分割,整体放在花括号中({})。
  • 键必须独一无二,但值则不必。
  • 值可以取任何数据类型。键必须不可变,可以用数字,字符串或元组充当,所以用列表就不行.
    dict = {['Name']: 'Zara', 'Age': 7} 
    print "dict['Name']: ", dict['Name']
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    dict = {['Name']: 'Zara', 'Age': 7} 
TypeError: list objects are unhashable

访问字典中的值

print(d['key']) 如果写了不存在的值会报错

print(d.get('key')) 如果写了不存在的值,返回None

print(d.get('key','not find ')) 如果找不到存在的值,返回 “not find”

print(d.keys())  #获取所有的key 值
print(d.values()) #获取所有的value 值
if  'key' in d:   #判断key是否存在
    print('key')
for k,v in d.items():   #遍历字典
    print(k,v)
无须转换成list 格式,保留了字典原有的特性

for k in dict:
    print(k,dict[k])
    print(k,dict.get(k))   取值速度快,性能好

修改字典

d['key'] = value  key 存在即修改,key不存在即新增
d.setdefault('key',value)   只能新增

删除字典元素

d.pop('key')  必须要传值,因为字典是无序的
d.popitem()  随机删除一个
del d['key'] 
d.clear()  清空字典

Python字典包含了以下内置函数:

cmp(dict1, dict2)  #比较两个字典元素。
len(dict)              #计算字典元素个数,即键的总数。
str(dict)              #输出字典可打印的字符串表示。
type(variable)     #返回输入的变量类型,如果变量是字典就返回字典类型。        

Python字典包含了以下内置方法:

radiansdict.clear()    #删除字典内所有元素
radiansdict.copy()    #返回一个字典的浅复制
radiansdict.fromkeys()    #创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值
radiansdict.get(key, default=None)    #返回指定键的值,如果值不在字典中返回default值
radiansdict.has_key(key)    #如果键在字典dict里返回true,否则返回false
radiansdict.items()    #以列表返回可遍历的(键, 值) 元组数组
radiansdict.keys()    #以列表返回一个字典所有的键
radiansdict.setdefault(key, default=None)    #和get()类似, 但如果键不已经存在于字典中,将会添加键并将值设为default
radiansdict.update(dict2)    #把字典dict2的键/值对更新到dict里
radiansdict.values()    #以列表返回字典中的所有值

set集合

集合(set)是一个无序的不重复元素序列。

可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。

创建

a=set('boy')
b=set(['y', 'b', 'o','o'])
c=set({"k1":'v1','k2':'v2'})
d={'k1','k2','k2'}
e={('k1', 'k2','k2')}
print(a,type(a))
print(b,type(b))
print(c,type(c))
print(d,type(d))
print(e,type(e))

OUTPUT:
{'o', 'b', 'y'} <class 'set'>
{'o', 'b', 'y'} <class 'set'>
{'k1', 'k2'} <class 'set'>
{'k1', 'k2'} <class 'set'>
{('k1', 'k2', 'k2')} <class 'set'>

比较

se = {11, 22, 33}
be = {22, 55}
temp1 = se.difference(be)        #找到se中存在,be中不存在的集合,返回新值
print(temp1)        #{33, 11}
print(se)           #{33, 11, 22}

temp2 = se.difference_update(be) #找到se中存在,be中不存在的集合,覆盖掉se
print(temp2)        #None
print(se)           #{33, 11},

删除

discard()、remove()、pop()

se = {11, 22, 33}
se.discard(11)
se.discard(44)  # 移除不存的元素不会报错
print(se)

se = {11, 22, 33}
se.remove(11)
se.remove(44)  # 移除不存的元素会报错
print(se)

se = {11, 22, 33}  # 移除末尾元素并把移除的元素赋给新值
temp = se.pop()
print(temp)  # 33
print(se) # {11, 22}

取交集

intersection()

se = {11, 22, 33}
be = {22, 55}

temp1 = se.intersection(be)             #取交集,赋给新值
print(temp1)  # 22
print(se)  # {11, 22, 33}

temp2 = se.intersection_update(be)      #取交集并更新自己
print(temp2)  # None
print(se)  # 22

判断

isdisjoint issebset issuperset

se = {11, 22, 33}
be = {22}

print(se.isdisjoint(be))        #False,没有相同项 返回true
print(se.issubset(be))          #False,判断se是否是be的子集合
print(se.issuperset(be))        #True, 判断se是否是be的父集合

合并

symmetric_difference_update

se = {11, 22, 33}
be = {22}

temp1 = se.symmetric_difference(be)  # 合并不同项,并赋新值
print(temp1)    #{33, 11}
print(se)       #{33, 11, 22}

temp2 = se.symmetric_difference_update(be)  # 合并不同项,并更新自己
print(temp2)          #None
print(se)             #{33, 11}

取并集

union()

se = {11, 22, 33}
be = {22,44,55}

temp=se.union(be)   #取并集,并赋新值
print(se)       #{33, 11, 22}
print(temp)     #{33, 22, 55, 11, 44}

更新

update()

se = {11, 22, 33}
be = {22,44,55}

se.update(be)  # 把se和be合并,得出的值覆盖se
print(se)
se.update([66, 77])  # 可增加迭代项
print(se)

file文件读取

读文件

打开一个文件用open()方法(open()返回一个文件对象,它是可迭代的):

f = open(‘test.txt’, ‘r’)
r表示是文本文件,rb是二进制文件。(这个mode参数默认值就是r)

如果文件不存在,open()函数就会抛出一个IOError的错误,并且给出错误码和详细的信息告诉你文件不存在:

f=open('test.txt', 'r')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'

文件使用完毕后必须关闭,因为文件对象会占用操作系统的资源,并且操作系统同一时间能打开的文件数量也是有限的

f.close()
由于文件读写时都有可能产生IOError,一旦出错,后面的f.close()就不会调用。所以,为了保证无论是否出错都能正确地关闭文件,我们可以使用try … finally来实现:

try:
    f = open('/path/to/file', 'r')
    print(f.read())
finally:
    if f:
        f.close()

但是每次都这么写实在太繁琐,所以,Python引入了with语句来自动帮我们调用close()方法:

with open('/path/to/file', 'r') as f:
    print(f.read())

python文件对象提供了三个“读”方法: read()、readline() 和 readlines()。每种方法可以接受一个变量以限制每次读取的数据量。

read() 每次读取整个文件,它通常用于将文件内容放到一个字符串变量中。如果文件大于可用内存,为了保险起见,可以反复调用read(size)方法,每次最多读取size个字节的内容。
readlines() 之间的差异是后者一次读取整个文件,象 .read() 一样。.readlines() 自动将文件内容分析成一个行的列表,该列表可以由 Python 的 for … in … 结构进行处理。
readline() 每次只读取一行,通常比readlines() 慢得多。仅当没有足够内存可以一次读取整个文件时,才应该使用 readline()。
注意:这三种方法是把每行末尾的’\n’也读进来了,它并不会默认的把’\n’去掉,需要我们手动去掉。

In[2]: with open('test1.txt', 'r') as f1:
    list1 = f1.readlines()
In[3]: list1
Out[3]: ['111\n', '222\n', '333\n', '444\n', '555\n', '666\n']    
去掉'\n'
In[4]: with open('test1.txt', 'r') as f1:
    list1 = f1.readlines()
for i in range(0, len(list1)):
    list1[i] = list1[i].rstrip('\n')
In[5]: list1
Out[5]: ['111', '222', '333', '444', '555', '666']

对于read()和readline()也是把’\n’读入了,但是print的时候可以正常显示(因为print里的’\n’被认为是换行的意思)

In[7]: with open('test1.txt', 'r') as f1:
    list1 = f1.read()
In[8]: list1
Out[8]: '111\n222\n333\n444\n555\n666\n'
In[9]: print(list1)
111
222
333
444
555
666

In[10]: with open('test1.txt', 'r') as f1:
    list1 = f1.readline()
In[11]: list1
Out[11]: '111\n'
In[12]: print(list1)
111

一个python面试题的例子:

有两个文件,每个都有很多行ip地址,求出两个文件中相同的ip地址:

# coding:utf-8
import bisect

with open('test1.txt', 'r') as f1:
    list1 = f1.readlines()
for i in range(0, len(list1)):
    list1[i] = list1[i].strip('\n')
with open('test2.txt', 'r') as f2:
    list2 = f2.readlines()
for i in range(0, len(list2)):
    list2[i] = list2[i].strip('\n')

list2.sort()
length_2 = len(list2)
same_data = []
for i in list1:
    pos = bisect.bisect_left(list2, i)
    if pos < len(list2) and list2[pos] == i:
        same_data.append(i)
same_data = list(set(same_data))
print(same_data)

要点就是:(1)用with (2)处理行末的’\n’ (3)使用二分查找提高算法效率。(4)使用set快速去重。

写文件
写文件和读文件是一样的,唯一区别是调用open()函数时,传入标识符’w’或者’wb’表示写文本文件或写二进制文件:

 f = open('test.txt', 'w') # 若是'wb'就表示写二进制文件
 f.write('Hello, world!')
 f.close()

注意:'w’这个模式是酱紫:如果没有这个文件,就创建一个;如果有,那么就会先把原文件的内容清空再写入新的东西。所以若不想清空原来的内容而是直接在后面追加新的内容,就用’a’这个模式。

我们可以反复调用write()来写入文件,但是务必要调用f.close()来关闭文件。当我们写文件时,操作系统往往不会立刻把数据写入磁盘,而是放到内存缓存起来,空闲的时候再慢慢写入。只有调用close()方法时,操作系统才保证把没有写入的数据全部写入磁盘。忘记调用close()的后果是数据可能只写了一部分到磁盘,剩下的丢失了。所以,还是用with语句来得保险:

with open('test.txt', 'w') as f:
    f.write('Hello, world!')
python文件对象提供了两个“写”方法: write() 和 writelines()

write()方法和read()、readline()方法对应,是将字符串写入到文件中。
writelines()方法和readlines()方法对应,也是针对列表的操作。它接收一个字符串列表作为参数,将他们写入到文件中,换行符不会自动的加入,因此,需要显式的加入换行符。

f1 = open('test1.txt', 'w')
f1.writelines(["1", "2", "3"])
#    此时test1.txt的内容为:123

f1 = open('test1.txt', 'w')
f1.writelines(["1\n", "2\n", "3\n"])
#    此时test1.txt的内容为:
#    1
#    2        
#    3

关于open()的mode参数:

'r':读

'w':写

'a':追加

'r+' == r+w(可读可写,文件若不存在就报错(IOError)'w+' == w+r(可读可写,文件若不存在就创建)

'a+' ==a+r(可追加可写,文件若不存在就创建)

对应的,如果是二进制文件,就都加一个b就好啦:

'rb'  'wb'  'ab'  'rb+'  'wb+'  'ab+'

题目

读取一个文件,将文件转换为字典,key值为学习项目,value值为一个负责人列表,判断字典中是否有负责人负责多个项目

d = {}
with open('homework.txt', mode='r', encoding='utf8') as f:
    while True:
        data = f.readline()
        if data is "":
            break
        else:
            row = list(map(lambda x:x.strip(), data.split(" ")))
            for i in range(len(row)):
                if i == 0:
                    d[row[i]] = []
                else:
                    d[row[0]].append(row[i])
print(d)
result = {}
for key, value in d.items():
    for val in value:
        if result.get(val) is None:
            result[val] = 1
        else:
            result[val] += 1
for key, value in result.items():
    if value > 1:
        print(key)
print(result)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值