统计数组在字符串中出现的次数
Count occurrences of array in string
我是编程的新手,我已经尽我所能 researching/reading 但还没有找到具体的答案来帮助我。
我的问题如下,如果我有一个包含多个单词的数组如下:
myArray= "this","makes","bats","look","funny"
而且我有一长串混乱的单词(包括数组中列出的单词的一些实例,例如:
字符串 = "jkmakespohbatsllfunnythisqwemakes"
字符串包含单词 "makes" 的两个实例、"bats" 的一个实例、"funny" 的一个实例、"this" 的一个实例和 [= 的零个实例27=].
有人可以用一些伪代码帮助我指明正确的方向吗我如何从我的数组中搜索字符串中的任何子字符串单词并打印找到的每个实例的计数(如上所述)或未找到.
我现在对编程语言有不同的看法,但是当我更好地掌握结构时,我可能想稍后编写代码。
谢谢!
# define a list of your words (call it 'words')
# define the string you want to search through (call it 'jumble')
# Define an empty dictionary that will hold your results. You can use a regular
# dictionary, or a cool thing in Python called "DefaultDict", in the collections module.
# Call it 'count'.
# Now loop through each of your words
# Start searching at the beginning of your jumble, by
# specifying a start position of 0. (This will make sense later.)
# Add another loop here, that keeps looping until we're sure there's no
# more words to be found.
# Try to find (hint: check out the 'string' module) the word inside of jumble.
# If we did NOT find a word
# We just 'break' out of the inner loop
# Else If we DID find a word, let's save the position of that word.
# Call that 'index'.
# If this is the first time, we put the word in our dictionary
# with a count of 1
# Else If this is not the first time, add 1 to the dictionary entry
# for that word.
# Now we search AGAIN through the jumble, but start AFTER the
# position where we saw the first time.
# Now we've exited the entire thing, let's just print out our 'count' dictionary.
这是伪代码。
以下是您可能想要使用的各种功能和特性的一些示例:
colors = ['red', 'blue', 'green'] # Lists
for color in colors: # For loops
print(color)
person = {} # Empty dictionary
person['name'] = 'Symmitchry' # Add a key / value pair to a dictionary
if 'name' in person: # Looking for a key in a dictionary
print('Person has a name:') # How to print
print(person['name']) # How to access a value from a dictionary
else: # how to write an else statement
print('Person has no name! Weird.')
import random # importing a module
x = random.randint() # Using a function from a module
我是编程的新手,我已经尽我所能 researching/reading 但还没有找到具体的答案来帮助我。
我的问题如下,如果我有一个包含多个单词的数组如下: myArray= "this","makes","bats","look","funny"
而且我有一长串混乱的单词(包括数组中列出的单词的一些实例,例如: 字符串 = "jkmakespohbatsllfunnythisqwemakes"
字符串包含单词 "makes" 的两个实例、"bats" 的一个实例、"funny" 的一个实例、"this" 的一个实例和 [= 的零个实例27=].
有人可以用一些伪代码帮助我指明正确的方向吗我如何从我的数组中搜索字符串中的任何子字符串单词并打印找到的每个实例的计数(如上所述)或未找到.
我现在对编程语言有不同的看法,但是当我更好地掌握结构时,我可能想稍后编写代码。
谢谢!
# define a list of your words (call it 'words')
# define the string you want to search through (call it 'jumble')
# Define an empty dictionary that will hold your results. You can use a regular
# dictionary, or a cool thing in Python called "DefaultDict", in the collections module.
# Call it 'count'.
# Now loop through each of your words
# Start searching at the beginning of your jumble, by
# specifying a start position of 0. (This will make sense later.)
# Add another loop here, that keeps looping until we're sure there's no
# more words to be found.
# Try to find (hint: check out the 'string' module) the word inside of jumble.
# If we did NOT find a word
# We just 'break' out of the inner loop
# Else If we DID find a word, let's save the position of that word.
# Call that 'index'.
# If this is the first time, we put the word in our dictionary
# with a count of 1
# Else If this is not the first time, add 1 to the dictionary entry
# for that word.
# Now we search AGAIN through the jumble, but start AFTER the
# position where we saw the first time.
# Now we've exited the entire thing, let's just print out our 'count' dictionary.
这是伪代码。
以下是您可能想要使用的各种功能和特性的一些示例:
colors = ['red', 'blue', 'green'] # Lists
for color in colors: # For loops
print(color)
person = {} # Empty dictionary
person['name'] = 'Symmitchry' # Add a key / value pair to a dictionary
if 'name' in person: # Looking for a key in a dictionary
print('Person has a name:') # How to print
print(person['name']) # How to access a value from a dictionary
else: # how to write an else statement
print('Person has no name! Weird.')
import random # importing a module
x = random.randint() # Using a function from a module