# In[1]标签:Chapter,motorcycles,简介,list,列表,foods,str,print From: https://www.cnblogs.com/IT-QiuYe/p/17008752.html
""" 3.1 列表是什么? """
bicycles = ['tiek', 'cannondale', 'redline', 'specialized']
print(bicycles[0].title())
print(bicycles[-1])
""" 3.2 修改、添加和删除元素 """
'''3.2.1 修改列表元素'''
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[0])
motorcycles[0] = 'ducati'
print(motorcycles[0])
'''3.2.2 在列表中添加元素'''
motorcycles.append("asdad")
print(motorcycles)
motorcycles.insert(0, 'asdasdasd') # 方法insert()在索引0处添加空间,并将值'asdasdasd'存储到这个地方。
print(motorcycles)
'''3.2.3 从列表中删除元素'''
# 使用 del
del motorcycles[0]
print(motorcycles)
# 使用方法pop()删除元素,并接着使用它的值
motorcycles = ['honda', 'yamaha', 'suzuki']
poped_motorcycles = motorcycles.pop(0)
print(motorcycles)
print(poped_motorcycles)
# 总结:如果你要从列表中删除一个元素,且不再以任何方式使用它,就使用del语句;如果你要在删除元素后还能继续使用它,就使用方法pop()。
'''4. 根据值删除元素'''
motorcycles = ['honda', 'honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.remove('honda') # 方法remove()只删除第一个指定的值
print(motorcycles)
''' 3.3 组织列表 '''
motorcycles = ['honda', 'yamaha', 'suzuki']
test_Sort = ['12', 'asd', '12.3', '123']
'''3.3.1 使用方法 sort()对列表进行永久性排序'''
motorcycles.sort()
print(motorcycles)
test_Sort = ['12', 'asd', '12.3', '123']
test_Sort.sort() # 永久性地修改了列表元素的排列顺序
print(test_Sort) # ['12', '12.3', '123', 'asd']
str_ = 'asdASDgh'
test_Sort.reverse()
str_ = str_.swapcase()
print(str_)
print(test_Sort)
'''3.3.2 使用函数 sorted()对列表进行临时排序'''
motorcycles = ['honda', 'Yamaha', 'yam', 'suzuki']
print(sorted(motorcycles))
print(motorcycles)
print(sorted(motorcycles, reverse=True))
print(motorcycles)
# In[2]
# 倒置字符串1
str_ = 'abcdefghijklmn'
list_str = []
for i in str_:
list_str.append(i)
list_str.reverse()
str_reverse = ''
for i in list_str:
str_reverse += i
print(str_reverse)
# 倒置字符串2
print(str_[::])
print(str_[::-1])
# 倒置字符串3
result = ''
for i in range(len(str_) - 1, -1, -1):
result += str_[i]
print(result)
# In[2]
list_1 = {'string': '1', 'string2': 2}
list_2 = list_1
list_2.update({'string3': '3'})
print(list_1)
print(list_2)
'''
my_foods = ['pizza', 'falafel', 'carrot cake']
#friend_foods = my_foods[:]
# VS
friend_foods = my_foods # 并非把副本存储到 friend_foods
my_foods.append('cannoli')
friend_foods.append('123')
print(friend_foods)
print(my_foods)
'''