Lao Qi から Python を学び、list と str を比較する

WBOY
リリース: 2016-06-16 08:41:59
オリジナル
1069 人が閲覧しました

同一性

すべてのデータはシーケンス タイプに属します

いわゆるシーケンス型のデータとは、数値を指定することで各要素を取得できるもので、専門用語で「オフセット」と呼ばれますが、複数の要素を一度に取得したい場合にはスライスを利用することができます。オフセットは 0 から始まり、要素の合計数から 1 を引いた値で終わります。

例:

>>> welcome_str = "Welcome you"
>>> welcome_str[0]
'W'
>>> welcome_str[1]
'e'
>>> welcome_str[len(welcome_str)-1]
'u'
>>> welcome_str[:4]
'Welc'
>>> a = "python"
>>> a*3
'pythonpythonpython'

>>> git_list = ["qiwsir","github","io"]
>>> git_list[0]
'qiwsir'
>>> git_list[len(git_list)-1]
'io'
>>> git_list[0:2]
['qiwsir', 'github']
>>> b = ['qiwsir']
>>> b*7
['qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir', 'qiwsir']

ログイン後にコピー

このタイプのデータの場合、次の操作のいくつかは類似しています:

>>> first = "hello,world"
>>> welcome_str
'Welcome you'
>>> first+","+welcome_str  #用+号连接str
'hello,world,Welcome you'
>>> welcome_str       #原来的str没有受到影响,即上面的+号连接后从新生成了一个字符串
'Welcome you'
>>> first
'hello,world'

>>> language = ['python']
>>> git_list
['qiwsir', 'github', 'io']
>>> language + git_list   #用+号连接list,得到一个新的list
['python', 'qiwsir', 'github', 'io']
>>> git_list
['qiwsir', 'github', 'io']
>>> language
['python']

>>> len(welcome_str)  #得到字符数
11
>>> len(git_list)    #得到元素数
3

ログイン後にコピー

違い

list と str の最大の違いは、list は元の場所で変更できるのに対し、str は元の場所で不変であることです。これをどのように理解すればよいでしょうか?

まず、リスト上のこれらの操作を見てください。その特徴は、リストが元の場所で変更されていることです。

>>> git_list
['qiwsir', 'github', 'io']

>>> git_list.append("python")
>>> git_list
['qiwsir', 'github', 'io', 'python']

>>> git_list[1]        
'github'
>>> git_list[1] = 'github.com'
>>> git_list
['qiwsir', 'github.com', 'io', 'python']

>>> git_list.insert(1,"algorithm")
>>> git_list
['qiwsir', 'algorithm', 'github.com', 'io', 'python']

>>> git_list.pop()
'python'

>>> del git_list[1]
>>> git_list
['qiwsir', 'github.com', 'io']

ログイン後にコピー

上記の操作が str で使用される場合、次のようなエラーが報告されます:

>>> welcome_str
'Welcome you'

>>> welcome_str[1] = 'E'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

>>> del welcome_str[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object doesn't support item deletion

>>> welcome_str.append("E")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'

ログイン後にコピー

str を変更したい場合は、これを行う必要があります。

>>> welcome_str
'Welcome you'
>>> welcome_str[0] + "E" + welcome_str[2:] #从新生成一个str
'WElcome you'
>>> welcome_str             #对原来的没有任何影响
'Welcome you'
ログイン後にコピー

実際、このアプローチでは、str を最初から生成するのと同じです。

多次元リスト

少し突飛ではありますが、これが 2 つの違いであると考えるべきです。 str では、各要素は文字のみにすることができます。list では、要素は任意のタイプのデータにすることができます。これまでに見てきたもののほとんどは数字または文字ですが、次のようなものもあります:

>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> matrix[0][1]
2
>>> mult = [[1,2,3],['a','b','c'],'d','e']
>>> mult
[[1, 2, 3], ['a', 'b', 'c'], 'd', 'e']
>>> mult[1][1]
'b'
>>> mult[2]
'd'
ログイン後にコピー

上記は多次元リストとアクセス方法を示しています。多次元の場合、内部のリストも前の要素と同様に扱われます。

リストと文字列の変換

str.split()

この組み込み関数は、str をリストに変換します。 str="" は区切り文字です。

例を見る前に、対話モードで次の操作を行ってください:

>>>ヘルプ(str.split)
この組み込み関数の完全な説明を取得します。特に強調する: これは非常に優れた学習方法です

split(...)
S.split([sep [,maxsplit]]) -> list of strings
Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.
ログイン後にコピー

上の段落を理解したかどうかに関係なく、例を見てください。以上の内容を読者の皆様にご理解いただければ幸いです。

>>> line = "Hello.I am qiwsir.Welcome you." 

>>> line.split(".")   #以英文的句点为分隔符,得到list
['Hello', 'I am qiwsir', 'Welcome you', '']

>>> line.split(".",1)  #这个1,就是表达了上文中的:If maxsplit is given, at most maxsplit splits are done.
['Hello', 'I am qiwsir.Welcome you.']    

>>> name = "Albert Ainstain"  #也有可能用空格来做为分隔符
>>> name.split(" ")
['Albert', 'Ainstain']
"[sep]".join(list)

ログイン後にコピー

join は、split の逆操作であると言えます。例:

>>> name
['Albert', 'Ainstain']
>>> "".join(name)    #将list中的元素连接起来,但是没有连接符,表示一个一个紧邻着
'AlbertAinstain'
>>> ".".join(name)   #以英文的句点做为连接分隔符
'Albert.Ainstain'
>>> " ".join(name)   #以空格做为连接的分隔符
'Albert Ainstain'
ログイン後にコピー

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!