मैं इनपुट लेना चाहता हूं और इनपुट (स्ट्रिंग्स) में सामान्य अक्षरों की संख्या खोजने के लिए ऑपरेशन करना चाहता हूं।
उदाहरण इनपुट:
abcaa
bcbd
bgc
आउटपुट 2 है क्योंकि b और c सभी में मौजूद हैं।
मैं कोड लिखने की कोशिश करता हूं लेकिन मैं यहां चौथी पंक्ति में फंस गया हूं:
t = int(input())
for i in range(3):
s1=input()
#a=list(set(s1)&set(s2))
print(a)
'''
input:
3
abcaa
bcbd
bgc
output:
2
because 'b' and 'c' are present in all three
'''
4 जवाब
जितनी आप तुलना करना चाहते हैं, उतनी संख्या दर्ज करें:
as_many_inputs = 3
asked_inputs = [set(input("Enter the string you want\t")) for i in range(as_many_inputs)]
from functools import reduce
print("Number of common is:\t", len(reduce(set.intersection, asked_inputs)))
यहां आप चौराहे को खोजने के लिए इनबिल्ट कम () फ़ंक्शन का उपयोग कर सकते हैं। साथ ही, लेन() नंबर वापस कर देगा।
Enter the string you want aah
Enter the string you want aak
Enter the string you want aal
Number of common is: 1
मैंने 5 के साथ भी परीक्षण किया:
Enter the string you want agh
Enter the string you want agf
Enter the string you want age
Enter the string you want agt
Enter the string you want agm
Number of common is: 2
आप इसे आजमा सकते हैं:
t = int(input("How many inputs there will be:\t")) # take the number of inputs will be given
inputs = [] # empty list for inputs
a = [] # empty list for inputs as set of letters
# loop through the range of t (total number of inputs given at first)
for i in range(t):
input_taken = input("Enter your input:\t") # take the input
inputs.append(input_taken) # store input to the inputs list
# loop through inputs, make sets of letters for each item and store them in a
for i in inputs:
a.append(set(i))
u = set.intersection(*a) # find the common letter's set from all items of a and save it as a set(u)
print(len(u), u) # print the length of u and u(different letters from each items of a)
@Rohitjojo09
, आप इस तरह कोशिश कर सकते हैं।
नोट: मैं आपके t
के स्थान पर n
और s1
के स्थान पर s
वेरिएबल का उपयोग कर रहा हूं और अधिकतर यह समझ में आता है।
आप इस कोड को https://rextester.com/ISE37337 पर ऑनलाइन आज़मा सकते हैं।
def get_count():
n = int(input())
for i in range(n):
s = input().strip()
if not i:
l = list(set(s))
else:
i = 0
while l and i < len(l) - 1:
ch = l[i]
if not ch in s:
l.remove(ch)
else:
i += 1
return len(l)
count = get_count()
print(count) # 2
3
abcaa
bcbd
bgc
इसे जांचें !!
s=set() #create empty set
c=1
num=int(input("Enter no of strings you want to enter: "))
for i in range(num):
x= input("enter string: ")
if c < 2:
for j in x:
s.add(j) #adding elemnts of x in set
c+=1
else:
s=s.intersection(x)
print(s,len(s))
संबंधित सवाल
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।