python 02:データ型
8173 ワード
Pythonのデータ型は多種あって、主に数字、文字列、リスト、メタグループ、辞書、集合があります
1.数値タイプ
python数値タイプ:
1,2のような整数
浮動小数点数、例えば1.2 1.4 E 4
ブール、True、False
複素数、(-5+4 j)
2.bytesタイプ
テキストは常にUnicode strタイプ表示
バイナリデータはbytesタイプで表されます
3.文字列
文字列は引用符で囲む必要があります.単一引用符、二重引用符、三重引用符を使用できます.文字列は可変ではありません.
文字列関連アクション
4.リスト
リストはpythonの一般的なデータ型の1つで、「[]」で表され、リストの一般的な操作スライス、リスト解析
リストのレプリケーションには、浅いレプリケーションと深いレプリケーションの違いがあります.
リスト解析
リスト解析は、既存のリストに基づいて効率的に新しいリストを迅速に生成する方法であり、反復メカニズムの応用である.
5.辞書
Pythonの辞書は無秩序であり,キー値ペアの形で存在し,定義辞書は「{」「name」「トム」「age」「23}を用いる必要がある.
6.集合
1.数値タイプ
python数値タイプ:
1,2のような整数
浮動小数点数、例えば1.2 1.4 E 4
ブール、True、False
複素数、(-5+4 j)
2.bytesタイプ
テキストは常にUnicode strタイプ表示
バイナリデータはbytesタイプで表されます
>>> str = " ">>> print(str.encode())
b'\xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8'
>>> byte = str.encode() # byte
>>> print(byte.decode()) # byte
3.文字列
文字列は引用符で囲む必要があります.単一引用符、二重引用符、三重引用符を使用できます.文字列は可変ではありません.
文字列関連アクション
# ‘m’
>>> print(name.find("m"))
0
#
>>> print(name1.format(name="alex",age=23))
my name is alex,age is 23
#
>>> print(name1.format_map({"name": "oldboy", "age": 29}))
my name is oldboy,age is 29
#
>>> print("1234dd".isalnum())
True
#
>>> print("adbd".isalpha())
True
#
>>> print("123445".isdecimal())
True
#
>>> print("123".isdigit())
True
#
>>> print("-1A".isidentifier())
False
#
>>> print("+".join(['1', '2', '3']))
1+2+3
#
>>> print("\tAlex\t".strip())
Alex
#
>>> print("alex li".replace('l','L'))
aLex Li
#split
>>> print("1+2+3+4".split("+"))
['1', '2', '3', '4']
# ”
” ,
>>> print("1+2
+3+4".splitlines())
['1+2', '+3+4']
#
>>> print("alex li".title())
Alex Li
4.リスト
リストはpythonの一般的なデータ型の1つで、「[]」で表され、リストの一般的な操作スライス、リスト解析
#
>>> names = ["Alex","Hudaohong","Oldboy","tom"]
#
# 1
>>> names[1:2]
['Hudaohong']
# 0,1
>>> names[:2]
['Alex', 'Hudaohong']
#-1
>>> names[-4:-1:2]
['Alex', 'Oldboy']
>>> names[:-1:2]
['Alex', 'Oldboy']
#
>>> names.append("Lucy")
>>> names
['Alex', 'Hudaohong', 'Oldboy', 'tom', 'Lucy']
#
>>> names.insert(3,"Tom")
>>> names
['Alex', 'Hudaohong', 'Oldboy', 'Tom', 'tom', 'Lucy']
# , , ,
>>> names.pop()
'Lucy'
>>> names.pop("Lucy")
Traceback (most recent call last):
File "", line 1, in
names.pop("Lucy")
TypeError: 'str' object cannot be interpreted as an integer
#
>>> names[names.index("Tom")]
'Tom'
#
>>> names.count("Tom")
1
#
>>> names
['tom', 'Tom', 'Oldboy', 'Hudaohong', 'Alex']
>>> names.reverse()
>>> names
['Alex', 'Hudaohong', 'Oldboy', 'Tom', 'tom']
#
>>> names.sort()
>>> names
['Alex', 'Hudaohong', 'Oldboy', 'Tom', 'tom']
リストのレプリケーションには、浅いレプリケーションと深いレプリケーションの違いがあります.
>>> import copy
>>> names = ["Alex","Hudaohong","Oldboy","Tom",["Jim","Lucy"]]
#
>>> names1 = names.copy()
#
>>> names2=copy.deepcopy(names)
# 2
>>> names[1] = " "# 1
>>> names[4][0]="JIM">>>#
>>> print("{name} is names".format(name=names))
['Alex', ' ', 'Oldboy', 'Tom', ['JIM', 'Lucy']] is names
>>> print("{name} is names1".format(name=names1))
['Alex', 'Hudaohong', 'Oldboy', 'Tom', ['JIM', 'Lucy']] is names1
>>> print("{name} is names2".format(name=names2))
['Alex', 'Hudaohong', 'Oldboy', 'Tom', ['Jim', 'Lucy']] is names2
リスト解析
リスト解析は、既存のリストに基づいて効率的に新しいリストを迅速に生成する方法であり、反復メカニズムの応用である.
[expression for iter_var in iterable]
[expression for iter_var in iterable if cond_expr]
>>> l1=[1,2,3,4]
#
>>> l2 = [I ** 2 for I in l1]
>>> l2
[1, 4, 9, 16]
>>> l3 = [I ** 2 for I in l1 if I> 2]
>>> l3
[9, 16]
5.辞書
Pythonの辞書は無秩序であり,キー値ペアの形で存在し,定義辞書は「{」「name」「トム」「age」「23}を用いる必要がある.
#
>>> info = {"stu01":"Tom","stu02":"Lucy","stu03":"Jim"}#
>>> info["stu01"]'Tom'
#
>>> info["stu04"] = "Baby"
#
>>> del info["stu04"]
>>> info.pop("stu03")
'Jim'
# ,
>>> info.get("stu04")
>>> info.get("stu01")
'Tom'
#
>>> print("stu01" in info)
True
#update ,
>>> dictb={"stu100":"zhangsan","stu02":"LiSi","stu101":"WangWu"}
>>> info.update(dictb)
>>> info
{'stu01': 'Tom', 'stu02': 'LiSi', 'stu100': 'zhangsan', 'stu101': 'WangWu'}
#
>>> info_c = info.items()
>>> print(info_c)
dict_items([('stu01', 'Tom'), ('stu02', 'LiSi'), ('stu100', 'zhangsan'), ('stu101', 'WangWu')])
#
>>> dictC=dict.fromkeys([6,7,8],[1,{"name":"Boy"},444])
>>> dictC
{6: [1, {'name': 'Boy'}, 444], 7: [1, {'name': 'Boy'}, 444], 8: [1, {'name': 'Boy'}, 444]}
# 3 name ,
>>> dictC[7][1]["name"] = "girls"
>>> dictC
{6: [1, {'name': 'girls'}, 444], 7: [1, {'name': 'girls'}, 444], 8: [1, {'name': 'girls'}, 444]}
# ,
>>> for key in dictC:print(key,dictC[key])6 [1, {'name': 'girls'}, 444]7 [1, {'name': 'girls'}, 444]8 [1, {'name': 'girls'}, 444]
#
>>> for k,v in info.items():print(k,v)
stu01 Tom
stu02 LiSi
stu100 zhangsan
stu101 WangWu
6.集合
set
: , ,
#
>>> list01 = [1, 4, 5, 7, 3, 5, 1, 9]
>>> list01= set(list01)
>>> list01
{1, 3, 4, 5, 7, 9}
>>> list02=set([2,4,6,8,3])
#
>>> print(list01.intersection(list02))
{3, 4}
>>> print(list01&list02)
{3, 4}
#
>>> print(list01.union(list02))
{1, 2, 3, 4, 5, 6, 7, 8, 9}
>>> print(list01 | list02)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
# ,list01 ,list02
>>> print(list01.difference(list02))
{1, 5, 9, 7}
>>> print(list01 - list02)
{1, 5, 9, 7}
# : list03 list01
>>> list03 = set([1,3,7])
>>> print(list03.issubset(list01))
True
# list01 list3
>>> print(list01.issubset(list03))
False
# , >
>> print(list01.symmetric_difference(list03))
{4, 5, 9}
>>> print(list01 ^ list03)
{4, 5, 9}
# , True
>>> list04=([5,6,8])
>>> print(list03.isdisjoint(list04))
True
#
>>> list01.add(99)
>>> list01.update([88,77,55])
>>> print(list01)
{1, 3, 4, 5, 99, 7, 9, 77, 55, 88}
#
>>> list01.pop()
1
#remove
>>> list01.remove(77)
>>> list01
{3, 4, 5, 99, 7, 9, 55, 88}