python-3.x - Can a list with repeating values be converted into a multiset in python?
问题描述
I am having two lists with repeating values and I wanted to take the intersection of the repeating values along with the values that have occurred only once in any one of the lists. I am just a beginner and would love to hear simple suggestions!
解决方案
方法一
l1=[1,2,3,4]
l2=[1,2,5]
intersection=[value for value in l1 if value in l2]
for x in l1+l2:
if x not in intersection:
intersection.append(x)
print(intersection)
方法二
print(list(set(l1+l2)))
推荐阅读
- wxpython - 如何设置 wx.TextEntryDialog 按钮的标签
- java - Spring Boot公共库项目中的单元测试
- java - 似乎无法让 Eclipse IDE 在我的 Mac 上找到 geckodriver
- c# - 如何从 CSV 文件中获取特定列的索引 ID
- node.js - 如何绕过express中的中间件
- qt - 使用 Qt 和 cmake 构建非控制台 Windows 应用程序
- python - 如何保留当前的 tkinter 窗口并使用按钮更新它?
- javascript - 在 Angular 2+ 中进行 DOM 操作的正确方法
- python - 使用 Pandas 的唯一连接数据框
- android - 空指针异常 - 动态壁纸