【问题标题】:How to search letter in list/array using python?如何使用python在列表/数组中搜索字母?
【发布时间】:2023-04-09 09:02:01
【问题描述】:

我有一个这样的列表/数组。

ch = [ eretail, retail, fax, xerox ]

我尝试使用单个/多个字母获取字符串。

例如,我发送了“e”字母作为输入。它将搜索整个字符串,如果它与字符串匹配,则字符串将显示为输出

例子:

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

4)Input: retail
  output: eretail
          retail

我需要上面的输出。

【问题讨论】:

标签: python mongodb


【解决方案1】:

您可能需要这样做:

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)

结果:

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

或者,以防万一输入不是数组:

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)

结果:

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

请尝试上面的代码,我是 python 的初学者 - 如果需要增强,请编辑此答案。 re 也是 Python 的内置 Regex 模块,在这个例子中使用。

【讨论】:

  • 我试图在lamba函数中执行上面的代码,我得到了像这样的错误兄弟。 "errorMessage": "预期的字符串或类似字节的对象"
  • @RameshReddy :不确定为什么 lambda 会被该错误触发,你的 ch 是字符串列表吗,在普通的 python 编辑器中尝试这个功能,它应该可以按预期工作,你检查错误堆栈是什么代码行抛出这个问题,我猜它不应该来自这个!
  • 是的兄弟,我已经在 python 编辑器中执行了上面的代码,它正在工作,但在 lambda 函数中,它不工作。在错误堆栈中,“if(re.findall(search, i)):”这里我收到错误兄弟。
  • 兄弟我已经在查询中清楚地解释了上述问题,请您看一次。 stackoverflow.com/questions/58660375/…
  • @RameshReddy:检查这两个的类型:输入应该是一个字符串& ch 应该是一个数组/字符串列表!做这样的事情:: type(name),如果它们不是字符串,那么你必须使用 str(name) 转换它们
【解决方案2】:

我不知道这是不是你想要的。

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

如果没有,请为您的问题提供更多信息。 希望对您有所帮助。

【讨论】:

  • 它适用于我的机器。你现在使用什么python版本?它会触发什么样的错误?
  • 我使用的是 python 3.6 版本。我已经在 aws lambda 函数中执行了上述操作,我只得到一个字母。假设我发送“t”作为输入,我只得到输出“t”,而不是整个单词。
  • 我收到的只是字母而不是整个单词。我需要与字母匹配的整个单词。
【解决方案3】:

您没有指定是否要在 python2 或 python3 上的代码 我是用python3写的,但这里没有太大的不同。

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

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

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

【讨论】:

    【解决方案4】:

    试试这个:

    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)
    

    【讨论】:

    • 它不起作用,可能是附加错误。你能检查一次吗
    • 它对我来说完美无缺。确保当您替换 input_letter 变量中的字母时,该字母位于引号内。还要检查列表 ch 中的所有字母是否都在引号内。请告知是否解决了问题。
    • 我使用的是 python 3.6 版本。我已经在 aws lambda 函数中执行了上述操作,我只得到一个字母。假设我发送“t”作为输入,我只得到输出“t”,没有得到匹配的“t”整个单词。
    • 我只收到一封作为输入发送的字母,但我需要与该字母匹配的整个单词。
    【解决方案5】:
    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
    
    

    【讨论】:

      猜你喜欢
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      • 2011-02-24
      • 2013-06-27
      • 2016-10-16
      • 1970-01-01
      相关资源
      最近更新 更多