Python – 列表和任务

python - 列表和任务

学习索引和切片之后,我们开始更多地了解列表和内置方法。方法有

不返回

追加插入删除排序反转清晰

返回整数

索引数数

返回str

立即学习“Python免费学习笔记(深入)”;

流行

对于交付列表的较小更改,内置功能本身就足够了。但是当我们想要对列表进行更多操作时,就需要for循环、if循环。

例如,有一个列表 [‘pencil’, ‘notebook’, ‘marker’, ‘highlighter’, ‘glue stick’, ‘eraser’, ‘laptop’, 3],其中只有字符串需要转换为大写。这里我们不能直接使用列表或字符串方法。理想的逻辑是

我们必须逐一迭代列表检查项目是字符串还是其他类型相应地使用字符串方法 upper() 和append()

所以这里for循环,需要if,else条件

delivery_list= ['pencil', 'notebook', 'marker', 'highlighter', 'glue stick', 'eraser', 'laptop', 3]upper_list = []for item in delivery_list:    if type(item) == str:        upper_list.append(item.upper())    else:        upper_list.append(item)print('upper case list is:', upper_list)output:upper case list is: ['pencil', 'notebook', 'marker', 'highlighter', 'glue stick', 'eraser', 'laptop', 3]

给出的任务是更多地练习这些方法和逻辑。找到可用的逻辑或方法很有趣。
解决的细节是

tasks_list.py########################################################################################1. create a list of five delivery items and print the third item in the list. #eg: [“notebook”, “pencil”, “eraser”, “ruler”, “marker”]#######################################################################################delivery_list = ['notebook', 'pencil', 'eraser', 'ruler', 'marker']print (f't1. third item in the list{delivery_list} is: {delivery_list[2]}')######################################################################################## 2.a new delivery item “glue stick” needs to be added to the list. # add it to the end of the list and print the updated list.#######################################################################################delivery_list.append('glue stick')print('t2. updated delivery list is:', delivery_list)######################################################################################## 3. insert “highlighter” between the second and third items and print the updated list.#######################################################################################delivery_list.insert(2, 'highlighter')print('t3. updated list by inserting highlighter b/w 2nd &3rd:', delivery_list)######################################################################################## 4. one delivery was canceled. remove “ruler” from the list and print the updated list.#######################################################################################delivery_list.remove('ruler')print('t4. updated list after removing ruler is:', delivery_list)######################################################################################## 5. the delivery man needs to deliver only the first three items. # print a sublist containing only these items.#######################################################################################sublist =[]for item in delivery_list[:3]:    #sublist =sublist.append(item)      #this is incorrect as sublist.append() returns none.    sublist.append(item)print('t5. sublist of first three elements using loop:', sublist) print('t   sublist of first three elements using slicing:', delivery_list[:3]) ######################################################################################## 6.the delivery man has finished his deliveries. # convert all item names to uppercase using a list comprehension and print the new list.#######################################################################################uppercase_list=[]for item in delivery_list:    uppercase_list.append(item.upper())print('t6. uppercase list of delivery items:', uppercase_list)uppercase_list_lc = [item.upper() for item in delivery_list]print('t6. uppercase list using list compre:', uppercase_list_lc)######################################################################################## 7. check if “marker” is still in the list and print a message indicating whether it is found.# 8. print the number of delivery items in the list.#######################################################################################is_found= delivery_list.count('marker')if 'marker' in delivery_list:    print(f't7. marker is found {is_found} times')else:    print(f't7. marker is not found {is_found}')print(f't8. number of delivery item from {delivery_list} is: ', len(delivery_list))######################################################################################## 9. sort the list of items in alphabetical order and print the sorted list# 10. the delivery man decides to reverse the order of his deliveries. # reverse the list and print it#######################################################################################delivery_list.sort()print(f't9. sorted delivery list is {delivery_list}')delivery_list.reverse()print(f't10. reverse list of delivery list is {delivery_list}')######################################################################################## 11. create a list where each item is a list containing a delivery item and its delivery time. # print the first item and its time.#######################################################################################delivery_list_time = [['letter', '10:00'], ['parcel', '10:15'], ['magazine', '10:45'], ['newspaper', '11:00']]print('t11. first delivery item and time', delivery_list_time[0])delivery_list = ['pencil', 'notebook', 'marker', 'highlighter', 'glue stick', 'eraser', 'laptop']delivery_time = ['11:00','11:20','11:40','12:00','12:30','13:00', '13:30']#paring of two list using zippaired_list=list(zip(delivery_list,delivery_time))print('tpaired list using zip:', paired_list[0:2])#combine corresponding item from multiple list using zip and foritem_time_list = [[item,time] for item, time in zip(delivery_list, delivery_time)]print ('titem_time_list using list comprehension: ', item_time_list[0:1])######################################################################################## 12. count how many times “ruler” appears in the list and print the count.# 13. find the index of “pencil” in the list and print it.# 14. extend the list items with another list of new delivery items and print the updated list.# 15. clear the list of all delivery items and print the list.# 16. create a list with the item “notebook” repeated three times and print the list.# 17. using a nested list comprehension, create a list of lists where each sublist contains an item # and its length, then print the new list.# 18. filter the list to include only items that contain the letter “e” and print the filtered list.# 19. remove duplicate items from the list and print the list of unique items.#######################################################################################is_found= delivery_list.count('ruler')print('t12. the count of ruler in delivery list is:', is_found)index_pencil = delivery_list.index('pencil')print(f't13. index of pencil in {delivery_list} is {index_pencil}')small_list = ['ink','']delivery_list.extend(small_list)print('t14. extend list by extend:', delivery_list)item_time_list.clear()print('t15. clearing the list using clear():', item_time_list)notebook_list = ['notebook']*3print('t16. notebook list is:', notebook_list)#filter the list with e letterdelivery_listnew_delivery_list = []for item in delivery_list:    if 'e' in item:        new_delivery_list.append(item)print ('t18. filtered list with items containing e:', new_delivery_list)new_list_compre = [item for item in delivery_list if 'e' in item]print ('t18. filtered list by list comprehension:', new_list_compre)#remove duplicate itemsdelivery_list.extend(['ink', 'marker'])print('t   ', delivery_list)for item in delivery_list:    if delivery_list.count(item) > 1:        delivery_list.remove(item)print('t19. duplicate remove list:',delivery_list)print('t19. duplicate remove list:',list(set(delivery_list)))######################################################################################## 17. using a nested list comprehension, create a list of lists where each sublist contains an item # and its length, then print the new list.########################################################################################without list comprehensionnested_list = []for item in delivery_list:    nested_list.append([item, len(item)])print('t17. ', nested_list[-1:-6:-1])#using list comprehension printing nested listnested_list = [[item,len(item)] for item in delivery_list]print('t17. nested list with length:', nested_list[:5])

答案:

PS C:ProjectsPythonSuresh> python Tasks_list.py        1. Third item in the list['Notebook', 'Pencil', 'Eraser', 'Ruler', 'Marker'] is: Eraser        2. Updated delivery list is: ['Notebook', 'Pencil', 'Eraser', 'Ruler', 'Marker', 'Glue Stick']        3. Updated list by inserting Highlighter b/w 2nd &3rd: ['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Ruler', 'Marker', 'Glue Stick']        4. Updated list after removing Ruler is: ['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Marker', 'Glue Stick']        5. Sublist of first three elements using loop: ['Notebook', 'Pencil', 'Highlighter']           Sublist of first three elements using slicing: ['Notebook', 'Pencil', 'Highlighter']        6. Uppercase list of delivery items: ['NOTEBOOK', 'PENCIL', 'HIGHLIGHTER', 'ERASER', 'MARKER', 'GLUE STICK']        6. Uppercase list using list compre: ['NOTEBOOK', 'PENCIL', 'HIGHLIGHTER', 'ERASER', 'MARKER', 'GLUE STICK']        7. Marker is found 1 times        8. Number of delivery item from ['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Marker', 'Glue Stick'] is:  6        9. Sorted delivery list is ['Eraser', 'Glue Stick', 'Highlighter', 'Marker', 'Notebook', 'Pencil']        10. Reverse list of delivery list is ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser']        11. First delivery item and time ['Letter', '10:00']        Paired list using zip: [('Pencil', '11:00'), ('Notebook', '11:20')]        Item_time_list using list comprehension:  [['Pencil', '11:00']]        12. The count of Ruler in delivery list is: 0        13. Index of Pencil in ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser', 'Laptop'] is 0        14. Extend list by extend: ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser', 'Laptop', 'Ink', '']        15. Clearing the list using clear(): []        16. Notebook list is: ['Notebook', 'Notebook', 'Notebook']        18. Filtered list with items containing e: ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser']        18. Filtered list by list comprehension: ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser']            ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser', 'Laptop', 'Ink', '', 'Ink', 'Marker']        19. Duplicate remove list: ['Pencil', 'Notebook', 'Highlighter', 'Glue Stick', 'Eraser', 'Laptop', '', 'Ink', 'Marker']        19. Duplicate remove list: ['', 'Ink', 'Pencil', 'Notebook', 'Marker', 'Eraser', 'Laptop', 'Highlighter', 'Glue Stick']        17.  [['Marker', 6], ['Ink', 3], ['', 0], ['Laptop', 6], ['Eraser', 6]]        17. Nested list with length: [['Pencil', 6], ['Notebook', 8], ['Highlighter', 11], ['Glue Stick', 10], ['Eraser', 6]]

以上就是Python – 列表和任务的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1348669.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月13日 12:14:37
下一篇 2025年12月13日 12:14:57

相关推荐

  • 在 Python 中使用 SQLAlchemy 创建关系

    当尝试创建 sql 表时,sqlalchemy 可以帮助完成 python 中所需的许多任务,其中之一就是创建关系。 使用 sqlalchemy 创建关系比仅使用 sql 容易得多。它通过更易于遵循的语法和更少的步骤来简化流程。 sqlalchemy 已导入 python,所有快捷语法都可以使用。 …

    好文分享 2025年12月13日
    000
  • Python:全面介绍

    Python 是一种高级解释型编程语言,以其简单性、可读性和多功能性而闻名。 Python 由 Guido van Rossum 创建并于 1991 年首次发布,现已成为世界上最流行的编程语言之一。其设计理念强调代码可读性和显着缩进的使用,使其成为初学者和经验丰富的开发人员的理想选择。Python …

    2025年12月13日
    000
  • python怎么加入库

    Python 中导入库有两种方法:使用 import 语句导入整个库。使用 from … import … 语句导入特定模块或函数。选择导入方法时,考虑代码的可读性和性能:需要导入大量模块或函数时使用 import,需要一个或几个特定模块或函数时使用 from ……

    2025年12月13日
    000
  • python怎么更改安装路径

    Python 的安装路径可以根据系统进行更改:Windows:在自定义安装中指定目标文件夹。macOS:在自定义安装中选择安装位置。Linux:在配置安装时使用 –prefix 选项指定自定义路径。 如何在 Python 中更改安装路径 引言Python 的默认安装路径通常是各个操作系统…

    2025年12月13日
    000
  • python怎么调颜色

    Python中调整图像颜色的方法:使用Pillow库进行亮度、对比度和饱和度调节。使用NumPy和OpenCV进行颜色空间转换和掩蔽。使用PIL库进行逐像素颜色调整和滤镜应用。使用recolor()函数进行颜色量化重新着色。使用colorcet库提供高质量颜色图进行自定义配色。 Python中调整颜…

    2025年12月13日
    000
  • python怎么修改代码

    修改 Python 代码分为以下步骤:识别需要修改的代码部分。完成所需的修改,包括更正错误、添加功能或优化代码。保存更改并测试程序以验证修改。提交更改至版本控制系统(团队项目)。持续检查代码并进行改进。 如何修改 Python 代码 修改 Python 代码至关重要,以修复错误、添加新功能或提升性能…

    2025年12月13日
    000
  • python怎么换字体颜色

    Python 中更改字体颜色有两种方法:使用 ANSI 转义序列和使用 colorama 等第三方库。通过 ANSI 转义序列,可以使用控制台终端的特殊字符序列更改颜色,例如 “33[1;31m” 表示红色。colorama 库提供了更方便的 API,如 Fore.RED 表…

    2025年12月13日
    000
  • python怎么把图片放进窗口

    通过使用 Tkinter 库,可以将图片放入 Python 窗口中。具体步骤包括:导入 Tkinter 库;创建一个窗口;创建一个图像对象;创建一个标签并设置图像;启动主事件循环。 如何在 Python 中将图片放入窗口 在 Python 中,可以通过使用 Tkinter 库将图片放入窗口中。以下是…

    2025年12月13日
    000
  • python怎么打开白色窗口

    可以使用 Tkinter、Pyglet 或 PyQt 在 Python 中创建白色背景的窗口。具体方法包括:使用 Tkinter 的 config(background=”white”) 方法;使用 Pyglet 的 set_clear_color(255, 255, 255…

    2025年12月13日
    000
  • python怎么改成白色

    Python 终端默认背景色为黑色,要更改为白色,可执行以下步骤:通过命令行安装 colorlog 并在 shell 中设置环境变量。打开 IDLE 并配置“终端 Shell”部分的背景颜色。使用其他终端仿真器(如 Cmder 或 iTerm2)调整背景色选项。 如何将 Python 终端改成白色 …

    2025年12月13日
    000
  • python怎么把背景改为黑色

    要在 Python 中将图表背景颜色更改为黑色,请执行以下步骤:导入 Matplotlib 库。创建 Matplotlib Figure。使用 set_facecolor() 方法设置背景颜色。绘制图表数据。使用 show() 方法显示图表。 如何将 Python 图表的背景颜色更改为黑色 在 Py…

    2025年12月13日
    000
  • python怎么读出txt文件

    Python 中读取 txt 文件的方法:使用 open() 函数读取整个文件的内容。使用 readlines() 函数将文件按行读取,存储在列表中。使用 readline() 函数逐行读取文件,每次返回一行。 如何使用 Python 读取 txt 文件 Python 中读取 txt 文件非常简单,…

    2025年12月13日
    000
  • python怎么把背景换成黑色

    可以使用 OpenCV 库将图像背景设为黑色,步骤为:导入 OpenCV。读取图像。创建与图像大小相同的黑色背景。分离前景,使用二值化阈值分离图像中的前景与背景。查找前景区域的轮廓。将前景区域绘制到黑色背景上。保存结果。 如何在 Python 中将图像背景设为黑色 在 Python 中,可以使用 O…

    2025年12月13日
    000
  • python怎么改白色背景

    Python GUI 中的背景颜色通常为白色,但可以通过更改窗口、控件或画布背景来更改。更改窗口背景:使用 configure() 方法设置背景颜色。更改控件背景:使用 config() 方法配置控件背景。自定义画布背景:使用 Canvas 控件创建自定义图形并控制背景颜色。 如何更改 Python…

    2025年12月13日
    000
  • python怎么读写txt

    如何用 Python 读写 TXT 文件?读取 TXT 文件:导入库:import os打开文件:with open(“text.txt”, “r”) as f: content = f.read()写入 TXT 文件:导入库:import os创建文…

    2025年12月13日
    000
  • idlepython怎么运行

    在 Windows 或 Mac 系统上,通过以下步骤在 IDLE 中运行 Python 代码:打开 IDLE。创建新文件(可选)。输入 Python 代码。使用 F5 快捷键或“运行”菜单运行代码。使用调试器(可选)查找并修复错误。 如何运行 idlepython 打开 IDLE 在 Windows…

    2025年12月13日
    000
  • idle python怎么安装

    要安装 IDLE Python,请访问官方网站、选择与您操作系统匹配的版本、按照提示进行安装,然后验证安装是否成功。IDLE 是一个轻量级的 Python 集成开发环境,非常适合初学者和脚本编写。 如何安装 IDLE Python 步骤 1:访问官方网站 前往 Python 官方网站 https:/…

    2025年12月13日
    000
  • python怎么下载到d盘

    在D盘下载Python的步骤:1.访问Python官网;2.选择Windows下载;3.选择D盘保存;4.运行安装程序并选择D盘安装;5.验证安装版本。 如何在 D 盘下载 Python 下载 Python 到 D 盘是一个简单的过程,可以分以下几个步骤进行: 1. 访问 Python 官方网站 访…

    2025年12月13日
    000
  • python怎么安装到桌面

    将 Python 安装到 Windows 桌面步骤:1.下载安装程序并选择版本;2.运行安装程序并勾选 “将 Python 添加到 PATH”;3.验证安装通过命令提示符;4.(可选)将 Python 图标添加到桌面即可。 如何将 Python 安装到 Windows 桌面 …

    2025年12月13日
    000
  • python怎么下载软件包

    Python 使用 pip 下载软件包,具体步骤包括:安装 pip。使用 pip install 命令下载软件包。用 pip list 命令检查安装情况。 如何使用 Python 下载软件包 Python 提供了一种简单的方法来下载和安装软件包,这些软件包包含代码库和模块,可用于执行各种任务。 步骤…

    2025年12月13日
    000

发表回复

登录后才能评论
关注微信