1、在列表中添加对象
vowels = ['a','e','i','o','u']
word = input("please input:")
found = []
for letter in word:
if letter in vowels:
if letter not in found:
found.append(letter)
for vowel in found:
print(vowel)
2、删除指定数据值(第一次出现):
nums = [1,2,3,4]
nums.remove(3)
从现有的列表中删除和返回一个对象
1):不指定索引值
nums = [1,2,3,4]
nums.pop()
2):指定索引值
nums = [1,2,3,4]
nums.pop(0)
3、将对象增加到现有列表
nums = [1,2,3,4]
nums.extend([5,6])
4、在列表中插入一个对象
nums = [1,2,3,4]
nums.insert(0,10)
5、复制列表
first = [1,2,3,4,5]
second = first.copy()
second.append(7)
6、列表支持负索引值(从右向左计数)
saying = "12345"
letters = list(saying)
print(letters)
print(letters[-1])
7、列表切片[开始值:结束值:步长值]
print(letters[0:5:2])
8、将数组转换为字符串
print("".join(letters[0:5]))
9、for循环切片实例
paranoid_android = "Marvin,the Paranoid Android"
letters = list(paranoid_android)
for char in letters[:6]:
print('\t',char)
print()
for char in letters[-7:]:
print('\t'*2,char)
print()
for char in letters[12:20]:
print('\t'*3,char)