首页 > 编程语言 >python列表(一)

python列表(一)

时间:2023-07-06 22:12:37浏览次数:38  
标签:motorcycles python cars 元素 列表 bicycles print

列表由一系列按特定顺序排列的元素组成。

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)

1.访问列表元素

# 索引
print(bicycles[0])

# 最后一个元素
print(bicycles[-1])

# 倒数第二个元素
print(bicycles[-2])

2.修改、添加和删除元素

2.1 修改元素

# 修改第一个元素的值
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)

2.2 添加元素

# 列表末尾追加元素
motorcycles.append('honda')
print(motorcycles)

# 在任意索引处添加元素,其右元素都右移一位
motorcycles.insert(1, 'yadi')
print(motorcycles)

2.3 删除元素

# 删除列表指定索引的元素
del motorcycles[1]
print(motorcycles)

# 从列表尾部弹出元素,并可获取该元素
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

# 弹出任意位置的元素,并获取该元素
popped_motorcycle = motorcycles.pop(1)
print(motorcycles)
print(popped_motorcycle)

# 根据元素值来删除元素(若有重复的元素,remove只会删除第一个)
motorcycles.remove('yamaha')
print(motorcycles)

3. 组织列表

3.1 排序

# 永久排序
cars = ['bmw', 'audi', 'toyata', 'subaruu']
print(cars)
cars.sort()
print(cars)
# 永久反向排序
cars.sort(reverse=True)
print(cars)

# 临时排序
cars = ['bmw', 'audi', 'toyata', 'subaruu']
print(sorted(cars, reverse=True))
print(sorted(cars))
print(cars)

3.2 倒序(反转)

print(cars)
cars.reverse()
print(cars)

3.3 列表的长度

# 获取列表的长度
len(cars)

标签:motorcycles,python,cars,元素,列表,bicycles,print
From: https://www.cnblogs.com/route/p/17533462.html

相关文章

  • python基础day39 生产者消费者模型和线程相关
    如何查看进程的id号进程都有几个属性:进程名、进程id号(pid--->processid)每个进程都有一个唯一的id号,通过这个id号就能找到这个进程importosimporttimedeftask():print("task中的子进程号:",os.getpid())print("主进程中的进程号:",os.getppid())#parent......
  • 多线程python
    如何开启进程使用的是内置的模块:multiprocessfrommultiprocessingimportProcessdeftask():withopen('a.txt','w',encoding="utf8")asf:f.write('helloworld')#开一个进程来执行task这个任务#如何开进程"""在Wind......
  • Logistic回归模型,python
    代码参考https://blog.csdn.net/DL11007/article/details/129204192?ops_request_misc=&request_id=&biz_id=102&utm_term=logistic%E6%A8%A1%E5%9E%8Bpython&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-1-129204192.142^v......
  • Python中标准输入(stdin)、标准输出(stdout)、标准错误(stdout)的用法
    1.标准输入input()、raw_input()Python3.x中input()函数可以实现提示输入,python2.x中要使用raw_input(),例如:foo=input("Enter:")#python2.x要用raw_input()print("Youinput:[%s]"%(foo))#测试执行Enter:abcdeYouinput:[abcde]#读取一行(不......
  • Python中os.system()、subprocess.run()、call()、check_output()的用法
    1.os.system()os.system()是对C语言中system()系统函数的封装,允许执行一条命令,并返回退出码(exitcode),命令输出的内容会直接打印到屏幕上,无法直接获取。示例:#test.pyimportosos.system("ls-l|greptest")#允许管道符#测试执行$ll<=======......
  • Python中startswith()和endswith()方法
    startswith()方法startswith()方法用于检索字符串是否以指定字符串开头,如果是返回True;反之返回False。endswith()方法endswith()方法用于检索字符串是否以指定字符串结尾,如果是则返回True;反之则返回Falses='helloword'print("s.startswith('wor'):",s.startswith('wor......
  • 【Python】多维列表变为一维列表的方法--numpy
    转载:(18条消息)【Python】多维列表变为一维列表的方法_四维列表变一维_Vincent__Lai的博客-CSDN博客题目给定一个多维列表,怎么让其变为一维?例如,输入:[[1,4],[2],[3,5,6]],输出:[1,4,2,3,5,6]常规一行做法a=[[1,4],[2],[3,5,6]]a=[jforiinaforjini......
  • 将PYTHON包环境从一个电脑拷贝到另外一个电脑
    将PYTHON包环境从一个电脑拷贝到另外一个电脑1、在当前电脑复制D:\ProgramFiles\Python\Python311\Lib中的所有文件生成myrequirement.txt文件pipfreeze>myrequirement.txtmyrequirement.txt文件如下:colorama==0.4.6constantly==15.1.0cpca==0.5.5cryptography=......
  • python pydoc模块生成html网页版内容
    pydoc是一个能生成网页版的模块,内置模块命令:python-mpydoc-p1234-m加载模块-p网页访问端口命令行:b打开浏览器q退出效果:Windows环境下:python-mpydoc-watexit//在当前目录创建atexit.htmlpython-mpydoc-p5000//启动一个Web服务器监听h......
  • CPython, Pypy, MicroPython...还在傻傻分不清楚?
    哈喽大家好,我是咸鱼当我们说Python时,通常指的是官方实现的CPython但还有很多比如Pypy、Jython、MicroPython、Brython、RustPython等“python”许多小伙伴看到这些带“python”的概念可能一头雾水,心想这跟我平时接触到的python有什么区别吗?这些到底是什么那么今天这......