Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要创建一个程序,打印出以特定字母开头的单词_Python_Arrays_Input_For Loop_While Loop - Fatal编程技术网

Python 需要创建一个程序,打印出以特定字母开头的单词

Python 需要创建一个程序,打印出以特定字母开头的单词,python,arrays,input,for-loop,while-loop,Python,Arrays,Input,For Loop,While Loop,我需要一个程序,要求用户提供3个字母,然后要求用户提供一个字符串,然后打印出字符串中以3个字母开头的所有单词…例如 Enter 3 letters: AMD Enter text: Advanced Micro Devices is a brand for all microsoft desktops word: Advanced Micro Devices word: all microsoft desktops 这很简单。我是新来的,很难弄清楚我的代码目前是如何 ipt1 = raw_in

我需要一个程序,要求用户提供3个字母,然后要求用户提供一个字符串,然后打印出字符串中以3个字母开头的所有单词…例如

Enter 3 letters: AMD
Enter text: Advanced Micro Devices is a brand for all microsoft desktops
word: Advanced Micro Devices
word: all microsoft desktops
这很简单。我是新来的,很难弄清楚我的代码目前是如何

ipt1 = raw_input("Three letters: ") ## Asks for three letters
ipt2 = raw_input("Text: ") ## Asks for text
ipt1_split = ipt1.split() ## Converts three letters to list
ipt2_split = ipt2.split() ## Converts text to list
我不确定你是否需要一份清单,有人知道如何解决这个问题吗?谢谢

一些提示:

  • 要测试一个字符串是否以另一个字符串开头,可以使用
    string.startswith()
  • 您的第一个输入不需要拆分,字符串是一个序列

    • 我会这样做:

      letters = raw_input("letters: ").lower()
      n = len(letters)
      words = raw_input("text: ").split()
      words_lc = [x.lower() for x in words] #lowercase copy for case-insensitive check
      
      for x in range(len(words) - n + 1):
          if all((words_lc[x+n].startswith(letters[n]) for n in range(n))):
              print "match: ", ' '.join(words[x:x+n])
      
      在这种情况下,字母的数量是动态的,如果您想将其固定为三,只需将
      n
      设置为三即可。如果要匹配字母的大小写,请删除原始输入上的
      lower
      调用和
      all
      中的比较,然后尝试以下操作:

      letters = "AMD"
      text = "Advanced Micro Devices is a brand for all microsoft desktops"
      words = text.split()
      for i in xrange(len(words)-len(letters)+1):
          if "".join(map(lambda x: x[0], words[i:i+len(letters)])).lower() == letters.lower():
              print "word:", ".join(words[i:i+len(letters)])
      

      字母的
      拆分
      是错误的(除非它们是用空格输入的),只需使用list()将字符串转换为字符列表。谢谢,修复了这个问题,但仍然不知道如何处理:\。在mdisregard上处理它,实际上,在这种情况下,将它作为字符串保存就足够了,因为字符串可以像列表一样索引。谢谢!工作完美无瑕。在接下来的几周里,我将大量使用此功能,谢谢:)