0

I have one list/array like this.

ch = [ eretail, retail, fax, xerox ]

I tried fetch strings using single/multiple letters.

for example, i sent 'e' letter as input. it will search entire string, incase it will match with string, the string will display as output

examples:

1)Input:   e
  output:  eretail
           retail
           xerox
2)Input: x
  output:  fax
           xerox
3)Input: o
  output: xerox

4)Input: retail
  output: eretail
          retail

I need the output like above.

2

5 Answers 5

1

You might need to do something like this :

import re

ch = ['eretail', 'retail', 'fax', 'xerox']

input = ['e', 'x', 'o', 'retail']

for each in input:
    search = each + '+'
    output = []
    for i in ch:
        if(re.findall(each, i)):
            output.append(i)
    print('Input', each, '\n', 'output', output)

Result :

Input e 
 output ['eretail', 'retail', 'xerox']
Input x 
 output ['fax', 'xerox']
Input o 
 output ['xerox']
Input retail 
 output ['eretail', 'retail']

Or, just in case input is not an array :

import re

ch = ['eretail', 'retail', 'fax', 'xerox']

input = 'e'
search = input + '+'
output = []

for i in ch:
    if(re.findall(search, i)):
        output.append(i)

print('Input', input, '\n', 'output', output)

Result:

Input e 
 output ['eretail', 'retail', 'xerox']

Please try the above code, I'm a beginner in python - edit this answer if it needs to be enhanced. Also re is python's built-in Regex module that is being used in this example.

Sign up to request clarification or add additional context in comments.

7 Comments

I tried to execute the above code in lamba function, i am getting error like this bro. "errorMessage": "expected string or bytes-like object"
@RameshReddy : not sure why lambda is getting triggered with that error, are your ch is list of strings, try this functionality in normal python editor it should work as expected, Did you check error stack for what lines of code is throwing this issue, I would guess it shouldn't be from this !!
yes bro, i have executed the above code in python editor, it's working but in lambda function, it's not working. in error stack, "if(re.findall(search, i)): " here i am getting error bro.
bro i have clearly explain the above issue in query, can you please see once. stackoverflow.com/questions/58660375/…
@RameshReddy : Check the type of those two : input should be a string & ch should be an array/list of strings !! Do something like this :: type(name), if they're not string then you've to convert those using str(name)
|
1

I don't know whether this is what you want.

target = input()
for word in ch:
    if target in word:
        print(word)

If not, please give more information to your question. Hope this help.

3 Comments

It works on my machine. What python version are you using right now? What kind of error does it trigger?
I am using python 3.6 version. I have executed the above in aws lambda function, i am getting only one letter.suppose I sent "t" as input, i am getting output "t" only, not entire word.
I am getting only letter not entire word. I need entire word matched with the letter.
0

You did not specified if you want the code on python2 or python3 I wrote it in python3 but there isn't so much different here.

ch = ["eretail", "retail", "fax", "xerox"]

myLetter = str(input("Enter your letter: "))

for word in ch:
    if myLetter in word:
        print(word)

Comments

0

Try this:

ch = ['eretail', 'retail', 'fax', 'xerox' ]
input_letter = "x"

new_list = []
for item in ch:
    if input_letter in item:
        new_list.append(item)
print(new_list)

4 Comments

it's not working, may be append is wrong. can you please check once
It is working perfectly on my end. Make sure that when you replace the letter in the input_letter variable that the letter is within quotation marks. Also check that all letters in the list ch are within quotation marks. Please advise if it solves the problem.
I am using python 3.6 version. I have executed the above in aws lambda function, i am getting only one letter.suppose I sent "t" as input, i am getting output "t" only, not getting ,matched "t" entire word.
I am getting only one letter which i sent as a input but i need the entire word which is matched with the letter.
0
def find(needle, haystack):
    """
    Descr:
    Checks for the presence of a string/character(needle) in the 
    items of an iterable object
    """
    if isinstance(haystack, list):
        for h in haystack:
            find(needle=needle, haystack=h)
    elif isinstance(haystack, dict):
        for k, v in haystack.items():
            find(needle=needle, haystack=v)
    else:
        if str(needle) in haystack:
            print(haystack)

# We can then call the function above as follows:
print("1. Search Results for e in '[eretail, retail, fax xerox]")
find(needle="e", haystack=[ "eretail", "retail", "fax" "xerox" ])

print("\n2. Search Results for x in '[eretail, retail, fax xerox]'")
find(needle="x", haystack=[ "eretail", "retail", "fax" "xerox" ])

print("\n3. Search Results for o in '[eretail, retail, fax xerox]'")
find(needle="o", haystack=[ "eretail", "retail", "fax" "xerox" ])

# Mixed Haystack illustration...
print("\n4. Search Results for cat in '[bobcat, [concatenate,boy, girl], dict(a=vacate, b=boy)]'")
find(needle="cat", haystack=["bobcat", ["concatenate", "boy", "girl"], dict(a="vacate", b="boy")])

# output:

1. Search Results for e in '[eretail, retail, fax xerox]
eretail
retail
faxxerox

2. Search Results for x in '[eretail, retail, fax xerox]'
faxxerox

3. Search Results for o in '[eretail, retail, fax xerox]'
faxxerox

4. Search Results for cat in '[bobcat, [concatenate,boy, girl], dict(a=vacate, b=boy)]'
bobcat
concatenate
vacate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.