列表和元组—序列
序列:数据元素的集合,数据元素可以是数值、字符串、布尔类型、对象等。 序列的两种类型:列表和元组
name = "Bill"
names = [ "Bill" , "Mary" , "John" ]
values = [ "Bill" , True , False , 10 , 20.5 , [ 1 , 2 , 3 , 4 , 5 ] ]
names = [ "Bill" , "Mike" ]
numbers = [ 1 , 2 , 3 , 4 , 5 ]
salary = [ 3000.0 , 4000.0 , 5000.0 ]
flags = [ True , False , True , True ]
values = [ names, numbers, salary, flags, [ 'a' , 'b' ] ]
for value in values:
print ( value)
['Bill', 'Mike']
[1, 2, 3, 4, 5]
[3000.0, 4000.0, 5000.0]
[True, False, True, True]
['a', 'b']
序列的基本操作-----通过索引获取序列的值
通过一个序号表示一个序列元素值,序号称为索引 索引从0开始,Python语言中,索引可以是负数
names = [ "Bill" , "Mary" , "Jack" ]
print ( names[ 0 ] )
print ( names[ 2 ] )
s = "I love pyhton"
print ( s[ 0 ] )
print ( s[ 7 ] )
print ( "Hello" [ 1 ] )
print ( names[ - 1 ] )
print ( names[ - 2 ] )
print ( names[ - 3 ] )
Bill
Jack
I
p
e
Jack
Mary
Bill
months = [ '一月' ,
'二月' ,
'三月' ,
'四月' ,
'五月' ,
'六月' ,
'七月' ,
'八月' ,
'九月' ,
'十月' ,
'十一月' ,
'十二月'
]
year = input ( "年:" )
month = input ( '月(1-12): ' )
day = input ( '日(1-31): ' )
monthNumber = int ( month)
monthName = months[ monthNumber - 1 ]
print ( year, "年" , monthName, "月" , day, "日" )
年:2028
月(1-12): 5
日(1-31): 24
2028 年 五月 月 24 日
分片(Slicing):从一个序列中获取子序列。
分片有3个参数:startIndex、endIndex和step。 分片可以截取字符串。
url = 'https://geekori.com'
print ( url[ 0 : 5 ] )
print ( url[ 8 : len ( url) ] )
https
geekori.com
numbers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]
print ( numbers[ 1 : 3 ] )
print ( numbers[ 0 : 6 ] )
print ( numbers[ - 3 : - 1 ] )
print ( numbers[ - 3 : 1 ] )
print ( numbers[ 4 : len ( numbers) ] )
print ( numbers[ 4 : ] )
print ( numbers[ 1 : ] )
print ( numbers[ 0 : 5 ] )
print ( numbers[ : ] )
numbers1 = numbers[ : ]
numbers1[ 1 ] = 400
print ( numbers)
print ( numbers1)
print ( numbers[ 0 : : 2 ] )
print ( numbers[ 1 : : 2 ] )
print ( numbers[ len ( numbers) : 2 : - 1 ] )
print ( numbers[ : : - 1 ] )
[2, 3]
[1, 2, 3, 4, 5, 6]
[6, 7]
[]
[5, 6, 7, 8]
[5, 6, 7, 8]
[2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 400, 3, 4, 5, 6, 7, 8]
[1, 3, 5, 7]
[2, 4, 6, 8]
[8, 7, 6, 5, 4]
[8, 7, 6, 5, 4, 3, 2, 1]
序列的乘法
s = "a"
print ( s * 12 )
aaaaaaaaaaaa
numbers = [ 1 , 2 , 3 , 4 , 5 ]
print ( numbers * 10 )
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
line = 10
spaceNum = line // 2
i = 1
lineSpaceNum = spaceNum
triangle = [ ]
while lineSpaceNum >= 0 :
leftSpaceList = [ ' ' ] * lineSpaceNum
starList = [ '*' ] * ( 2 * i - 1 )
rightSpaceList = [ ' ' ] * lineSpaceNum
lineList = leftSpaceList + starList + rightSpaceList
triangle. append( lineList)
lineSpaceNum -= 1
i += 1
for line in triangle:
print ( line)
[' ', ' ', ' ', ' ', ' ', '*', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', '*', '*', '*', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', '*', '*', '*', '*', '*', ' ', ' ', ' ']
[' ', ' ', '*', '*', '*', '*', '*', '*', '*', ' ', ' ']
[' ', '*', '*', '*', '*', '*', '*', '*', '*', '*', ' ']
['*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*']
s = "I love python."
print ( "python" in s)
print ( "you" in s)
True
False
values = [ "Bill" , False , 20 ]
print ( "Bill" in values)
print ( False in values)
print ( True in values)
print ( 20 in values)
True
True
False
True
account = [
[ "geekori" , "123456" ] ,
[ "bill" , "54321" ] ,
[ "superman" , 456 ]
]
username = input ( "账号:" )
password = input ( "密码:" )
if [ username, password] in account:
print ( "登录成功!" )
else :
print ( "登录失败!" )
账号:bill
密码:54321
登录成功!
values = [ int ( "3" ) , True , 10 , 54 , 23 , - 56 , 64 , 12 , 86 , 10.4 ]
print ( len ( values) )
print ( max ( values) )
print ( min ( values) )
print ( max ( 5 , 3 , 1 , 9 ) )
10
86
-56
9
values = [ "Bill" , "Mary" , "John" ]
values[ 0 ] = "Mike"
values[ 1 ] = 10.4
print ( values)
values[ - 1 ] = "Joe"
print ( values)
['Mike', 10.4, 'John']
['Mike', 10.4, 'Joe']
numbers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]
del numbers[ 3 ]
print ( len ( numbers) )
print ( numbers)
7
[1, 2, 3, 5, 6, 7, 8]
names = [ "Bill" , "Joe" , "李宁" , "马云" ]
print ( names[ 3 : ] )
names[ 3 : ] = [ "a" , "b" , "c" ]
print ( len ( names) )
print ( names)
['马云']
6
['Bill', 'Joe', '李宁', 'a', 'b', 'c']
name = list ( "John" )
print ( name)
name[ 2 : ] = list ( "e" )
print ( name)
['J', 'o', 'h', 'n']
['J', 'o', 'e']
print ( numbers)
numbers[ 3 : 6 ] = [ ]
print ( numbers)
[1, 2, 3]
[1, 2, 3]
numbers[ 3 : 6 ] = [ 5 , 6 , 7 , 8 ]
print ( numbers)
[1, 2, 3, 5, 6, 7, 8]
numbers[ 3 : 5 ] = [ ]
print ( numbers)
[1, 2, 3, 7, 8]
numbers[ 3 : 5 ] = [ 6 , 7 , 8 ]
print ( numbers)
[1, 2, 3, 6, 7, 8]
numbers[ 3 : 4 ] = [ 5 ]
print ( numbers)
[1, 2, 3, 5, 7, 8]
numbers[ 4 : 4 ] = [ 6 ]
print ( numbers)
[1, 2, 3, 5, 6, 7, 8]
numbers[ 3 : 6 ] = [ ]
print ( numbers)
[1, 2, 3, 8]
列表方法
1.append:在列表最后插入新的值 2.clear:用于清除列表的内容 3.copy:用于复制一个列表 4.count:用于统计某个元素在列表中出现的次数 5.extend:用于在列表结尾插入另一个列表,也就是让两个列表首尾相连。该方法改变的是被扩展的列表。list1.extend(list2),列表的相加创建一个新的列表。 6.index:用于列表中找出某个值第一次出现的索引位置。 7.insert:用于将值插入到列表中指定的位置 8.pop:用于移除列表中的元素(默认是最后一个元素),并返回该元素的值。 9.remove:用于移除列表中某个值的第一次匹配项。 10.reverse:用于将列表中的元素反向存放。 11.sort:用于对列表进行排序,调用该方法会改变原来的列表。
print ( "------------append方法--------------" )
numbers = [ 1 , 2 , 3 , 4 , 5 ]
numbers. append( 20 )
print ( numbers)
numbers. append( "hello" )
numbers. append( [ 1 , 2 , 3 ] )
print ( numbers)
------------append方法--------------
[1, 2, 3, 4, 5, 20]
[1, 2, 3, 4, 5, 20, 'hello', [1, 2, 3]]
print ( "--------clear方法--------" )
names = [ "Bill" , "Mary" ]
print ( names)
names. clear( )
print ( names)
--------clear方法--------
['Bill', 'Mary']
[]
print ( "----copy方法-----" )
a = [ 1 , 2 , 3 , 4 , 5 ]
acopy = a
acopy1 = a[ : ]
acopy2 = a. copy( )
a[ 3 ] = "hello"
print ( acopy)
print ( acopy1)
print ( acopy2)
----copy方法-----
[1, 2, 3, 'hello', 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
print ( "------count方法-------" )
search = [ "he" , "new" , "he" , "he" , "world" , "peter" , [ 1 , 2 , 3 ] ]
print ( search. count( "he" ) )
print ( search. count( [ 1 , 2 , 3 ] ) )
print ( search. count( 20 ) )
------count方法-------
3
1
0
print ( "----extend方法----" )
a = [ 1 , 2 , 3 ]
b = [ 4 , 5 , 6 ]
a. extend( b)
print ( a)
a[ 2 ] = 123
print ( a)
a = a + b
print ( a)
----extend方法----
[1, 2, 3, 4, 5, 6]
[1, 2, 123, 4, 5, 6]
[1, 2, 123, 4, 5, 6, 4, 5, 6]
x = a
a = a + b
x[ 2 ] = 543
print ( a)
[1, 2, 123, 4, 5, 6, 4, 5, 6, 4, 5, 6]
print ( "------index-------" )
numbers = [ 5 , 3 , 6 , 8 ]
print ( numbers. index( 6 ) )
print ( numbers. index( 10 ) )
------index-------
2
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-11-16750590f639> in <module>
2 numbers = [5,3,6,8]
3 print(numbers.index(6))
----> 4 print(numbers.index(10))
ValueError: 10 is not in list
print ( "----insert方法----" )
numbers = [ 1 , 2 , 3 , 4 , 5 ]
numbers. insert( 3 , "four" )
print ( numbers)
----insert方法----
[1, 2, 3, 'four', 4, 5]
numbers[ 3 : 3 ] = [ "four" ]
print ( numbers)
[1, 2, 3, 'four', 'four', 4, 5]
print ( "---pop方法---" )
numbers = [ 1 , 2 , 3 ]
print ( numbers. pop( ) )
print ( numbers. pop( 1 ) )
print ( numbers)
---pop方法---
3
2
[1]
print ( "---remove方法----" )
words = [ "he" , "new" , "he" , "yes" ]
words. remove( "he" )
print ( words)
---remove方法----
['new', 'he', 'yes']
print ( "---reverse方法----" )
numbers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
numbers. reverse( )
print ( numbers)
---reverse方法----
[7, 6, 5, 4, 3, 2, 1]
print ( "-----sort方法----" )
numbers = [ 4 , 3 , 1 , 7 , 4 , 83 , 2 , - 3 ]
numbers. sort( )
print ( numbers)
-----sort方法----
[-3, 1, 2, 3, 4, 4, 7, 83]
numbers1 = numbers[ : ]
numbers1. sort( )
print ( numbers)
print ( numbers1)
[-3, 1, 2, 3, 4, 4, 7, 83]
[-3, 1, 2, 3, 4, 4, 7, 83]
x = [ 7 , 5 , 4 , 9 , 4 ]
y = sorted ( x)
print ( x)
print ( y)
[7, 5, 4, 9, 4]
[4, 4, 5, 7, 9]
print ( sorted ( "Jackson.H" ) )
['.', 'H', 'J', 'a', 'c', 'k', 'n', 'o', 's']
x = [ 7 , 3 , 9 , 2 , 1 , 12 ]
x. sort( reverse = True )
print ( x)
[12, 9, 7, 3, 2, 1]
元组
元组可以在映射中作为键值使用,而列表是不能作为键值使用 很多内建函数和方法的返回值就是元组,所以在使用这些函数和方法时必须使用元组。 元组也是一种序列,唯一不同的是元组不能修改,元组是只读的。
a = ( )
b = ( 1 , 2 , 3 )
c = 1 , 2 , 3 , 4 , 5
print ( c)
(1, 2, 3, 4, 5)
x = 30 ,
print ( x)
(30,)
print ( ( 12 , ) * 12 )
(12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12)
练习题
1、编写Python程序,通过Python控制台输入若干的整数,直到输入end结束输入(可以使用while或for循环),在输入的过程中,将输入的每一个整数追加到numbers列表中,然后对numbers列表进行降序排列,最后输出numbers列表的元素值。
numbers = [ ]
while True :
numStr = input ( "请输入一个整数:" )
if numStr == "exit" :
break ;
num = int ( numStr)
numbers. append( num)
numbers. sort( reverse = True )
print ( numbers)
请输入一个整数:5
请输入一个整数:8
请输入一个整数:5
请输入一个整数:7
请输入一个整数:9
请输入一个整数:exit
[9, 8, 7, 5, 5]
2、编写Python程序,创建两个列表:numbers1和numbers2,将numbers1中索引从1到3的元素值追加到numbers2列表的结尾,然后对numbers2中的元素值进行升序排列,最后输出numbers中的所有元素值。
numbers1 = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
numbers2 = [ 10 , 20 , 30 ]
numbers2. extend( numbers1[ 1 : 4 ] )
numbers2. sort( reverse = False )
print ( numbers2)
[2, 3, 4, 10, 20, 30]
3、编写列表程序获取列表中的最大值(不要进行排序)
numbers = [ 5 , 4 , 6 , 8 , 9 , 21 ]
print ( max ( numbers) )
print ( min ( numbers) )
21
4
4、编写Python程序,通过Python控制台输入一个大于1的整数n,然后产生一个二维列表。二维列表的尺寸是n * n,每一个列表元素的值从1到n * n,依次排序。例如输入的整数是3,会产生如下的二维列表。 [1,2,3] [4,5,6] [7,8,9] 产生完列表后,会互换二维列表中的行列元素值,如将上面的二维列表互换行列值得结果如下: [1,4,7] [2,5,8] [3,6,9]
numStr = input ( "请输入一个大于1的整数:" )
n = int ( numStr)
i = 1
m = n * n
numbers = [ ]
values = [ ]
while i <= m:
values. append( i)
if i % n == 0 :
numbers. append( values. copy( ) )
values. clear( )
i += 1
for number in numbers:
print ( number)
i = 0
j = 0
while i < n:
while j < n:
numbers[ i] [ j] , numbers[ j] [ i] = numbers[ j] [ i] , numbers[ i] [ j]
j += 1
i += 1
for number in numbers:
print ( number)
请输入一个大于1的整数:4
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]
[13, 14, 15, 16]
[1, 5, 9, 13]
[2, 6, 7, 8]
[3, 10, 11, 12]
[4, 14, 15, 16]