列表理解
当您想要基于现有列表的值创建新列表时,列表理解提供了更短的语法。 (参考-https://www.w3schools.com/python/python_lists_compressive.asp)
示例:1
方法:1
fruits = ["apple", "banana", "cherry", "kiwi", "mango"] newlist = [] for x in fruits: if "a" in x: newlist.append(x) print(newlist)
方法:2(综合)
fruits = ["apple", "banana", "cherry", "kiwi", "mango"] newlist = [x for x in fruits if "a" in x] print(newlist)
输出:
['apple', 'banana', 'mango']
示例:2
l = [10,20,30,40] newlist = [] #using normal loop for num in l: newlist.append(num**2) print(newlist) #using loop in comprehensive way newlist = [num**2 for num in l] print(newlist)
输出:
[100, 400, 900, 1600] [100, 400, 900, 1600]
练习:
1.从2个列表中查找相似的数字以及从相同的2个列表中查找不同的数字。
l1 = [10,20,30,40]
l2 = [30,40,50,60]
得到这个输出:
a) 30,40
#30,40 l1 = [10,20,30,40] l2 = [30,40,50,60] #normal method for num in l1: for no in l2: if num== no: print(num,end=' ') #comprehensive print([num for num in l1 for no in l2 if num==no])
输出:
[30, 40]
b) 10,20,50,60
l1 = [10,20,30,40] l2 = [30,40,50,60] #comprehensive output = [num for num in l1 if num not in l2] output = output + [num for num in l2 if num not in l1] print(output) #normal method for num in l1: if num not in l2: print(num,end=' ') for num in l2: if num not in l1: print(num,end=' ')
输出:
[10, 20, 50, 60] 10 20 50 60
2。以综合方法查找给定输出的程序
l1 = [1,2,3]
l2 = [5,6,7]
输出:[(1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (3, 5), (3, 6) , (3, 7)]
l1 = [1,2,3] l2 = [5,6,7] l = [(i,j) for i in l1 for j in l2 if i!=j] print(l)
输出:
[(1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (3, 5), (3, 6), (3, 7)]
3。查找给定输出的程序:
s = "a1b2c3"
输出:abc123
方法:1
s = "a1b2c3" alpha_list = [] num_list = [] for letter in s: if letter.isalpha(): alpha_list.append(letter) else: num_list.append(letter) print("".join(alpha_list+num_list))
方法:2
s = "a1b2c3" letter=''.join([i for i in s if i.isalpha()]) no=''.join([i for i in s if i.isdigit()]) print(letter+no)
输出:
abc123
4。查找给定输出的程序:
s = "a4k3b2"
输出:aeknbd
s = "a4k3b2" i = 0 while i<len first="s[i]" second="int(s[i+1])" print chr i> <p>输出:</p> <p>aeknbd</p> <p><strong>说明:</strong></p> <p>-->使用ord(first)获得first的ASCII值,然后将其添加到其中以找到新字符。<br> -->ord() 用于查找 ASCII 值。<br> -->chr() 转换 ASCII 值-->字符。</p> </len>
以上是Python Day-List 理解-练习的详细内容。更多信息请关注PHP中文网其他相关文章!

本文讨论了Python版本3.10中介绍的新“匹配”语句,该语句与其他语言相同。它增强了代码的可读性,并为传统的if-elif-el提供了性能优势

Python中的功能注释将元数据添加到函数中,以进行类型检查,文档和IDE支持。它们增强了代码的可读性,维护,并且在API开发,数据科学和图书馆创建中至关重要。

本文讨论了Python中的单位测试,其好处以及如何有效编写它们。它突出显示了诸如UNITSEST和PYTEST等工具进行测试。

文章讨论了Python的\ _ \ _ Init \ _ \ _()方法和Self在初始化对象属性中的作用。还涵盖了其他类方法和继承对\ _ \ _ Init \ _ \ _()的影响。

本文讨论了python中@classmethod,@staticmethod和实例方法之间的差异,详细介绍了它们的属性,用例和好处。它说明了如何根据所需功能选择正确的方法类型和DA

Inpython,YouAppendElementStoAlistusingTheAppend()方法。1)useappend()forsingleelements:my_list.append(4).2)useextend()orextend()或= formultiplelements:my_list.extend.extend(emote_list)ormy_list = [4,5,6] .3)useInsert()forspefificpositions:my_list.insert(1,5).beaware


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SublimeText3 Linux新版
SublimeText3 Linux最新版

记事本++7.3.1
好用且免费的代码编辑器