Beginning Python From Novice to Professional (3) - 列表操作

列表操作

list函数:

[python] view plaincopy

  1. >>> list(‘hello‘)
  2. [‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘]

改变列表:

[python] view plaincopy

  1. >>> x=[1,1,1]
  2. >>> x[1]=2
  3. >>> x
  4. [1, 2, 1]

删除元素:

[python] view plaincopy

  1. >>> names = [‘wu‘,‘li‘,‘zhao‘,‘qian‘]
  2. >>> del names[1]
  3. >>> names
  4. [‘wu‘, ‘zhao‘, ‘qian‘]

分片赋值:

[python] view plaincopy

  1. >>> name = list(‘perl‘)
  2. >>> name
  3. [‘p‘, ‘e‘, ‘r‘, ‘l‘]
  4. >>> name[2:] = list(‘ar‘)
  5. >>> name
  6. [‘p‘, ‘e‘, ‘a‘, ‘r‘]

[python] view plaincopy

  1. >>> num = [1,2,3,4,5]
  2. >>> num[1:4]=[] #从1位开始但不包括4位
  3. >>> num
  4. [1, 5]

append 列表末尾添加新元素:

[python] view plaincopy

  1. >>> lst = [1,2,3]
  2. >>> lst.append(4)
  3. >>> lst
  4. [1, 2, 3, 4]

count 统计元素的个数:

[python] view plaincopy

  1. >>> [‘we‘,‘have‘,‘we‘,‘a‘,‘dog‘].count(‘we‘)
  2. 2

extend 扩展列表:

[python] view plaincopy

  1. >>> a = [1,2,3]
  2. >>> b = [4,5,6]
  3. >>> a.extend(b)
  4. >>> a
  5. [1, 2, 3, 4, 5, 6]

index 找出第一个匹配项的位置:

[python] view plaincopy

  1. >>> sentence = [‘I‘,‘have‘,‘a‘,‘little‘,‘dog‘]
  2. >>> sentence.index(‘little‘)
  3. 3

insert 将对象插入列表:

[python] view plaincopy

  1. >>> num = [1,2,3,5,6,7]
  2. >>> num.insert(3,‘four‘)
  3. >>> num
  4. [1, 2, 3, ‘four‘, 5, 6, 7]

pop 移除列表元素,默认最后一个:

[python] view plaincopy

  1. >>> x = [1,2,3]
  2. >>> x.pop()
  3. 3
  4. >>> x
  5. [1, 2]
  6. >>> x.pop(0)
  7. 1
  8. >>> x
  9. [2]

结合append:

[python] view plaincopy

  1. >>> x = [1,2,3]
  2. >>> x.append(x.pop())
  3. >>> x
  4. [1, 2, 3]

remove 移除列表中的第一个匹配项:

[python] view plaincopy

  1. >>> x = [‘to‘,‘be‘,‘or‘,‘not‘,‘to‘,‘be‘]
  2. >>> x.remove(‘be‘)
  3. >>> x
  4. [‘to‘, ‘or‘, ‘not‘, ‘to‘, ‘be‘]

reverse 反向存放元素:

[python] view plaincopy

  1. >>> x = [1,2,3]
  2. >>> x.reverse()
  3. >>> x
  4. [3, 2, 1]

sort 排序:

[python] view plaincopy

  1. >>> x = [4,6,2,1,7,9]
  2. >>> x.sort()
  3. >>> x
  4. [1, 2, 4, 6, 7, 9]

[python] view plaincopy

    1. >>> x.sort(reverse=True)
    2. >>> x
    3. [9, 7, 6, 4, 2, 1]
    4. >>> x.sort(reverse=False)
    5. >>> x
    6. [1, 2, 4, 6, 7, 9]
时间: 2024-12-10 03:15:41

Beginning Python From Novice to Professional (3) - 列表操作的相关文章

Beginning Python From Novice to Professional (2) - 命令行运行Python脚本

命令行运行Python脚本 Linux下先创建一个hello.py [python] view plaincopy $ gedit hello.py 输入: [python] view plaincopy #!/usr/bin/env python print 2+2 保存退出,运行: [python] view plaincopy $ python hello.py 4 我们也可以让它变得和普通程序一样执行 执行之前,让脚本文件具备可执行属性: [python] view plaincopy

Beginning Python From Novice to Professional (4) - 演示样本格式字符串

$ gedit price.py #!/usr/bin/env python width = input('Please enter width: ') price_width = 10 item_width = width - price_width header_format = '%-*s%*s' format = '%-*s%*.2f' print '=' * width print header_format % (item_width, 'Item', price_width, 'P

Beginning Python From Novice to Professional (9) - Socket

Socket 小型服务器: #!/usr/bin/env python import socket s = socket.socket() host = socket.gethostname() port = 1234 s.bind((host,port)) s.listen(5) while True: c,addr = s.accept() print 'Got connection from',addr c.send('Thank you for connecting') c.close(

Beginning Python From Novice to Professional (7) - 类

类 创建简单类: #!/usr/bin/env python __metaclass__ = type class Person: def setName(self,name): self.name = name def getName(self): return self.name def greet(self): print "Hello,world! I'm %s." % self.name foo = Person() bar = Person() foo.setName('L

Beginning Python From Novice to Professional (8) - 文件方法

文件方法 读写: #!/usr/bin/env python f = open('somefile.txt','w') f.write('Hello,') f.write('World!') f.close() f = open('somefile.txt','r') print f.read(5) Hello 使用基本文件方法: #!/usr/bin/env python f = open(r'somefile.txt') print f.read() f.close() f = open(r

Beginning Python From Novice to Professional (4) - 字符串格式示例

$ gedit price.py #!/usr/bin/env python width = input('Please enter width: ') price_width = 10 item_width = width - price_width header_format = '%-*s%*s' format = '%-*s%*.2f' print '=' * width print header_format % (item_width, 'Item', price_width, 'P

Beginning Python From Novice to Professional (6) - 函数使用

函数使用 定义函数: #!/usr/bin/env python def hello(name): return 'Hello, ' + name + '' print hello('world') print hello('Gumby') Hello, world Hello, Gumby 斐波那契序列举例: #!/usr/bin/env python def fibs(num): result = [0,1] for i in range(num-2): result.append(resu

Beginning Python From Novice to Professional (5) - 条件与循环

条件与循环 条件执行: name = raw_input('What is your name? ') if name.endswith('Gumby'): print 'Hello, Mr.Gumby' What is your name? Gumby Hello, Mr.Gumby name = raw_input('What is your name? ') if name.endswith('Gumby'): print 'Hello, Mr.Gumby' else: print 'He

Beginning Python From Novice to Professional (1) - 数字和表达式

数字和表达式 加减乘除: >>> 2+2 4 >>> 100-50 50 >>> 3*5 15 >>> 1/2 0 >>> 1.0/2.0 0.5 求余.乘方: >>> 1%2 1 >>> 10%3 1 >>> 2**3 8 >>> -3**2 -9 >>> (-3)**2 9 十六进制.八进制: >>> 0x