首页 > 其他分享 >Chapter_3_列表简介

Chapter_3_列表简介

时间:2022-12-27 18:45:27浏览次数:64  
标签:Chapter motorcycles 简介 list 列表 foods str print

# In[1]
""" 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)
'''

标签:Chapter,motorcycles,简介,list,列表,foods,str,print
From: https://www.cnblogs.com/IT-QiuYe/p/17008752.html

相关文章

  • Chapter_4_操作列表
    #In[1]magicians=['alice','david','carolina']foriinmagicians:print(i)'''4.1.1深入地研究循环'''a=list(range(1,10,2))print(a)print(sum(a))'''----......
  • Chapter_5_if语句
    #In[1]5.2.2检查是否相等时不考虑大小写var='Audi'print(var.lower()=='audi')print(var)#函数lower()不会修改存储在变量car中的值#In[2]5.2.3检查是否不......
  • Java: ParameterizedType用法与简介(转载)
    转载地址:https://blog.csdn.net/sageyin/article/details/114701550https://www.cnblogs.com/baiqiantao/p/7460580.html......
  • Android性能分析工具简介
    在Android项目开发工程中,功能开发只是其中的一部分,更多的时候是优化,优化除了个人的良好习惯,往往还需要借助第三方工具。本文罗列Android优化过程中的一些常用工具借助这些工......
  • OpenCV中轮廓处理简介
    一、OpenCV中的轮廓图像的上半部分是一张白色背景上的测试图像,包含了一系列标记A到E的区域。寻找到的轮廓被标记为cX或hX,其中c代表“轮廓(contour)”,h代表“孔(h......
  • Python中的列表条件求和方法
    列表条件求和方法 list_data=[[1.0,'配件','522422','铝扣板用纽扣','金色','',72.0,'PC',''],[2.0,'配件','500031','十字槽沉头自钻自攻螺钉4......
  • k01_简介
    JAVA简介-介绍各位盟友们大家好:​ 经过很长时间的学习与突破,个人认为经常使用并且观看许多up主以及别人的博客而很少付钱的我,很有必要出一期有关JAVA开发的博客,以供各位......
  • Golang开发项目目录简介以及目录结构设置规范
    一、Golang项目简单介绍Golang简单的目录结构如下:其中,bin用来存放经过gobulid后的可执行文件,pkg存放编译后的gomodule,而src就存放我们项目的代码 二、三种常用目录结......
  • 列表搜索(过滤)
    在有些项目中,一些没分页的搜索功能需要做,搜索一般分为精确搜索和模糊搜索。搜索也要叫过滤,一般用filter来实现。consta=[1,2,3,4,5,7,23,12]constresult=a.filter(i......
  • C++11:列表初始化
    在C++98/03中,对象初始化方法有很多种,如下代码所示://初始化列表inti_arr[3]={1,2,3};//普通数组structA{intx;structB{inti;......