如何实现两个有相似元素列表,其中一个删除相似元素——python

本文探讨了在Python中如何删除一个列表中与另一个列表相同的元素的问题。通过一个实例展示了错误的尝试和正确的解决方案,强调在使用'for'循环遍历列表时不能直接修改列表。
摘要由CSDN通过智能技术生成

如何实现两个有相似元素列表,其中一个删除相似元素——python


#Before the starting
This is my first blog, cause i want to learn english, so i will try to finish every blog all by english. i hope my english will turn to be great!!!
#the following is the main body

here have two lists,
str1 = [1,2,3,4]
str2 = [3,4,5,6]

i want to find the elements that is in str1 but not in str2, at the first, i try the following code:

// An highlighted block
for i in str1:
	if i in str2:
		str1.remove(i)
print(str1)

I hope the result was ‘[1,2]’,however the result is ‘[1,2,3]’. this confused me, and i take so much time to figure out whe

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值