[英]Write a python program to display all the common characters between two strings
def firstAndLast(first,last):
common = []
if first in last:
print first
first = list(raw_input("Enter first name: "))
last = list(raw_input("Enter last name: "))
firstAndLast(first, last)
File "<ipython-input-13-f4ec192dd3a8>", line 11
print names
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(names)?
The error tells you what to do.该错误告诉您该怎么做。 In python-3.x print() is function is with parentheses, so you've to pass the arguments in the parentheses.
在 python-3.x 中,print() 函数是带括号的,所以你必须在括号中传递参数。
print(first)
And you can also just use input()
function instead of raw_input()
function你也可以只使用
input()
函数而不是raw_input()
函数
PROGRAM程序
def firstAndLast(first,last):
common = []
for letter in first:
if letter in last:
common.append(letter)
return common
first = input("Enter first name: ")
last = input("Enter last name: ")
common = firstAndLast(first, last)
#common will be the list of common letters
print(common)
OUTPUT输出
Enter first name: janee
Enter last name: denver
['n', 'e', 'e']
Program:程序:
s1=raw_input("Enter first string:")
s2=raw_input("Enter second string:")
a=list(set(s1)&set(s2))
print("The common letters are:")
for i in a:
print(i)
s1=input("Enter first string:") s1=input("请输入第一个字符串:")
s2=input("Enter second string:") s2=input("请输入第二个字符串:")
a=list(set(s1)&set(s2)) a=list(set(s1)&set(s2))
print("The common letters are:") print("常见的字母有:")
for i in a: print(i) for i in a: print(i)
Try this,尝试这个,
first = list(input("Enter first name: "))
last = list(input("Enter last name: "))
print(f"common characters are: {', '.join(set(first) & set(last))}")
output:输出:
Enter first name: Jaydip
Enter last name: Kyada
common characters are: a, y, d
I think this single line will answer you question.我认为这一行会回答你的问题。
You can numpy'sintersect1D
:-你可以
numpy'sintersect1D
:-
import numpy as np
def firstAndLast(first,last):
return np.intersect1d(first, last)
first = input("Enter first name: ")
last = input("Enter last name: ")
first, last = [*first], [*last]
firstAndLast(first, last)
Output输出
Enter first name: gaurav
Enter last name: chowdhary
array(['a', 'r'], dtype='<U1')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.