python中的序列(列表,字符串)

"""列表(list)是值的序列。在字符串中这些值是字符;在列表中,它可以是任何类型。
列表中的值称为元素(element),有时也称为列表项(item)"""
list = [10,23,'sdlfkdsjf','ram bladder']
"""列表是可变的"""
list[3] = 'aban'  # [10, 23, 'sdlfkdsjf', 'aban']
"""遍历一个列表"""
number = [13,3,2,"lsdf"]
for i in range(len(number)):
    number[i] = number[i]*2
print(number)
# 运行结果:[26, 6, 4, 'lsdflsdf']
"""虽然列表可以包含其他的列表,嵌套的列表仍可被看作一个单独的元素。"""
len([23,[2,'3232'],[23,2332]])  # 长度为3
"""###########列表操作#################"""
a = [23,3,3]
b = [23,2,3]
c = a+b       # c = [23, 3, 3, 23, 2, 3]
d = [3,2,1]*3 # d = [3, 2, 1, 3, 2, 1, 3, 2, 1]
t = ['a','b','c','d','d']
e = t[1:3]  # slice 包含左不包含右 e = ['b', 'c']
f = t[:4]    # f = ['a', 'b', 'c', 'd']
"""append可以在尾部添加新的元素,append修改列表,返回None
#########一次只能添加一个元素#########"""
g = ['b',',','d']
g.append('skdlfjsldkf') # g = ['b', ',', 'd', 'skdlfjsldkf']
"""extend方法接收一个列表作为参数,并将其所有的元素附加到列表中"""
t1 = ['ds','ksd','df']
t2 = [2,3,5,4,6,7,8.9,0,9]
t1.extend(t2)
#  t2 = [2, 3, 5, 4, 6, 7, 8.9, 0, 9]
#  t1 = ['ds', 'ksd', 'df', 2, 3, 5, 4, 6, 7, 8.9, 0, 9]
"""列表中的大多数方法无返回值"""
t = ['a','b','c','d','sdf']
t.sort()
print(t)      # t =  ['a', 'b', 'c', 'd', 'sdf']
b = t.sort()  # b = None
"""Map filter and reduce"""
def add_all(t):
    total = 0
    for x in t:
        total += x
    return total
t = [1,2,3,4,5,6,7,8,9,10,0]
b = add_all(t)     # b = 55
a = sum(t)         # a = 55
######capitalize()方法返回一个首字母大写的字符串
def capitalize_all(t):  # 亦称作映射(map)
    res = []
    for a in t:
        res.append(t.capitalize())
    return res
capitalize_all("sdkfDDF")
# res = ['Sdkfddf', 'Sdkfddf', 'Sdkfddf', 'Sdkfddf', 'Sdkfddf', 'Sdkfddf', 'Sdkfddf']
"""返回只包含大写字母的字符串"""
def only_upper(t):
    res = []
    for s in t:
        if s.isupper():
            res.append(s)
    return res
only_upper("sldkfjFSdkKsSKS")
# res = ['F', 'S', 'K', 'S', 'K', 'S']
"""删除元素,有pop,del;pop删除并返回删除的元素;del直接删除元素;remove删除对应元素"""
t = ['a','b','c']
x = t.pop(1)
# t = ['a','c'], x = b
t = ['a','b','c']
del t[1]     # t = ['a','c']
t = ['a','b','c']
t.remove('b')  # t = ['a','c']
t = ['a','b','c','d','e','f']
del t[1:5]    # t = ['a','f']
"""字符串是字符的序列,而列表是值的序列,但字符的列表和字符串并不相同。
若要将一个字符串转换为一个字符的列表,可以使用函数list"""
s = "spam"
t = list(s)     # t = ['s', 'p', 'a', 'm']
u = 'pining for fjords'
t = u.split(' ')  # 等价于 t = u.split()
# t = ['pining', 'for', 'fjords']
'''join是split的逆操作,它接收字符串列表,并拼接每个元素。
join是字符串方法,所以必须在分隔符上调用第,并传入列表作实参'''
t = ['pining','for','the','fjords']
delimiter = ' '
t[3] = 'fjords!'
s = delimiter.join(t)
# s = pining for the fjords!   t = ['pining', 'for', 'the', 'fjords!']
print(s)
"""对象和值"""
a = 'banana'
b = 'banana'
a is b       # True
"""在上一个例子中,python只创建了一个字符串对象,而a,b都引用它
但是,当你创建两个列表时,会得到两个对象"""
a = [1,3,4]
b = [1,3,4]
a is b       # False
"""别名----如果a引用一个对象,而你赋值b=a,则两个变量都会引用同一个对象"""
a = [1,3,3]
b = a
b is a        # True
#########
t1 = [2,3,4]
t3 = t1 + [3]
# t1 = [2, 3, 4]   t3 = [2,3,4,3]
"""操作符+创建一个新列表,而原始列表并不改变"""
def had_delete_head(t):
    t = t[1:]
    print(t)
t4 = [2,3,2,5]
had_delete_head(t4)   # [3,2,5]
print(t4)      # [2,3,2,5]
#######################
def tail(t):
    return t[1:]
letters = ['a','b','c']
rest = tail(letters)
print(rest,letters)    # ['b', 'c'] ['a', 'b', 'c']
"""调试"""
word = "fdskdf'sfdf"
word= word.strip('f')
print(word)   # dskdf'sfd


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值