python实现手机通讯录搜索功能

时间:2022-10-10 16:24:55

本文实例为大家分享了python通过输入联系人首字母查询联系人的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding:utf-8 -*-
# 练习2:
# 模拟手机通讯录搜索功能
class PERSON(object):
  def __init__(self):
    self.name_list = ['zhangsan', 'zhangsanfeng', 'zhaoliu', 'wangwu', 'wulong', 'wanda', 'lisi', 'liuyang','luoxiaoxiao', 'linmeimei', 'luhan', 'gaohairui', 'guojing', 'huangrong', 'hanhan']
    #声明空字典
    self.ss_dict = {}
  def total(self):
    #for循环遍历列表,取出每一个姓名
    for name in self.name_list:
      #取出首字母
      s =name[0]
      #判断字典中是否有s这个key
      if s not in self.ss_dict:
        #如果没有,根据key将存放name的列表放入字典
        # s_list = []
        # s_list.append(name)
        # self.ss_dict[s] = s_list
        self.ss_dict[s] = [name]
      else:
        #如果有,根据key取出列表,将这个name添加到列表
        s_list = self.ss_dict[s]
        s_list.append(name)
    print(self.ss_dict)
  def add_person(self):
    name = input('请输入姓名:')
    if name not in self.name_list:
      self.name_list.append(name)
      s = name[0]
      if s not in self.ss_dict:
        self.ss_dict[s] = [name]
      else:
        s_list = self.ss_dict[s]
        s_list.append(name)
    else:
      print('已存在!')
  def query_person(self):
    s = input('请输入要查找联系人名字的首字母:')
    # s就是要查找的字典的key
    if s not in self.ss_dict:
      print('不存在')
    else:
      # 根据这个key取出字典中对应的值(列表)
      s_list = self.ss_dict[s]
      for index,name in enumerate(s_list):
        print('序号:%s 姓名:%s' % (index,name))
      # for x in range(0,len(s_list)):
      #   name = s_list[x]
      #   print('序号:%s 姓名:%s'%(x,name))
    return s
  def alter_person(self):
    s= self.query_person()
    index = int(input('请输入要修改联系人的序号:'))
    new_name = input('输入更改后的名字(%s):'%self.ss_dict[s][index])
    del self.ss_dict[s][index]
    s = new_name[0]
    if s not in self.ss_dict:
      self.ss_dict[s] = [new_name]
    else:
      s_list = self.ss_dict[s]
      s_list.append(new_name)
  def delete_person(self):
    while len(self.ss_dict) == '0':
      print('信息为空!')
    print('1.根据名字删除')
    print('2.全部删除')
    print('0.退出')
    num = int(input('输入序号:'))
    while num not in range(0,3 ):
      num = int(input('选项不存在,请重输:'))
    if num == 1:
      s = self.query_person()
      index = int(input('请输入要删除联系人的序号:'))
      rs = input('是否真的要删除,(y/n):')
      if rs == 'y':
        self.ss_dict[s].pop(index)
        print('删除成功')
      else:
        print('取消删除')
 
    elif num == 2:
      rs = input('是否真的要删除,(y/n):')
      if rs == 'y':
        self.ss_dict.clear()
        print('删除成功')
      else:
        print('取消删除')
    else:
      return
  def start(self):
    self.total()
    while 1:
      print('1.添加联系人')
      print('2.查询联系人')
      print('3.修改联系人')
      print('4.删除联系人')
      print('0.退出')
      num = int(input('输入序号:'))
      while num not in range(0,5):
        num = int(input('选项不存在,请重输:'))
      if num == 1:
        self.add_person()
      elif num == 2:
        self.query_person()
      elif num == 3:
        self.alter_person()
      elif num == 4:
        self.delete_person()
      else:
        break
 
p = PERSON()
p.start()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/huo_1214/article/details/79202642