#### ###タプル
タプル:括弧備え命じ、Pythonのタプルの不変のセット;
リスト:ビート配列ホルモン
タプルは:マジックは、リスト(不変のデータ型とスペル付加および欠失なし、任意のデータ型を格納することができる)
タプル1 ##を定義していません。
t = (1,2.3,'westos',True) ##创建元组t
print(t,type(t)) ##打印t并显示类型
(1, 2.3, 'westos', True) <class 'tuple'> ##t 元组类型
t1 = ([1,2,3],4) ##列表也可以包含在元组中建立
print(t1,type(t1))
([1, 2, 3], 4) <class 'tuple'>
2.)タプル値は、削除、追加、\変更することはできません作成されたら、変更した場合は、変数のデータ型が含まれているタプル、することができます間接的にタプルコンテンツ
t1 = ([1,2,3],4) ##建立一个包含列表的元组
print(t1,type(t1))
t1[0].append(6) ##给列表添加
print(t1)
([1, 2, 3], 4) <class 'tuple'>
([1, 2, 3, 6], 4) <class 'tuple'>##列表增添成功,元组类型
t2 = () ##建立t2元组
print(t2,type(t2))
() <class 'tuple'>
t3 = tuple([]) ##建立t3元组,包含列表
print(t3,type(t3))
() <class 'tuple'>
t4 = list(t3) ##转换成列表类型
print(t4,type(t4)
[] <class 'list'>
# 元组如果只有一个元素,元素后面一定要加逗号,否则数据类型不确定
list =[1,2]
print(list,type(list))
[1, 2] <class 'list'>
t = (2)
print(t,type(t))
2 <class 'int'>
t1= (2,)
print(t1,type(t1))
(2,) <class 'tuple'>
##タプルの一般的な方法
3)一般的な方法タプル。
t = (1,2.5,'westos',True,'westos')
print(t.count('westos')) ##返回该值在元组中出现的次数
print(t.index(2.5)) ##在元组中搜索指定值,并返回指出该值的位置
2
1
特性##タプル
のt =(1,2.5、 'westos' 、真の'westos')
1)#インデックス
print(t[0])
print(t[1])
print(t[2])
1
2.5
westos
2)スライス#
print(t[:-1])
print(t[:1])
print(t[::-2])
(1, 2.5, 'westos', True)
(1,)
('westos', 'westos', 1)
3)#接続
print(t + (1,4,5,6))
(1, 2.5, 'westos', True, 'westos', 1, 4, 5, 6)
4)は、異なるデータ型との間に接続することができない#
#print(t + [1,10,4,5]) ##连接列表
TypeError: can only concatenate tuple (not "list") to tuple ##报错:元组不能来连接列表
print(t + 'redhat')
#print(t + 'westos')TypeError: can only concatenate tuple (not "str") to tuple ##报错:元组不能连接字符串类型
5)を繰り返し#
print(t * 3)
(1, 2.5, 'westos', True, 'westos', 1, 2.5, 'westos', True, 'westos', 1, 2.5, 'westos', True, 'westos')
ループ反復するタプルについて
for i in t:
print(i)
1
2.5
westos
True
westos
6)#部材演算子()プロジェクトが存在するかどうかをチェックします
print(10 in t)
print(2.5 in t)
print(True in t)
False
True
True
運動:
1.システムの複数のユーザがユーザ情報が現在どのリストに格納され、ある
ユーザー= [「ルートは」、「westos」]
のpasswd = [「123」、「456」]
-
ユーザーの追加:
。?1)ユーザーが存在するかどうかを判断する
2)その場合は、エラーが;.
3)でない場合は、リストにユーザー名とパスワードを追加;. -
ユーザを削除する
。1)は、ユーザ名があるか否かを判断
2)存在する場合、削除され;.
3)エラーがある場合.; -
ユーザーログイン
-
ユーザービュー
-
終了
シナリオ##タプル
a = 5
b = 8
b,a = a,b ## b,a = (5,8) b=(5,8)[0] a=(5,8)[1] 切片的方法
print(a)
print(b)
#印刷変数
name = 'westos'
age = 11
#print('name:%s,age:%d' %(name,age)) ##方法一
t = (name,age)
print('name:%s,age:%d' %t) ##方法二
#タプルの割り当て:それは変数の数を受けてどのように多くの要素
t = ('westos',10,100)
name,age,score =t
print(name,age,score)
###のコレクション####
セットは、括弧を含む非順序、インデックスのないデータセットである;
1)コレクションを作成します
s = {1,2,3,4,1,2,3,4,1,2,3,4}# 集合里面的元素不可重复的
print(s,type(s))
{1, 2, 3, 4} <class 'set'>
# 定义一个空集合
s2 = {} #默认情况下是dict
print(s2,type(s2))
{} <class 'dict'>
s3 = set([]) ##创建空集合
print(s3,type(s3))
set() <class 'set'>
# 列表去重
li = [1,2,3,1,2,3,4,5,6,4,5,6,7,7,9,9]
print(list(set(li)))
[1, 2, 3, 4, 5, 6, 7, 9]
特徴##のセット
のみでサポートされる機能の設定:メンバー演算子
s = {1,2,5}
print(1 in s)
print(5 not in s)
for i in s:
print(i,end='')
print()
print('###')
一般的な方法###のセット:
#は変数データタイプのセットである
添加の順序とは異なる順序で#セットに格納された
S = {1,6,8,3,7}
s = {1,6,8,3,7}
print(s)
{1, 3, 6, 7, 8}
追加#
s.add(2)
s.add(0)
print(s)
{0, 1, 2, 3, 6, 7, 8}
#複数の要素を追加します。
s.update({3,5,1,2,7})
print(s)
{0, 1, 2, 3, 5, 6, 7, 8}
#削除
s.pop() ##默认弹出最后一个值
print(s)
{1, 2, 3, 5, 6, 7, 8}
#指定された要素を削除します。
s.remove(5)
print(s)
{1, 2, 3, 6, 7, 8}
#s.remove(100)#删除指定元素 元素必须要存在
#print(s)
Traceback (most recent call last):
File "/home/kiosk/PycharmProjects/python学习/集合.py", line 48, in <module>
s.remove(100)
KeyError: 100
Process finished with exit code 1
#積集合と差セット
S1 = {1,2,3}
S2 = {2,3,4}
#組合
print('并集:',s1.union(s2)) #方法一
并集: {1, 2, 3, 4, 5, 6, 7, 8}
print('并集:',s1 | s2) ##方法二
并集: {1, 2, 3, 4, 5, 6, 7, 8}
#交差点
print('交集:',s1.intersection(s2))
交集: {8, 3}
print('交集:',s1 & s2)
交集: {8, 3}
#差分セット
#s1和s2的差集:s1中有哪些元素s2中没有
print('差集:',s1.difference(s2))
差集: {1, 4, 6}
print('差集:',s1-s2)
差集: {1, 4, 6}
#s2和s1的差集
print('差集:',s2.difference(s1))
差集: {2, 5, 7}
print('差集:',s2-s1)
差集: {2, 5, 7}
#ピア差:組合 - 交差点
print('对等差分:',s1.symmetric_difference(s2))
对等差分: {1, 2, 4, 5, 6, 7}
print('对等差分:',s1^s2)
对等差分: {1, 2, 4, 5, 6, 7}
= {1,2,3} S3
S4 = {1,2,3,4}
#s3是否s4的子集
print(s3.issubset(s4))
True
#s3是否s4的超集
print(s3.issuperset(s4))
False
#两个集合是不是不相交
print(s3.isdisjoint(s4))
False
Huawei社のマシンのテストの質問:
明らかに調査を行うために一緒に学校の学生のいくつかをお願いしたいと思います、の客観性をテストするために、
彼の最初のコンピュータを1〜千Nの数(N <= 1000)の間のランダムな整数を生成し
、Nであります以下のためにユーザによって入力
番号を重複たが、一つは、残りの同じ番号を除去するために、
異なる番号が異なる学校の生徒の数に対応
し、それら
ソーティングの多数の小さなから、良好な順に従って行に行きます学生が研究を行うために、
あなたは明らかに動作一種の「重い行く」と完成を助ける
一般的に使用される方法###のpythonの
1テイク最大値と最小値の最小値、最大値を
[kiosk@foundation46 bin]$ ./python3
Python 3.6.4 (default, May 23 2019, 18:41:28)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> min(3,10)
3
>>> min(2,11)
2
>>> max(3,10)
10
>>> max(2,11)
11
>>>
2.求和
求1~100间加数的和
#>>> sum(range(1,101))
#5050
求1~100间奇数的和
#>>> sum(range(1,101,2))
#2500
求1~100间偶数的和
#>>> sum(range(2,101,2))
#2550
#枚举:返回索引值和对应的value值
for i,v in enumerate('hello'):
#print(i,v)
0 h
1 e
2 l
3 l
4 o
print(str(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)
('a', '2')
('b', '3')
('c', '4')
for i in zip(s1,s2):
print(''.join(i))
a2
b3
c4
for i in zip(s1,s2):
print('@'.join(i))
a@2
b@3
c@4
####字典###
字典是无序的、可修改的键值对key-value 集合,大括号包裹
1)创建字典
s = {}
print(s,type(s))
{} <class 'dict'>
字典:key-value 键值对
s = {
'user': ['root','student'],
'passwd':[123,456]
}
print(s,type(s))
{'user': ['root', 'student'], 'passwd': [123, 456]} <class 'dict'>
工厂函数
d = dict()
print(d,type(d))
{} <class 'dict'>
d = dict(a=1,b=2)
print(d,type(d))
{'a': 1, 'b': 2} <class 'dict'>
#字典的嵌套
ser = {
'123':{
'name':'root',
'passwd':'redhat'
},
'456':{
'name':'student',
'psaawd':'student'
}
}
print(user['123']['name'])
root
print(user['123']['passwd'])
redhat
print(user['456']['passwd'])
student
###字典的特性###
d = {
‘1’:‘a’,
‘2’:‘b’
}
print(d[‘1’])
字典不支持切片的
成员操作符(针对key)
print('1' in d)
True
print('1' not in d)
False
for循环 针对key
for key in d:
print(key)
1
2
遍历字典
for key in d:
print(key,d[key])
1 a
2 b
sorted()
##字典元素的增加
service = {
‘http’:80,
‘ftp’:23,
‘ssh’:22
}
#增加一个元素
#如果key值存在,则更新对应的value值
#如果key值不存在,则添加对应的key-value值
service['https'] = 443
print(service)
{'http': 80, 'ftp': 23, 'ssh': 22, 'https': 443}
service['ftp'] = 21
print(service)
{'http': 80, 'ftp': 21, 'ssh': 22, 'https': 443}
#增加多个key值
service_backup = {
'tomcat':8080,
'mysql':3306
}
service.update(service_backup)
print(service)
{'http': 80, 'ftp': 23, 'ssh': 22, 'tomcat': 8080, 'mysql': 3306}
service.update(flask=9000,dns=53)
print(service)
{'http': 80, 'ftp': 23, 'ssh': 22, 'tomcat': 8080, 'mysql': 3306, 'flask': 9000, 'dns': 53}
#如果key值存在:不做修改
#如果key值不存在:则添加对应的key-value
service.setdefault('http',9090)
print(service)
{'http': 80, 'ftp': 23, 'ssh': 22}
service.setdefault('oracle',44575)
print(service)
{'http': 80, 'ftp': 23, 'ssh': 22, 'oracle': 44575}
字典元素的删除
service = {
‘http’:80,
‘ftp’:23,
‘ssh’:22
}
del service['http']
print(service)
{'ftp': 23, 'ssh': 22}
pop删除指定key对应的value值
如果key存在,删除,并且返回删除key对应的value(可以用变量取接收)
如果不存在,直接报错
#item = service.pop(‘https’) ##b不存在key值
#print(item)
Traceback (most recent call last):
File “/home/kiosk/PycharmProjects/python学习/字典.py”, line 59, in
item = service.pop(‘https’)
KeyError: ‘https’
print(service)
Traceback (most recent call last):
File “/home/kiosk/PycharmProjects/python学习/字典.py”, line 59, in
item = service.pop(‘https’)
KeyError: ‘https’
Process finished with exit code 1
#popitem删除最后一个key-value值
item = service.popitem()
print(item)
(‘ssh’, 22)
print(service)
{‘http’: 80, ‘ftp’: 23}
#清空字典内容
service.clear()
print(service)
/usr/local/python3/bin/python3 /home/kiosk/PycharmProjects/python学习/字典.py
{}
##辞書の要素はチェック
-サービス= {
'HTTP'を80、
'FTP':23は、ある
'SSH':22である
}
#チェック辞書キー値
プリント(service.keys())
dict_keys([ 'HTTP'、「FTP '' SSH「])
#値引数辞書ビュー
プリント(service.values())
dict_values(23である[80、22])は
#キーと値の辞書は値を参照
)プリント(service.items()
[(dict_itemsを( 'HTTP'、80)、( 'FTP'、23がある)、( 'SSH'、22がされる)])
キーが存在しない、デフォルトの戻りなし
キーが存在、デフォルト値はNoneを返していないdefalut返されます
印刷(service.get( 'HTTPS'、 '443'))
443