python(list,tuple,set,dict)

前言

list

list(用法)

列表,數據成員可以是多種類型,也可嵌套列表
ipython示例:

z = [12,13,14]
In [1]: name1 = 'tom'                                                        

In [2]: name2 = 'lily'                                                       

In [3]: name3 = 'bob'                                                        

In [4]: name4 = 'coco'                                                       

In [5]: name1                                                                
Out[5]: 'tom'

In [6]: name2                                                                
Out[6]: 'lily'

In [7]: name3                                                                
Out[7]: 'bob'

In [8]: name4                                                                
Out[8]: 'coco'

In [9]: name = ['tom','lily','bob','coco']                                   

In [10]: name                                                                
Out[10]: ['tom', 'lily', 'bob', 'coco']

In [11]: type(name)                                                          
Out[11]: list

li = [1,1.2,True,‘hello’]
print(li)
print(type(li))

[1, 1.2, True, 'hello']
<class 'list'>

li = [1,1.2,True,‘hello’,[1,2,3,4,5]]
print(li)
print(type(li))

[1, 1.2, True, 'hello', [1, 2, 3, 4, 5]]
<class 'list'>

list(索引,切片,重複,連接,成員操作符,迭代)

service = [‘http’,‘ssh’,‘ftp’]

  • index索引
    print(service[0])

      http
    

    print(service[-1])

      ftp
    
  • slide切片
    print(service[::-1])

      ['ftp', 'ssh', 'http']
    

    print(service[1:])

      ['ssh', 'ftp']	
    

    print(service[:-1])

      ['http', 'ssh']
    
  • repeat重複
    print(service * 3)

      ['http', 'ssh', 'ftp', 'http', 'ssh', 'ftp', 'http', 'ssh', 'ftp']
    
  • link連接
    service1 = [‘mysql’,‘firewalld’]
    print(service + service1)

      ['http', 'ssh', 'ftp', 'mysql', 'firewalld']
    
  • in / not in成員操作
    print(‘firewalld’ in service)

      False
    

    print(‘firewalld’ in service1)

      True
    

    print(‘firewalld’ not in service1)

      False
    
  • for 迭代
    for se in service:
    print(se)

      http
      ssh
      ftp
    

service2 = [[‘http’,‘80’],[‘ssh’,‘22’],[‘ftp’,‘21’]]

  • index
    print(service2[0][1])

      80
    

    print(service2[-1][1])

      21
    
  • slide
    print(service2[:][1])

      ['ssh', '22']
    

    print(service2[:-1][0])

      ['http', '80']
    

list(練習:用法)

假定有下面這樣的列表:
names = [‘fentiao’, ‘fendai’, ‘fensi’, ‘apple’]
輸出結果爲:‘I have fentiao, fendai, fensi and apple.’

names = ['fentiao', 'fendai', 'fensi', 'apple']
print('I have ' + ', '.join(names[:-1]) + ' and ' + names[-1])

list(增,刪,改,查)

  • add增
    service = [‘http’,‘ssh’,‘ftp’]

    +
    print(service + [‘firewalld’])

    append
    service.append(‘firewalld’)
    print(service)

    extend
    service.extend([‘mysql’,‘firewalld’])
    print(service)

    insert(位置,成員)
    service.insert(1,‘samba’)
    print(service)

  • del刪

    1.pop
    從隊列尾部往出pop,pop出的成員最好接一下,以防丟失。
    也可指定下標pop

      service.pop(0)                                                       
       'http'
    

    2.remove
    a = service.remove(‘ssh’)
    print(service)

      ['http', 'ftp']
    

    print(a)

      none
    

    3.del刪除報錯
    print(service)

      ['http', 'ftp']
    

    del service
    print(service)

      Traceback (most recent call last):
        File "/root/Downloads/day04/10_list_del.py", line 57, in <module>
          print(service)
      NameError: name 'service' is not defined
    
  • change改

    service = [‘http’,‘ssh’,‘ftp’]

    index索引
    service[0] = ‘mysql’
    print(service)

      ['mysql', 'ssh', 'ftp']
    

    slide切片
    print(service[:2])

      ['mysql', 'ssh']
    

    service[:2] = [‘samba’,‘nfs’]
    print(service)

      ['samba', 'nfs', 'ftp']
    
  • look查

    service = [‘http’,‘ssh’,‘ftp’,‘http’]

    count統計
    print(service.count(‘http’))

      2
    

    index索引
    print(service.index(‘ssh’))

      1
    

    print(service.index(‘http’,0,13))

      0
    

list(排序)

  • sort
    service = [‘http’,‘ssh’,‘ftp’,‘http’,‘Http’]
    service.sort()
    print(service)

      ['Http', 'ftp', 'http', 'http', 'ssh']
    
  • range
    import random
    li = list(range(10))
    print(li)

      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
  • shuffle
    random.shuffle(li)
    print(li)

      [5, 9, 4, 2, 1, 0, 7, 6, 3, 8]
    

list(練習:用戶登陸系統)

添加用戶:
1). 判斷用戶是否存在?
2). 如果存在, 報錯;
3). 如果不存在,添加用戶名和密碼分別到列表中;

刪除用戶
1). 判斷用戶名是否存在
2). 如果存在,刪除;
3). 如果不存在, 報錯;
用戶登陸
用戶查看
1) 通過索引遍歷密碼
退出

要求:
1.系統裏面有多個用戶,用戶的信息目前保存在列表裏面
users = [‘root’,‘westos’]
passwd = [‘123’,‘456’]
2.用戶登陸(判斷用戶登陸是否成功
1).判斷用戶是否存在
2).如果存在
1).判斷用戶密碼是否正確
如果正確,登陸成功,推出循環
如果密碼不正確,重新登陸,總共有三次機會登陸
3).如果用戶不存在
重新登陸,總共有三次機會

users = ['root', 'westos']
passwds = ['123', '456']

trycount = 0

while trycount < 3:
    inuser = input('Username:')
    inpasswd = input('Password:')
    trycount += 1

    if inuser in users:
        index = users.index(inuser)
        passwd = passwds[index]
        if inpasswd == passwd:
            print('%s login success!' %inuser)
            break
        else:
            print('%s login failed: password not correct!' %inuser)
    else:
        print('User %s not exist!' %inuser)

else:
    print('no more chance!')

1.後臺管理員只有一個用戶: admin, 密碼: admin
2.當管理員登陸成功後, 可以管理前臺會員信息.
3.會員信息管理包含:
添加會員信息
刪除會員信息
查看會員信息
退出

print('管理員登陸'.center(50,'*'))
inuser = input('Username:')
inpasswd = input('Password:')

users = ['root', 'westos']

passwds = ['123', '456']

if inuser == 'admin' and inpasswd == 'admin':
    print('管理員登陸成功!')
    print('會員信息管理'.center(50,'*'))
    while True:
        print("""
           目錄 
       1.  添加會員信息
       2.  刪除會員信息
       3.  查看會員信息
       4.  退出
        """)
        choice = input('Please input your choice:')
        if choice == '1':
            pass
        elif choice == '2':
            pass
        elif choice == '3':
            pass
        elif choice == '4':
            exit()
        else:
            print('Please check your input!')
else:
    print('管理員登陸失敗!')

list(練習:棧)

棧的工作原理
入棧 append
出棧 pop
棧頂元素
棧的長度 len
棧是否爲空 len == 0

tuple

tuple(用法)

Python的元組與列表類似,不同之處在於元組的元素不能修改。
t = (1,2.3,True,‘westos’)
print(type(t))
print(t)

<class 'tuple'>
(1, 2.3, True, 'westos')

t1 = ([1,2,3],4)
t1[0].append(4)
print(t1)

([1, 2, 3, 4], 4)

t2 = (1,)
print(t2)
print(type(t2))

(1,)
<class 'tuple'>

touple(索引,重複,連接,成員操作符,迭代)

Users = (‘root’,‘westos’,‘redhat’)
Passwds = (‘123’,‘456’,‘789’)

  • index slide
    print(Users[0])
    print(Users[-1])
    print(Users[1:])
    print(Users[::-1])

  • repeat
    print(Passwds * 3)

  • link
    print(Users + (‘linux’,‘python’))

  • in /not in
    print(‘westos’ in Users)
    print(‘westos’ not in Users)

  • for
    for user in Users:
    print(user)

    for index,user in enumerate(Users):
    print(index,user)

    for user,passwd in zip(Users,Passwds):
    print(user,’:’,passwd)

  • common method(統計,通過索引尋找下標)

    t = (1,1.2,True,‘westos’)
    print(t.count(‘westos’))
    print(t.index(1))

tuple(練習:賦值,統計成績)

  • 獨特賦值法
    t = (‘westos’,11,100)
    name,age,score = t
    print(name,age,score)

      westos 11 100
    
  • 統計成績
    scores = (100,89,45,78,65)
    scoreli = list(scores)
    scoreli.sort()
    t = tuple(scoreli)
    print(t)

      (45, 65, 78, 89, 100)
    

    minscore,*middlescore,maxscore = t #中間值添加*是因爲返回個數有可能是多個
    print(minscore)

      45
    

    print(middlescore)

      [65, 78, 89]
    

    print(maxscore)

      100
    

    print(sum(middlescore) / len(middlescore))

      77.33333333333333
    

set

集合(set)是一個無序的不重複元素序列

set(用法)

s = {1,2,3,1,2,3,4,5}
print(s)

{1, 2, 3, 4, 5}

print(type(s))

<class 'set'>

s1 = {1}
print(s1)

{1}

print(type(s1))

<class 'set'>

s2 = {}
print(type(s2))

<class 'dict'>

s3 = set([])
print(type(s3))

<class 'set'>

li = [1,2,3,1,2,3]
print(list(set(li))

[1, 2, 3]

set(成員操作符,可迭代)

不可用下標索引,不可重複,不可連接

s = {1,2,3}

print(1 in s)

True

for i in s:
print(i,end=’|’)

1|2|3|

for i,v in enumerate(s):
print(i,v)

0 1
1 2
2 3

set(集和)

s = {6,7,8,9}
s.add(1)
print(s)

{1, 6, 7, 8, 9}

s.update({5,3,2})
print(s)

{1, 2, 3, 5, 6, 7, 8, 9}

s.pop()
print(s)

{2, 3, 5, 6, 7, 8, 9}

s.remove(6)
print(s)

{2, 3, 5, 7, 8, 9}
  • 交集,並集,差集
    s1 = {1,2,3}
    s2 = {2,3,4}

  • 並集
    print(‘並集:’,s1.union(s2))
    print(‘並集:’,s1 | s2)

      並集: {1, 2, 3, 4}
    
  • 交集
    print(‘交集:’,s1.intersection(s2))
    print(‘交集:’,s1 & s2)

      交集: {2, 3}
    
  • 差集
    print(‘差集:’,s1.difference(s2)) # s1-(s1&s2)

      差集: {1}
    

    print(‘差集:’,s2.difference(s1)) # s2-(s1&s2)

      差集: {4}
    
  • 對等差分:並集-差集
    print(‘對等差分:’,s1.symmetric_difference(s2))
    print(‘對等差分:’,s1^s2)

      對等差分: {1, 4}
    
  • 集和判斷
    s3 = {1,2}
    s4 = {1,2,3}

    print(s3.issuperset(s4))

      False
    

    print(s3.issubset(s4))

      True
    

    print(s3.isdisjoint(s4))

      False
    

set(練習:去重)

明明想在學校中請一些同學一起做一項問卷調查,爲了實驗的客觀性
他先用計算機生成了N個1~1000之間的隨機整數(N<=1000),N是用戶輸入的,
對於
其中重複的數字,只保留一個,把其餘相同的數字去掉,不同的數對應着不>同的學生的學號,然後再把這些
數從小到大排序,按照排好的順序去找同學做調查,請你協助明明完成“去重
”與排序工作

import random
s = set([])
for i in range(int(input('N:'))):
    s.add(random.randint(1,1000))

print(sorted(s))

dict

dict不可以通過下標索引

dict(用法)

users = [‘user1’,‘user2’]
passwds = [‘123’,‘456’]
print(zip(users,passwds))

<zip object at 0x7f332bec6820>

print(list(zip(users,passwds)))

[('user1', '123'), ('user2', '456')]

print(dict(zip(users,passwds)))

{'user1': '123', 'user2': '456'}

s = {}
print(type(s))

<class 'dict'>

s = {
‘linux’:[100,99,88],
‘westos’:[190,543,345]
}
print(s)

{'linux': [100, 99, 88], 'westos': [190, 543, 345]}

print(type(s))

<class 'dict'>

d = dict()
print(d)

{}

print(type(d))

<class 'dict'>

students = {
‘westos’:{
‘id’:‘03113009’,
‘age’:18,
‘score’:90
},
‘redhat’:{
‘id’:‘03113010’,
‘age’:20,
‘score’:100
}
}
print(students[‘westos’][‘id’])

03113009

dict(成員操作符,迭代)

d = {
‘1’:‘a’,
‘2’:‘b’
}

print(‘1’ in d)

True

print(‘1’ not in d)

False

for key in d:
print(key,d[key])

1 a
2 b

dict(增,刪,查)

  • add
    services = {
    ‘http’:80,
    ‘ftp’:21,
    ‘ssh’:22
    }
    services[‘mysql’] = 3306
    print(services)

      {'http': 80, 'ftp': 21, 'ssh': 22, 'mysql': 3306}
    

    services[‘http’] = 443
    print(services)

      {'http': 443, 'ftp': 21, 'ssh': 22, 'mysql': 3306}
    

    services_backup = {
    ‘https’:443,
    ‘tomcat’:8080,
    ‘http’:8080
    }

    services.update(services_backup)
    print(services)

      {'http': 8080, 'ftp': 21, 'ssh': 22, 'mysql': 3306, 'https': 443, 'tomcat': 8080}
    

    services.update(flask=9000,http=8000)
    print(services)

      {'http': 8000, 'ftp': 21, 'ssh': 22, 'mysql': 3306, 'https': 443, 'tomcat': 8080, 'flask': 9000}
    

    services.setdefault(‘http’,9090) #在沒有賦值的情況下賦予默認值
    print(services)

      {'http': 8000, 'ftp': 21, 'ssh': 22, 'mysql': 3306, 'https': 443, 'tomcat': 8080, 'flask': 9000}
    

    services.setdefault(‘oracle’,44575)
    print(services)

      {'http': 8000, 'ftp': 21, 'ssh': 22, 'mysql': 3306, 'https': 443, 'tomcat': 8080, 'flask': 9000, 'oracle': 44575}
    
  • del
    services = {
    ‘http’:80,
    ‘ftp’:21,
    ‘ssh’:22
    }

    del services[‘http’]
    print(services)

      {'ftp': 21, 'ssh': 22}
    

    item = services.pop(‘http’)
    print(item)
    print(services)

      80
      {'ftp': 21, 'ssh': 22}
    

    item = services.popitem()
    print(‘The last key-value is:’,item)

      The last key-value is: ('ssh', 22)
    

    print(services)

      {'ftp': 21}
    

    services.clear()
    print(services)

      {}
    
  • look
    services = {
    ‘http’:80,
    ‘ftp’:21,
    ‘ssh’:22
    }

    print(services.keys())

      dict_keys(['http', 'ftp', 'ssh'])
    

    print(services.values())

      dict_values([80, 21, 22])
    

    print(services.items())

      dict_items([('http', 80), ('ftp', 21), ('ssh', 22)])
    

    print(services[‘http’])

      80
    

    print(services.get(‘vsfptd’,‘key not exist’))

      key not exist
    

    for k in services:
    print(‘key:’,k,‘value:’,services[k])

      key: http value: 80
      key: ftp value: 21
      key: ssh value: 22
    

dict(練習:統計隨機數)

數字重複統計:
1). 隨機生成1000個整數;
2). 數字的範圍[20, 100],
3). 升序輸出所有不同的數字及其每個數字重複的次數;

import random

all_nums = []
for item in range(1000):
    all_nums.append(random.randint(20,100))

print(all_nums)

sorted_nums = sorted(all_nums)
num_dict = {}

for num in sorted_nums:
    if num in num_dict:
        num_dict[num] += 1
    else:
        num_dict[num] = 1

print(num_dict)

運行結果:

[69, 53, 96, 51, 64, 32, 100, 40, 37, 40, 53, 82, 23, 58, 54, 60, 32, 95, 90, 78, 71, 65, 28, 21, 47, 83, 92, 70, 62, 84, 45, 37, 47, 40, 29, 59, 55, 43, 37, 35, 87, 51, 21, 100, 70, 39, 95, 52, 93, 52, 22, 50, 72, 93, 76, 41, 51, 75, 20, 26, 57, 75, 77, 54, 62, 87, 30, 25, 23, 81, 34, 88, 57, 57, 62, 30, 67, 62, 73, 76, 57, 31, 24, 62, 43, 41, 21, 28, 91, 91, 39, 96, 79, 49, 36, 68, 83, 35, 77, 29, 23, 76, 84, 46, 77, 87, 54, 89, 75, 60, 38, 37, 28, 79, 48, 42, 70, 93, 75, 70, 69, 77, 74, 98, 24, 32, 57, 72, 89, 59, 93, 57, 20, 67, 97, 63, 47, 44, 67, 45, 82, 51, 45, 26, 59, 87, 29, 97, 45, 23, 65, 63, 57, 98, 80, 29, 77, 79, 98, 52, 34, 38, 70, 31, 90, 33, 58, 27, 78, 65, 30, 85, 68, 72, 31, 83, 74, 51, 94, 48, 29, 50, 91, 51, 59, 62, 44, 20, 31, 95, 97, 47, 92, 52, 47, 31, 98, 54, 64, 58, 38, 31, 52, 94, 88, 31, 92, 24, 61, 50, 22, 23, 93, 65, 46, 37, 46, 96, 54, 78, 54, 38, 80, 90, 67, 35, 61, 39, 85, 37, 52, 70, 46, 61, 52, 80, 61, 78, 38, 44, 100, 97, 42, 95, 50, 35, 55, 40, 72, 49, 35, 57, 23, 66, 24, 41, 93, 84, 25, 76, 88, 43, 42, 32, 50, 79, 82, 64, 25, 68, 47, 28, 65, 53, 70, 33, 21, 60, 74, 34, 91, 51, 48, 66, 23, 40, 62, 54, 60, 89, 87, 58, 81, 88, 77, 21, 53, 48, 93, 82, 80, 49, 73, 68, 42, 97, 30, 88, 30, 78, 79, 88, 98, 77, 80, 88, 96, 64, 71, 96, 81, 25, 43, 89, 83, 75, 20, 93, 41, 24, 64, 45, 98, 54, 21, 83, 25, 73, 30, 45, 93, 84, 82, 76, 59, 90, 88, 71, 79, 57, 28, 29, 70, 99, 87, 29, 26, 47, 85, 86, 22, 56, 36, 64, 36, 90, 80, 97, 84, 97, 24, 98, 70, 77, 70, 69, 26, 82, 62, 43, 78, 86, 34, 77, 33, 76, 53, 78, 92, 52, 84, 78, 80, 78, 33, 25, 35, 88, 34, 48, 81, 91, 37, 95, 51, 31, 38, 33, 33, 93, 79, 31, 84, 69, 40, 81, 77, 72, 96, 89, 74, 78, 52, 40, 50, 34, 51, 20, 80, 96, 48, 99, 90, 65, 35, 39, 81, 36, 25, 44, 98, 54, 46, 59, 81, 59, 44, 31, 68, 28, 95, 53, 62, 82, 54, 50, 22, 21, 32, 52, 87, 82, 61, 28, 32, 84, 71, 53, 85, 52, 20, 88, 92, 91, 36, 30, 97, 43, 87, 27, 54, 88, 79, 75, 51, 39, 29, 63, 55, 90, 60, 93, 50, 42, 20, 64, 68, 88, 91, 24, 70, 58, 72, 47, 47, 41, 55, 93, 86, 59, 28, 76, 66, 97, 76, 73, 91, 57, 48, 70, 38, 72, 43, 54, 50, 95, 75, 82, 42, 89, 31, 31, 35, 52, 56, 52, 74, 64, 41, 67, 29, 46, 36, 77, 30, 54, 87, 41, 68, 91, 82, 52, 76, 67, 74, 71, 68, 28, 32, 94, 35, 62, 36, 21, 82, 55, 22, 48, 96, 21, 21, 58, 65, 60, 85, 38, 25, 89, 50, 76, 30, 79, 25, 26, 47, 93, 99, 84, 41, 51, 51, 71, 46, 22, 63, 56, 41, 45, 20, 76, 51, 79, 55, 64, 24, 57, 73, 79, 66, 68, 72, 54, 55, 30, 83, 39, 41, 95, 38, 69, 60, 25, 80, 27, 71, 89, 75, 27, 73, 95, 98, 59, 44, 93, 69, 35, 58, 70, 37, 74, 72, 47, 79, 46, 62, 35, 34, 21, 63, 91, 43, 43, 33, 48, 39, 88, 74, 69, 75, 96, 86, 26, 99, 80, 97, 94, 92, 25, 43, 91, 44, 64, 81, 81, 43, 21, 97, 87, 100, 61, 25, 59, 54, 27, 57, 46, 43, 50, 28, 68, 49, 51, 48, 53, 27, 84, 67, 81, 98, 28, 93, 24, 54, 63, 67, 46, 93, 28, 95, 27, 66, 69, 41, 57, 70, 40, 99, 42, 77, 42, 39, 50, 86, 24, 60, 40, 61, 59, 82, 73, 27, 80, 59, 97, 80, 63, 53, 94, 78, 55, 100, 99, 33, 97, 78, 33, 92, 95, 38, 79, 81, 50, 26, 60, 63, 98, 100, 57, 55, 69, 80, 29, 27, 91, 37, 93, 22, 27, 94, 59, 90, 21, 30, 29, 78, 70, 85, 40, 53, 36, 53, 47, 77, 100, 86, 100, 21, 69, 36, 47, 30, 97, 92, 60, 32, 60, 42, 20, 20, 26, 37, 80, 71, 84, 63, 52, 79, 51, 83, 86, 75, 61, 64, 79, 82, 22, 100, 67, 75, 82, 24, 58, 37, 45, 80, 20, 54, 98, 67, 70, 89, 67, 20, 71, 82, 88, 91, 97, 49, 52, 93, 70, 58, 26, 82, 81, 79, 50, 34, 46, 34, 46, 43, 66, 51, 42, 57, 36, 57, 86, 27, 21, 38, 25, 24, 44, 100, 71, 32, 31, 53, 21, 24, 25, 58, 29, 63, 77, 23, 53, 48, 90, 88, 64, 92, 85, 96, 69, 35, 58, 69, 95, 64, 88, 30, 32, 67, 47, 68, 59, 55, 22, 55, 81, 96, 93, 94, 53, 88, 73, 66, 39, 64, 98, 58, 37, 94, 37, 58, 99, 24, 56, 46, 73, 20, 35, 71, 34, 90, 34, 62, 24, 25, 50, 42, 50, 99, 68, 75, 76, 81, 49, 50, 69, 63, 21, 56, 61, 48, 33, 73, 34, 75, 93, 34, 81, 49, 60, 45, 31, 60, 21, 78, 23, 35, 68, 61, 89, 50, 72, 57, 61, 53, 32, 85, 99, 88, 57, 94, 89, 88, 69, 81, 46, 83, 68, 49, 39, 87, 41, 22, 82, 67, 70, 41, 44, 61, 93, 45, 33]
{20: 13, 21: 18, 22: 10, 23: 9, 24: 15, 25: 15, 26: 9, 27: 11, 28: 12, 29: 12, 30: 13, 31: 14, 32: 11, 33: 11, 34: 13, 35: 14, 36: 10, 37: 13, 38: 11, 39: 10, 40: 10, 41: 13, 42: 11, 43: 13, 44: 9, 45: 10, 46: 14, 47: 14, 48: 12, 49: 8, 50: 18, 51: 16, 52: 16, 53: 15, 54: 17, 55: 11, 56: 5, 57: 18, 58: 13, 59: 14, 60: 13, 61: 12, 62: 12, 63: 11, 64: 14, 65: 7, 66: 7, 67: 13, 68: 14, 69: 14, 70: 18, 71: 11, 72: 10, 73: 10, 74: 8, 75: 13, 76: 12, 77: 14, 78: 14, 79: 16, 80: 15, 81: 16, 82: 17, 83: 8, 84: 11, 85: 8, 86: 8, 87: 11, 88: 19, 89: 11, 90: 10, 91: 13, 92: 9, 93: 21, 94: 9, 95: 12, 96: 11, 97: 15, 98: 13, 99: 9, 100: 10}

dict(練習:生成卡號)

1.隨機生成100個卡號;
卡號以6102009開頭, 後面3位依次是 (001, 002, 003, 100),
2.生成關於銀行卡號的字典, 默認每個卡號的初始密碼爲"redhat";

3.輸出卡號和密碼信息, 格式如下:
卡號 密碼
6102009001 000000

import random

account_num = []

for i in range(100):
    account_num.append('6102009%.3d' %(i+1))

account_info = {}.fromkeys(account_num,'redhat')

print('卡號\t\t\t\t 密碼')
for k,v in account_info.items():
    print(k,'\t\t',v)

運行結果:

卡號				 密碼
6102009001 		 redhat
6102009002 		 redhat
6102009003 		 redhat
..........

其他用法(最大最小,求和,枚舉,zip,fromkeys)

  • 最大最小判斷
    In [1]: min(3,4)
    Out[1]: 3

    In [2]: min(1,2)
    Out[2]: 1

    In [3]: max(3,4)
    Out[3]: 4

    In [4]: max(1,2)
    Out[4]: 2

  • 求和
    In [5]: sum(range(1,101))
    Out[5]: 5050

In [6]: sum(range(1,101,2))
Out[6]: 2500

In [7]: sum(range(2,101,2))
Out[7]: 2550

  • 枚舉
    for i,v in enumerate(‘hello’):
    print(i,v)
    結果

      0 h
      1 e
      2 l
      3 l
      4 o
    
  • zip
    s1 = ‘abc’
    s2 = ‘123’
    for i in zip(s1,s2):
    print(i)

    for i in zip(s1,s2):
    print(’’.join(i))

  • fromkeys
    Python 字典 fromkeys() 函數用於創建一個新字典,以序列 seq 中元素做字典的鍵,value 爲字典所有鍵對應的初始值。

    print({}.fromkeys({‘1’,‘2’},‘000000’))

      {'1': '000000', '2': '000000'}
    

後記

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章