Python 在较长的字符串中查找字符串,而不考虑大小写(并确定大小写)

Python 在较长的字符串中查找字符串,而不考虑大小写(并确定大小写),python,mixed-case,Python,Mixed Case,一般来说,我对python和编程非常陌生,目前正在Grok online上学习一门在线课程。目前,我被困在第二门课上(机器人排成一行!),因为简要的任务是设计一个程序,读入一行文本并打印出单词robot是否出现,尽管它必须确定单词是小写、大写还是混合大写。到目前为止,这是我的解决方案: text = input('Line: ') if 'robot' in text: print('There is a small robot in the line.') elif 'robot'.u

一般来说,我对python和编程非常陌生,目前正在Grok online上学习一门在线课程。目前,我被困在第二门课上(机器人排成一行!),因为简要的任务是设计一个程序,读入一行文本并打印出单词robot是否出现,尽管它必须确定单词是小写、大写还是混合大写。到目前为止,这是我的解决方案:

text = input('Line: ')

if 'robot' in text:
  print('There is a small robot in the line.')

elif 'robot'.upper() in text:
  print('There is a big robot in the line.')

elif 'robot' or 'ROBOT' != text.isupper() and not text.islower():
  print('There is a medium sized robot in the line.')

else:
  print('No robots here.')

另一件事是,程序必须将单词区分为单个字符串,因此它将为“robot”打印True,而为“strobotron”打印false。

您的第二个
elif
语句应该是

elif 'robot' in text.islower() and not ('ROBOT' in text or 'robot' in text):
如果你想在一行中完成所有这些

对于第二个要求,您可以使用regex:


您的第二个
elif
语句应该是

elif 'robot' in text.islower() and not ('ROBOT' in text or 'robot' in text):
如果你想在一行中完成所有这些

对于第二个要求,您可以使用regex:


我假设您的输入最多包含一个机器人字符串

>>> def findrobot(text):
        if 'robot' in text:
            print('There is a small robot in the line.')
        elif 'robot'.upper() in text:
            print('There is a big robot in the line.')
        elif re.search(r'(?i)robot', text):
            if 'robot' not in text and 'ROBOT' not in text:
                print('MIxed cased robot found')
        else:
            print('No robots here.')


>>> text = input('Line: ')
Line: robot
>>> findrobot(text)
There is a small robot in the line.
>>> text = input('Line: ')
Line: ROBOT
>>> findrobot(text)
There is a big robot in the line.
>>> text = input('Line: ')
Line: RobOt
>>> findrobot(text)
MIxed cased robot found
>>> text = input('Line: ')
Line: foo
>>> findrobot(text)
No robots here.

我假设您的输入最多包含一个机器人字符串

>>> def findrobot(text):
        if 'robot' in text:
            print('There is a small robot in the line.')
        elif 'robot'.upper() in text:
            print('There is a big robot in the line.')
        elif re.search(r'(?i)robot', text):
            if 'robot' not in text and 'ROBOT' not in text:
                print('MIxed cased robot found')
        else:
            print('No robots here.')


>>> text = input('Line: ')
Line: robot
>>> findrobot(text)
There is a small robot in the line.
>>> text = input('Line: ')
Line: ROBOT
>>> findrobot(text)
There is a big robot in the line.
>>> text = input('Line: ')
Line: RobOt
>>> findrobot(text)
MIxed cased robot found
>>> text = input('Line: ')
Line: foo
>>> findrobot(text)
No robots here.

这将处理标点符号,并避免匹配频闪管:

text = input('Line: ')

text = text.replace('.,?;:"', ' ')
words = text.split()
lowers = text.lower().split()

if 'robot' in words:
  print('There is a small robot in the line.')
elif 'robot'.upper() in words:
  print('There is a big robot in the line.')
elif 'robot' in lowers:
  print('There is a medium sized robot in the line.')
else:
  print('No robots here.')

这将处理标点符号,并避免匹配频闪管:

text = input('Line: ')

text = text.replace('.,?;:"', ' ')
words = text.split()
lowers = text.lower().split()

if 'robot' in words:
  print('There is a small robot in the line.')
elif 'robot'.upper() in words:
  print('There is a big robot in the line.')
elif 'robot' in lowers:
  print('There is a medium sized robot in the line.')
else:
  print('No robots here.')

有很多方法可以解决这个问题。正则表达式是其中一种工具。考虑到这是您的第二门编程课程,我建议您不要使用正则表达式。相反,我将尝试使用更基本的python工具和概念

首先在空白处拆分字符串:

words = text.split()
这将把字符串
“我是一个机器人”
拆分为一个单词列表:
[“我”、“我”、“a”、“机器人”]
。请注意,这不会分割标点符号<代码>“我是一个机器人。”将变成
[“我”、“我”、“机器人”。]
。注意“机器人”末尾的点。。对于其余的答案,我将假装没有标点符号,因为这将使问题复杂化,超出第二个编程课程的范围

现在,无论大小写,您都可以为
'robot'
筛选
单词

robots = []
for word in words:
  if word.lower() == 'robot':
    robots.append(word)
这个循环也可以这样写:

robots = [word for word in words if word.lower() == 'robot']
smallrobots = []
largerobots = []
mediumrobots = []
for robot in robots:
  if robot == 'robot':
    smallrobots.append(robot)
  elif robot == 'ROBOT':
    largerobots.append(robot)
  else:
    mediumrobots.append(robot)

if not robots:
  print('No robots here.')
if smallrobots:
  print('There is a small robot in the line.')
if largerobots:
  print('There is a big robot in the line.')
if mediumrobots:
  print('There is a medium sized robot in the line.')
这称为列表理解,基本上只是一种简洁的方法,可以编写一个循环,将列表中的某些项收集到另一个列表中。如果你还没有学会列表理解,那就忽略这一部分

从输入开始,“我是一个机器人,你是一个机器人,我们是机器人,但不是strobotron”列表将是
[“机器人”,“机器人”,“机器人”]
<代码>'strobotron'不在列表中,因为它不等于
'robot'
。这解决了查找
'robot'
而不是
'strobotron'
的问题

如果
机器人
列表为空,则您知道根本没有机器人。如果它不是空的,那么您可以检查小型、大型或中型机器人

if not robots:
  print('No robots here.')
elif 'robot' in robots:
  print('There is a small robot in the line.')
elif 'ROBOT' in robots:
  print('There is a big robot in the line.')
else:
  print('There is a medium sized robot in the line.')
第一个条件(
如果不是robots:
)是使用一种称为隐式布尔的python机制。几乎任何东西都可以在这样的if条件下使用,并且它将隐式转换为布尔值。在大多数情况下,如果事物是“空的”,它将被认为是假的

注意if-else链中条件的顺序。您必须先检查空列表,否则else部分将无法工作。逻辑是这样的:如果列表不是空的,并且列表中没有大小机器人,那么列表中的任何机器人都必须是中等机器人


你的问题描述有点含糊不清。如果队伍中同时有小型和大型(以及中型)机器人呢。它应该同时报告这两种情况吗?如果两者都符合要求,当前代码将只报告一个小型机器人。这是因为它首先检查一个小机器人,然后跳过其余的(这就是
elif
的语义)

要报告小型和大型(和中型)机器人,您可以这样做:

robots = [word for word in words if word.lower() == 'robot']
smallrobots = []
largerobots = []
mediumrobots = []
for robot in robots:
  if robot == 'robot':
    smallrobots.append(robot)
  elif robot == 'ROBOT':
    largerobots.append(robot)
  else:
    mediumrobots.append(robot)

if not robots:
  print('No robots here.')
if smallrobots:
  print('There is a small robot in the line.')
if largerobots:
  print('There is a big robot in the line.')
if mediumrobots:
  print('There is a medium sized robot in the line.')
请注意,
elif
现在仅在循环内。仅使用
if
进行报告意味着如果发现小型机器人,则不会跳过中型机器人

奖励:您现在甚至可以区分是否有一个或多个小型机器人排队:

if len(smallrobots) == 1:
  print('There is a small robot in the line.')
elif len(smallrobots) > 1:
  print('There are small robots in the line.')

有很多方法可以解决这个问题。正则表达式是其中一种工具。考虑到这是您的第二门编程课程,我建议您不要使用正则表达式。相反,我将尝试使用更基本的python工具和概念

首先在空白处拆分字符串:

words = text.split()
这将把字符串
“我是一个机器人”
拆分为一个单词列表:
[“我”、“我”、“a”、“机器人”]
。请注意,这不会分割标点符号<代码>“我是一个机器人。”将变成
[“我”、“我”、“机器人”。]
。注意“机器人”末尾的点。。对于其余的答案,我将假装没有标点符号,因为这将使问题复杂化,超出第二个编程课程的范围

现在,无论大小写,您都可以为
'robot'
筛选
单词

robots = []
for word in words:
  if word.lower() == 'robot':
    robots.append(word)
这个循环也可以这样写:

robots = [word for word in words if word.lower() == 'robot']
smallrobots = []
largerobots = []
mediumrobots = []
for robot in robots:
  if robot == 'robot':
    smallrobots.append(robot)
  elif robot == 'ROBOT':
    largerobots.append(robot)
  else:
    mediumrobots.append(robot)

if not robots:
  print('No robots here.')
if smallrobots:
  print('There is a small robot in the line.')
if largerobots:
  print('There is a big robot in the line.')
if mediumrobots:
  print('There is a medium sized robot in the line.')
这称为列表理解,基本上只是一种简洁的方法,可以编写一个循环,将列表中的某些项收集到另一个列表中。如果你还没有学会列表理解,那就忽略这一部分

从输入开始,“我是一个机器人,你是一个机器人,我们是机器人,但不是strobotron”列表将是
[“机器人”,“机器人”,“机器人”]
<代码>'strobotron'不在列表中,因为它不等于
'robot'
。这解决了查找
'robot'
而不是
'strobotron'
的问题

如果
机器人
列表为空,则您知道根本没有机器人。如果它不是空的,那么您可以检查小型、大型或中型机器人

if not robots:
  print('No robots here.')
elif 'robot' in robots:
  print('There is a small robot in the line.')
elif 'ROBOT' in robots:
  print('There is a big robot in the line.')
else:
  print('There is a medium sized robot in the line.')
第一个条件(
如果不是robots:
)是使用一种称为隐式布尔的python机制。几乎任何东西都可以在if条件下使用,如