目录:

     返回顶部

1、列表基本操作

1. 列表赋值 

a = [1,2,3,4,5,6,7,8]
a[0] = 100                                #the result : [100, 2, 3, 4, 5, 6, 7, 8]

 2. 元素删除

a = [1,2,3,4,5,6,7,8]
del a[0]                                 #the result : [2, 3, 4, 5, 6, 7, 8]

 3. 分片赋值

a = [1,2,3,4,5,6,7,8]
a[::2]                                   # [1, 3, 5, 7]
a[-2:]                                   # [7, 8]
a[1:1] = [0,0,0]                         # the result : [1, 0, 0, 0, 2, 3, 4, 5, 6, 7, 8]

4. 使用 for i in range(10,-1,-1) 生成列表

for i in range(10,-1,-1): # 开始位置(10),结束位置(-1), 步长(-1)
    print i,
# 打印结果:10 9 8 7 6 5 4 3 2 1 0 # 从10开始,每次向后取一个值,直到遇到结束位置 -1

2、列表方法

1. append

   作用:append用于在列表末尾追加新的对象

a = [1,2,3]
a.append(4)                           #the result : [1, 2, 3, 4]

 2. count

  作用:count方法统计某个元素在列表中出现的次数

a = ['aa','bb','cc','aa','aa']
print(a.count('aa'))                                    #the result : 3

3. extend

  作用:extend方法可以在列表的末尾一次性追加另一个序列中的多个值

a = [1,2,3]
b = [4,5,6]
a.extend(b)                    #the result :[1, 2, 3, 4, 5, 6]

4. index

  作用:index函数用于从列表中找出某个值第一个匹配项的索引位置

a = [1,2,3,1]
print(a.index(1))                                    #the result : 0

5. insert

  作用: insert方法用于将对象插入到列表中

a = [1,2,3]
a.insert(0,'aa')            #the result : ['aa', 1, 2, 3]

6. pop

  作用:pop方法会移除列表中的一个元素(默认是最后一个),并且返回该元素的值

a = [1,2,3]
a.pop()                                                 #the result : [1, 2]
a.pop(0)

7. remove

  作用:remove方法用于移除列表中某个值的第一个匹配项

a = ['aa','bb','cc','aa']
a.remove('aa')                       #the result : ['bb', 'cc', 'aa']

8. reverse

  作用:reverse方法将列表中的元素反向存放

a = ['a','b','c']
a.reverse()                  #the result : ['c', 'b', 'a']

9. sort

  作用:sort方法用于在原位置对列表进行排序,意味着改变原来的列表,让其中的元素按一定顺序排列

a = ['a','b','c',1,2,3]
a.sort()                        #the result :[1, 2, 3, 'a', 'b', 'c']

 10.  enumrate

li = [11,22,33]
for k,v in enumerate(li, 1):
    print(k,v)

11. range和xrange        指定范围,生成指定的数字

  注:python3中的range类似python2中的xrange,比如a = range(1,4) : a返回的不是列表对象而是一个可迭代对象(<class 'range'>)

#1、range根据start与stop指定的范围以及step设定的步长,生成一个序列:range([start,] stop[, step])
#2、xrange 用法与 range 完全相同,所不同的是生成的不是一个list对象,而是一个生成器
for i in range(1,10,2):
    print(i)

12. 列表去空

法1:
filter(None, your_list)

法2:
while '' in your_list:
    your_list.remove('')

法3:
your_list = [x for x in your_list if x != '']

3、元组

  元组定义:元组和列表一样,也是一种序列,唯一的不同是元组不能修改。

1. 创建元组举例

#1. 创建元组
a = (1,2,3,4,5,6)
#2. 将列表转换成元组
tuple([1,2,3,4])                                    #the result : (1, 2, 3, 4)

4、列表和元组常用函数

  com(x,y)                                                                                                  比较两个值

  len(seq)                                                                                                    返回序列的长度

  list(seq)                                                                                                    把序列转换成列表

  max(args)                                                                                                         返回序列或者参数集合中得最大值

  min(args)                                                                                                 返回序列或者参数集合中的最小值

  reversed(seq)                                                                                         对序列进行反向迭代

  sorted(seq)                                                                                             返回已经排列的包含seq 所有元素的列表

  tuple(seq)                                                                                                把序列转换成元组

     返回顶部

1、字符串格式化

1. 使用百分号(%)字符串格式化

num = 100
print("%d to hex is %x" %(num, num))        #100 to hex is 64
print("%d to hex is %#x" %(num, num))       #100 to hex is 0x64

2. 使用format字符串格式化

#1. 位置参数
print("{0} is {1} years old".format("tom", 28))           #tom is 28 years old
print("{} is {} years old".format("tom", 28))             #tom is 28 years old
print("Hi, {0}! {0} is {1} years old".format("tom", 28)) #Hi, tom! tom is 28 years old

#2. 关键字参数
print("{name} is {age} years old".format(name = "tom", age = 28))    #tom is 28 years old

#3. 下标参数
li = ["tom", 28]
print("{0[0]} is {0[1]} years old".format(li))          #tom is 28 years old

 2、字符串方法

1. find方法

  作用:find方法可以在一个较长的字符串中查找子串,他返回子串所在位置的最左端索引,如果没有找到则返回-1

a = 'abcdefghijk'
print(a.find('abc'))                           #the result : 0
print(a.find('abc',10,100))                    #the result : 11  指定查找的起始和结束查找位置

2. join方法

  作用:join方法是非常重要的字符串方法,他是split方法的逆方法,用来连接序列中的元素,并且需要被连接的元素都必须是字符串。

a = ['1','2','3']
print('+'.join(a))                                    #the result : 1+2+3

3. split方法

  作用:这是一个非常重要的字符串,它是join的逆方法,用来将字符串分割成序列

print('1+2+3+4'.split('+'))                            #the result : ['1', '2', '3', '4']

4. strip

  作用:strip 方法返回去除首位空格(不包括内部)的字符串

print("   test   test    ".strip())                #the result :“test   test”

5. replace

  作用:replace方法返回某字符串所有匹配项均被替换之后得到字符串

print("This is a test".replace('is','is_test'))     #the result : This_test is_test a test

6、首字母大写

>>> s = 'aBdkndfkFFD'
>>> s.capitalize()
'Abdkndfkffd'

7、Pinyin 模块,将汉字转换成拼音

#! /usr/bin/env python
# -*- coding: utf-8 -*-
from xpinyin import Pinyin

while True:
    p = Pinyin()
    fullname = raw_input('name:').strip()
    fullname = fullname.decode('utf8')
    print fullname
    xin = fullname[0]
    ming = fullname[1:]
    name = ming + '.' + xin
    username = p.get_pinyin(name, '')
    print username
    print username + '@yiducloud.cn'
汉字转拼音

相关文章:

  • 2021-10-04
  • 2021-12-13
  • 2021-10-09
猜你喜欢
  • 2021-04-11
  • 2022-12-23
  • 2021-08-13
  • 2021-11-06
  • 2021-10-11
  • 2022-01-12
相关资源
相似解决方案