Python学習2

30041 ワード

pythonには、リスト、メタグループ、辞書(using_list.py、using_tuple.py、using-dict.py)リストの3つの内部データ構造があります.shoplist=['apple','mango','carrot','banana']メソッド、末尾にshoplistを追加します.append('rice')、ソートshoplist.sort()、del shoplistを削除[i]
 1 # Filename: using_list.py
2 # This is my shopping list
3 shoplist = ['apple', 'mango', 'carrot', 'banana']
4 print 'i have', len(shoplist), 'items to choose'
5 print 'these items are:',
6 for item in shoplist:
7 print item,
8 print '
I also have to buy rice
'
9 shoplist.append('rice')
10 print 'my shoplist now is', shoplist
11 print 'i will sort my list now'
12 shoplist.sort()
13 print 'Sorted shopping list is', shoplist
14 print 'The first item i will buy is', shoplist[0]
15 olditem = shoplist[0]
16 del shoplist[0]
17 print 'i bought the', olditem
18 print 'My shopping list is now', shoplist

i have 4 items to choose
these items are: apple mango carrot banana
I also have to buy rice
my shoplist now is ['apple', 'mango', 'carrot', 'banana', 'rice']
i will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item i will buy is apple
i bought the apple
My shopping list is now ['banana', 'carrot', 'mango', 'rice']

 
tuple:メタグループ.リストと似ていますが、変更できません.zoo = ('wolf', 'elephant', 'penguin') new_zoo = ('monkey', 'dolphin', zoo)
メタグループ印刷文に適用:name='Mike'age=22 print'%s is years old'%(name,age)
1 # Filename: using_tuple.py
2 zoo = ('wolf', 'elephant', 'penguin')
3 print 'number of animals in the zoo is', len(zoo)
4 new_zoo = ('monkey', 'dolphin', zoo)
5 print 'number of animals in the new zoo is', len(new_zoo)
6 print 'all animals in the new zoo are', new_zoo
7 print 'Animals brought from old zoo are', new_zoo[2]
8 print 'last animal brought from old zoo is', new_zoo[2][2]

number of animals in the zoo is 3
number of animals in the new zoo is 3
all animals in the new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))
Animals brought from old zoo are ('wolf', 'elephant', 'penguin')
last animal brought from old zoo is penguin

 
dict:辞書.キー値ペアで構成され、キーは変更できず、値は変更できます.d={key 1:value 1,key 2:value 2}メソッド:ab.items()、ab.has_key('Guido')、del items()メソッドを削除してメタグループのリストを返します
 1 # Filename: using_dict.py
2 ab = {'Swaroop' : '[email protected]',
3 'Larry' : '[email protected]',
4 'Matsumoto' : '[email protected]',
5 'Spammer' : '[email protected]'}
6 print "Swaroop's address is %s" %ab['Swaroop']
7
8 # Adding a key/value pair
9 ab['Guido'] = '[email protected]'
10 print '
There are %d contacts in the address-book
' %len(ab)
11 for name, address in ab.items():
12 print 'Contact %s at %s' %(name, address)
13 if 'Guido' in ab: # ab.has_key('Guido')
14 print "
Guido's address is %s
" %ab['Guido']

Swaroop's address is [email protected]

There are 5 contacts in the address-book

Contact Swaroop at [email protected]
Contact Matsumoto at [email protected]
Contact Larry at [email protected]
Contact Spammer at [email protected]
Contact Guido at [email protected]

Guido's address is [email protected]

 
シーケンス(seq.py)メタグループ、リストはすべてシーケンスです.共通性:インデックスオペレータ、スライスオペレータインデックスは負数であり、末尾からカウントされます.スライス開始位置はシーケンススライス位置にあり、終了位置は除外される
 1 # FIlename: seq.py
2 shoplist = ['apple', 'mango', 'carrot', 'banana']
3 # Indexing or Subscriprion operation
4 print 'Item 0 is', shoplist[0]
5 print 'Item 1 is', shoplist[1]
6 print 'Item 2 is', shoplist[2]
7 print 'Item 3 is', shoplist[3]
8 print 'Item -1 is', shoplist[-1]
9 print 'Item -2 is', shoplist[-2]
10
11 # slicing on a list
12 print 'Item 1 to 3 is', shoplist[1:3]
13 print 'Item 2 to end is', shoplist[2:]
14 print 'Item 1 to -1 is', shoplist[1:-1]
15 print 'Item start to end is', shoplist[:]
16
17 # slicing on a string
18 name = 'swaroop'
19 print 'characters 1 to 3 is', name[1:3]
20 print 'characters 2 to end is', name[2:]
21 print 'characters 1 to -1 is', name[1:-1]
22 print 'characters start to end is', name[:]

Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop

 
参照(c#参照)(reference.py)mylist=shoplist、参照mylist=shoplist[:]、完全コピー
 1 # Filename: reference.py
2 print 'Simple Assignment'
3 shoplist = ['apple', 'mango', 'carrot', 'banana']
4 # mylist is just another name pointed to the same object!
5 mylist = shoplist
6 del shoplist[0]
7 print 'shoplist is', shoplist
8 print 'mylist is', mylist
9 # make a copy by doing a full slice
10 mylist = shoplist[:]
11 del mylist[0]
12 print 'shoplist is', shoplist
13 print 'mylst is', mylist

Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
shoplist is ['mango', 'carrot', 'banana']
mylst is ['carrot', 'banana']

 
文字列strクラスのメソッド(str_methods.py)startwith():文字列が指定された文字列で開始されるかどうかをテストするin:指定された文字列find()が含まれているかどうかをテストする:指定された文字列が文字列内の位置を返し、なければ-1 join()を返します:シーケンスとして使用可能な接続文字列
 1 # Filename: str_methods.py
2 name = 'Swaroop'
3 if name.startswith('Swa'):
4 print 'Yes, the string strats with "Swa"'
5 if 'a' in name:
6 print 'Yes, it contains the string "a"'
7 if name.find('war') != -1:
8 print 'Yes, it contains the string "war"'
9 delimiter = '_*_'
10 mylist = ['Brazil', 'Russia', 'India', 'China']
11 print delimiter.join(mylist)

Yes, the string strats with "Swa"
Yes, it contains the string "a"
Yes, it contains the string "war"
Brazil_*_Russia_*_India_*_China