python-リストとメタグループ
17502 ワード
1. python , (sequence).
---- , ( 0 , )
2.
, , 。
test = ['test hello',36]
print test[0] #resuslt is 'test hello'
print test[1] #result is 36
print test #result is ['test hello',36]
test = ['test hello',40]
value = ['value hello',30]
new = [test, value]
print new #restulst [['test hello',40],['value hello',30]]
: , 。 。
, ( )
2.1. 。
, (index), (slicing), (adding), (multplying)
。 ,
2.1.1 (index)
test = 'hello'
print test[0] #resutl is h
( )
print test[-1] #resuslt is o
:
test = raw_input("Year: ")[3] #input 2016
print test # test 6
:
months = [
'January',
'Febuary',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'Decmber'
]
# 1-31
endings = ['st','nd','rd'] + 17 * ['th']\
+['st','nd','rd'] + 7 * ['th']\
+['st']
year = raw_input("Year: ") 1974
month = raw_input("Month (1-12): ") 8
day = raw_input('Day(1-31): ') 16
# -1,
month_name = months[month_number -1]
ordinal = day + endings[day_number -1]
print month_name +' '+ ordinal + ' '+ year
2.1.2.
, 。
test = 'http://www.perl.org'
print test[11:15] #reulst is perl
print test[11:-4] #result is perl ,
test = [1,2,3,4,5,6,7,8,9,10]
print test[3:6] #resutl is 4,5,6
print test[-3:-1] #resutl is 8,9,10
test=[1,2,3,4,5,6,7,8,9,10]
print test[0:10:1] # 0:10 result si 1,2,3,4,5,6,7,8,9,19
print test[1:10:2] # 0:10, ,result is 1,3,5,7,9
print test[1:10:4] # result is 1,5,9
print test[8:3:-1] # 9,8,7,6,5
2.1.3.
[1,2,3] + [4,5,6] #result is [1,2,4,5,6]
'hello' + 'world' #result is helloworld
2.1.4. ( )
'perl' * 5 #'perlperlperlperlperl'
[6] * 5 # result is [6,6,6,6,6]
[]
None python , #perl undef, ,
2.1.5. ( )
test = 'hello'
'h' in test #result is True
'w' in test #result is False
test = ['laomeng','wang','lisi']
raw_input('Enter your name: ') in test
#laomeng result is True
2.1.6. ,
len(),
min(),
max()
test=[1,2,3,4,5,6,7]
len(test) #result is 7
minx(test) #result is 1
max(test) #result is 7
3.
-- , ,
3.1.1.list()
list() ,
list('hello')
['h','e','l','l','o']
3.1.2.
test =['abc','abd','abe','abf']
del test[2] #
3.1.3.
test = list('Perl') #'P','e','r','l'
test[2:] = list('ar') #result is 'P','e','a','r'
test=[1,5]
test[1:1] =[2,3,4] #result is ['1,2,3,4,5]
test =[1,2,3,4,5]
test[1:4]=[] #result is [1,5]
3.2.
test =[1,2,3]
3.2.1.append
test = [1,2,3,4]
append.test[5] #resut is [1,2,3,4,5]
perl
my @test = qw(1 2 3 4);
push @test,5;
print "@test
"; #result is 1 2 3 4 5
3.2.2.count
test = ['a','b','a','b','a','d']
test.count('a') # result is 3
3.2.3.extend
test = [1,2,3,4,5]
b=[5,7,8]
test.extend(b) #rest 1,2,3,4,5,5,7,8
3.3.4.index
test =['abc','abd','abe','abf','abg']
test.index('abe') #result is 2
3.3.5.insert
test = [1,2,3,4,5]
test.insert(3,'hello') #result is 1,2,3,'hello',4,5
3.3.6.pop , ,pop
test = [1,2,3,4,5]
test.pop() #result is 1,2,3,4
test.append(pop()) #result is 1,2,3,4,5
3.3.7.remove
test =['a','b','c','d']
test.remove('a') #result is b,c,d
3.3.8.reverse
x=[1,2,3,4]
x.reverse() #result is 4,3,2,1
x = 'hello'
x.list(reverse()) #result is o,l,l,h
3.3.9.sort
x=[4,6,2,1,7,9];
x.sort() #result is 1,2,4,6.7,9
y =x[:] #:
y.sort() #result is 1,2,4,6.7,9
4. :
1,2,3 #restult is (1,2,3)
() #
4.1.1.tulpe list
tulpe([1,2,3]) #result is (1,2,3)
4.1.2
x = 1,2,3
x[1] #result is 2
x[0:2] #result is 1,2
:
: , ( 0 ).
, , 。
。 ( ),
。
。in 。