python - 比较python中的两个数组以查看它们是否共享任何值
问题描述
我正在尝试在 python 中创建一个小程序,要求您输入以逗号分隔的车牌列表。
然后它将字符串转换为数组并要求提供第二组车牌(同样,用逗号分隔)。
我想要它做的是,一旦你输入了两组车牌,它应该告诉你是否有匹配的车牌,如果有,它们是什么。我目前有这个
print('Enter the first set of number plates')
print('Make sure to separate each plate with a comma')
nbPlateInput = input('\n')
nbPlates = str(nbplateinput)
if ',' in nbPlates:
splitPlate = nbPlates.split(',')
elif ',' not in nbPlates:
print('\nYou did not separate all number plates with a comma')
exit()
print('\nEnter the second set of number plates')
nbPlateInput2 = input()
nbPlates2 = str(nbPlateInput2)
if ',' in nbPlates:
splitPlate2 = nbPlates2.split(',')
elif ',' not in nbPlates2:
print('\nYou did not separate all number plates with a comma')
exit()
if any(word in splitPlate for word in splitPlate2):
duplicate = any(word in splitPlate for word in splitPlate2)
print(duplicate)
else:
print('No Match')
exit()
目前,当有匹配时它输出真,当没有匹配时,它输出“不匹配”我希望它输出匹配的车牌
PS。我对python很陌生,如果我的代码有点乱,我很抱歉
编辑:我最后使用了这段代码而不是我的“如果有”语句
intersect = list(set(splitplate) & set(splitplate2))
print(intersect)
这将打印任何相同的值
解决方案
推荐阅读
- php - 最快和最简单的方法来分解和查找分隔符之间的字符串
- reactjs - reactstrap 中缺少的列表
- batch-file - 如何设置是否有任何新错误写入换行符将显示消息(回显)使用批处理文件条件语句
- c# - 如何以编程方式将构建错误 CS0122 中提到的成员与罪魁祸首类型的实际方法相匹配?
- javascript - 电子邮件正文中的新行
- sql - 根据视图中的另一个列值将自定义字符串连接到列
- assembly - 如果我不将 8051 中的端口设置为输入或输出会怎样?
- python - 如何从bs4输出中获取没有空格的文本
- if-statement - Google 表格 - 在查询中使用 IF 语句
- javascript - useLazyQuery 在循环和 promise.all 中不起作用