Python学习笔记:List、Tuple、for循环

这篇具有很好参考价值的文章主要介绍了Python学习笔记:List、Tuple、for循环。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.list

list_demo = [7, 7, 8, 9, 9, 9]
print(list_demo.index(7))       # index 方法返回第一个index
list_demo.sort()                # 排序list
list_demo.reverse()             # 倒序list
list_demo1 = list_demo.copy()   # 复制list

Python学习笔记:List、Tuple、for循环,Python,学习,笔记 

2.matrix

其实就是list嵌套,可以使用双重for遍历,所包含方法与list一致

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[0][0])
for row in matrix:
    for i in row:
        print(i)
1
1
2
3
4
5
6
7
8
9

Process finished with exit code 0

3.for循环实例:随机生成一个固定大小的list,并找到list中最大的数和对应的index

import random
list_demo = []
for i in range(10):
    list_demo.append(random.randint(0, 100))
max_num = list_demo[0]
print(list_demo)
for i in list_demo:
    if i >= max_num:
        max_num = i
print(f"Max_Num = list_demo[{list_demo.index(max_num)}] = {max_num}")

4.for循环实例:删除list中重复的元素

list_num = [1, 1, 2, 3, 4, 5, 3, 1]
list_copy = []
for i in list_num:
    if i not in list_copy:
        list_copy.append(i)
print(list_copy)

 5.tuple

tuple不可变,但是可以多个tuple拼接组合

tuple_demo = (1, 2, 3, 1, 1)        # tuple不可变
tuple_demo.index(1)
tuple_demo.count(1)
coordinates = (1, 2, 3)
x, y, z = coordinates               # python优势,string\list\tuple等都可以
msg = 'string'
a, b, c = msg[0:3]
print()

6.dictionary

{key:value}

dic_demo = {"name": "abc", "age": 25, "is_verified": True, "birth_year": 1998}
print(dic_demo["name"])
print(dic_demo.get("age"))
print(dic_demo.get("1sad4"))
print(dic_demo.get("birth_year"))
print(dic_demo)
abc
25
None
1998
{'name': 'abc', 'age': 25, 'is_verified': True, 'birth_year': 1998}

Process finished with exit code 0

 7.dictionary application: Translate the input number into English

input默认是string,可以直接遍历string建立string:string的dictionary,也可以将input变为int,建立int:string的dictionary

define的method是将数字拆分单个数的组成的list文章来源地址https://www.toymoban.com/news/detail-630353.html

dic_demo = {
    1: "one",
    2: "two",
    3: "three",
    4: "four",
    5: "five",
    6: "six",
    7: "seven",
    8: "eight",
    9: "nine",
    0: "zero"
}
dic_demo1 = {
    "1": "one",
    "2": "two",
    "3": "three",
    "4": "four",
    "5": "five",
    "6": "six",
    "7": "seven",
    "8": "eight",
    "9": "nine",
    "0": "zero"
}
string_num = input("Please input the numbers:")


# string方法
string_ans = ""
for i in string_num:
    string_ans = string_ans+dic_demo1[i]+" "
print(string_ans)


def nums_to_single(nums_demo):
    nums_ans = []
    if nums_demo == 0:
        nums_ans.append(0)
    while nums_demo > 0:
        nums_ans.append(nums_demo % 10)
        nums_demo = int(nums_demo / 10)
    nums_ans.reverse()
    return nums_ans


# int方法
string_ans1 = ""
int_num = int(string_num)
num_list = nums_to_single(int_num)
for i in num_list:
    string_ans1 = string_ans1+dic_demo[i]+" "
print(string_ans1)

到了这里,关于Python学习笔记:List、Tuple、for循环的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Linux shell编程学习笔记17:for循环语句

    Linux Shell 脚本编程和其他编程语言一样,支持算数、关系、布尔、字符串、文件测试等多种运算,同样也需要进行根据条件进行流程控制,提供了if、for、while、until等语句。  之前我们探讨了if语句,现在我们来探讨for循环语句。 Linux Shell中的for语句十分灵活,格式多样,我

    2024年02月06日
    浏览(33)
  • Python列表(list)、元组(tuple)和字典(dictionary)的区别

    目录 列表(list)  访问列表元素  更新和删除列表元素 元组(tuple) 元组的访问、更新、删除操作  字典(dictionary)  创建空字典 修改、删除字典 总结 列表(list)、元组(tuple)和字典(dictionary)都是序列,序列都是由索引和元素组成。遍历和访问都按照如下格式: 具

    2023年04月13日
    浏览(36)
  • SOP/详解*和**/python数据结构(iter,list,tuple,dict)/ 解包

    1 . ==== combined_seq.named_children() 2 . isinstance 2th parameter : must be a type or tuple of types ==== 改为tuple,不要用列表。改为 LLLayer = (nn.Conv2d,nn.Linear) 3 . File “test.py”, line 90, in calculate_fin_fout print(“hi”, seq[k].weight.shape[0], layers_fin_fout[“fout”][module]) KeyError: ScaledNeuron( (neuron): IFNode( v_thres

    2024年02月14日
    浏览(32)
  • 抛弃for循环遍历list

    Java 8 API添加了一个新的抽象称为流Stream,可以让你以一种声明的方式处理数据。 Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达的高阶抽象。 filter filter:过滤,就是过滤器,符合条件的通过,不符合条件的过滤掉 map map:映射,

    2024年02月20日
    浏览(30)
  • 微信小程序开发学习笔记——3.4for循环列表渲染的用法

    跟着b站up主“咸虾米_”学习微信小程序开发中,把学习记录存到这方便后续查找。 课程连接:https://www.bilibili.com/video/BV19G4y1K74d?p=18vd_source=9b149469177ab5fdc47515e14cf3cf74 https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/list.html data.js文件的data部分:  data.wxml文件中添加如下代码:

    2024年02月22日
    浏览(38)
  • Python错误解决:list indices must be integers or slices, not tuple

    Python错误解决:list indices must be integers or slices, not tuple 在Python编程中,我们经常会遇到代码运行时出现错误的情况。其中,\\\"list indices must be integers or slices, not tuple\\\"是一种常见的错误类型。它通常发生在使用列表时,我们将元组作为索引值传递给列表时会出现这个错误。 该错

    2024年02月11日
    浏览(31)
  • Python数据容器(列表list、元组tuple、字符串str、字典dict、集合set)详解

    相关介绍: 一种可以容纳多份数据的数据类型,容纳的每一份数据称之为一个元素。每一个元素,可以是任意类型的数据 分为五类: 列表[list]、元组(tuple)、字符串(str)、集合{set}、字典{dict} 相应区别: 列表 元祖 字符串 集合 字典 元素数量 多个 多个 多个 多个 多个 元素类

    2024年02月11日
    浏览(61)
  • 【Python】进阶学习:列表推导式如何使用两个for循环

    【Python】进阶学习:列表推导式如何使用两个for循环 🌈 个人主页:高斯小哥 🔥 高质量专栏:Matplotlib之旅:零基础精通数据可视化、Python基础【高质量合集】、PyTorch零基础入门教程👈 希望得到您的订阅和支持~ 💡 创作高质量博文(平均质量分92+),分享更多关于深度学习、

    2024年03月17日
    浏览(51)
  • 任何时候都不要在 for 循环中删除 List 集合元素!!!

    首先说结论:无论什么场景,都不要对List使用for循环的同时,删除List集合元素,因为这么做就是不对的。 阿里开发手册也明确说明禁止使用foreach删除、增加List元素。 正确删除元素的方式是使用迭代器(Iterator),代码如下: JDK8后lambda写法: 不想知道为什么不能使用for循

    2023年04月15日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包