Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.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/12.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 给定两个字符串str和word,您必须找出可以从该字符串生成多少个单词_Python - Fatal编程技术网

Python 给定两个字符串str和word,您必须找出可以从该字符串生成多少个单词

Python 给定两个字符串str和word,您必须找出可以从该字符串生成多少个单词,python,Python,我试图解决一些问题,尽管逻辑似乎非常直截了当,但我无法构建一个可能的解决方案 给定两个字符串str和word,您必须找到可以从该字符串生成多少个单词 输入:str=“这是一个测试字符串”word=“tsit” ,产出:2 说明:在给定的str中有4个t's 4个s's 3个i's,通过它您只能生成2个“tsit” 输入:str=“这里是HashedIn Technologies”word=“neurons”输出:0 说明:因为str中没有“u”,所以u不能构成“neurons”这个词 我试图使用

我试图解决一些问题,尽管逻辑似乎非常直截了当,但我无法构建一个可能的解决方案

给定两个字符串str和word,您必须找到可以从该字符串生成多少个单词

输入:str=“这是一个测试字符串”word=“tsit” ,产出:2

说明:在给定的str中有4个t's 4个s's 3个i's,通过它您只能生成2个“tsit”

输入:str=“这里是HashedIn Technologies”word=“neurons”输出:0

说明:因为str中没有“u”,所以u不能构成“neurons”这个词

我试图使用字典逻辑是递增计数,直到任何字符计数变为零,但我如何将其放入代码中

def freq(s, word):
s = s.lower()
aux_s = {}
aux_word = {}
for i in s:
    aux_s[i] = aux_s.get(i, 0) + 1
for i in word:
    aux_word[i] = aux_word.get(i, 0) + 1
count, ans = 0, float('inf')
for i,val in aux_s.items():
    if i in aux_word:
        pass

因为根据你的例子,情况并不重要

str = "This is a test string".lower()
word = "tsit"
ans = len(str)
for i in word:
    ans = min(ans, str.count(i)//word.count(i))
print(ans)

我在解决方案中使用了字典,我只是对您使用的逻辑做了一点修改

import sys

test_str = "This is a test string"
test_str = test_str.lower()
word = "tsit"

res = {} 
word_count = {}

# loop to count frequency of all the characters in word
for keys in word:
  if keys != ' ':
    word_count[keys] = word_count.get(keys, 0) + 1

# loop to count frequency of all the characters in string
for keys in test_str: 
  if keys != ' ':   # if the char is space then ignore it
    res[keys] = res.get(keys, 0) + 1  # increment the count of the character if the character already exists

# assigning ans a max value
ans = sys.maxsize


for keys in word_count:
    if keys in res:
        a = res[keys]    #checking if the key exists in the res or not
    else:
        ans = 0   # if the char does not exist in the string then ans = 0
        break
    b = word_count[keys]
    n = a//b
    if n < ans:   # finding the lowest of the requirement
        ans = n   # assigning it to the answer
      
print(ans)   # print the answer 
导入系统 test\u str=“这是一个测试字符串” test_str=test_str.lower() word=“tsit” res={} 字数={} #循环计算word中所有字符的频率 对于word中的关键字: 如果键!='': 单词计数[键]=单词计数。获取(键,0)+1 #循环以计算字符串中所有字符的频率 对于test_str中的键: 如果钥匙!='':#如果字符是空格,则忽略它 res[keys]=res.get(keys,0)+1#如果字符已经存在,则增加字符的计数 #为ans分配最大值 ans=sys.maxsize 对于word_count中的关键字: 如果在res中输入键: a=res[keys]#检查res中是否存在密钥 其他: ans=0#如果字符串中不存在字符,则ans=0 打破 b=字数[键] n=a//b 如果n给出的答案对你有用吗?