Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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/0/amazon-s3/2.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 3.x 从一组字符串中查找整数_Python 3.x - Fatal编程技术网

Python 3.x 从一组字符串中查找整数

Python 3.x 从一组字符串中查找整数,python-3.x,Python 3.x,嗨,我想解决一个问题,如下所示:- 如果I/P为“OzonePower”,O/P为012,即将0的字符串(零)与输入字符串进行比较,当找到时,它将出现在输出中,依此类推1和2。 为参考提供一组输入和输出:- I/P: O/P: WEIGHFOXTOURIST 2468 OURNEONFOE 114 ETHER

嗨,我想解决一个问题,如下所示:- 如果I/P为“OzonePower”,O/P为012,即将0的字符串(零)与输入字符串进行比较,当找到时,它将出现在输出中,依此类推1和2。 为参考提供一组输入和输出:-

I/P:                               O/P:
WEIGHFOXTOURIST                    2468
OURNEONFOE                         114
ETHER                               3
我已经试过了,但这并不是所有情况下都有结果

def puzzle(dic_number,string,key):
    dic_values=0    
    length=len(dic_number)
    for i in dic_number:
        if i in string:
            dic_values+=1
    if dic_values ==length:
        print(key)


dic1={0:"ZERO",1:"ONE",2:"TWO",3:"THREE",4:"FOUR",5:"FIVE",6:"SIX",7:"SEVEN",8:"EIGHT",9:"NINE"}
string=input("Enter number")

for i,j in enumerate(dic1.values()):
    puzzle(j,string,i)

这里有一种实现方法:

numbers = {
    0: "ZERO",
    1: "ONE",
    2: "TWO",
    3: "THREE",
    4: "FOUR",
    5: "FIVE",
    6: "SIX",
    7: "SEVEN",
    8: "EIGHT",
    9: "NINE",
}


def puzzle(s):
    supper = s.upper()
    ret = []
    for n, chrs in numbers.items():
        if all(c in supper for c in chrs):
            ret.append(n)
    return ret


s = input("enter string ")

numbers_found = puzzle(s)
print('numbers found', numbers_found)
输出:

enter string WEIGHFOXTOURIST
numbers found [2, 3, 4, 6, 8]
enter string OURNEONFOE
numbers found [1, 4]
enter string ETHER
numbers found [3]
enter string OZONETOWER
numbers found [0, 1, 2]
enter string WEIGHFOXTOURIST
numbers found [2, 3, 6]
enter string OURNEONFOE
numbers found [1, 1, 4]
enter string ETHER
numbers found [3]
enter string OZONETOWER
numbers found [0, 1, 2]
注意

通过这种实现

  • 对于输入
    3
    ,您会得到一个额外的
    3
    结果
  • 对于输入
    OURNEONFOE

编辑

要获得重复匹配,拼图函数必须“使用”已使用的字符:

def puzzle(s):
    supper = s.upper()
    ret = []
    for n, chrs in numbers.items():
        while True:
            if all(c in supper for c in chrs):
                for c in chrs:
                    supper = supper.replace(c, '', 1)
                ret.append(n)
            else:
                break
    return ret
输出:

enter string WEIGHFOXTOURIST
numbers found [2, 3, 4, 6, 8]
enter string OURNEONFOE
numbers found [1, 4]
enter string ETHER
numbers found [3]
enter string OZONETOWER
numbers found [0, 1, 2]
enter string WEIGHFOXTOURIST
numbers found [2, 3, 6]
enter string OURNEONFOE
numbers found [1, 1, 4]
enter string ETHER
numbers found [3]
enter string OZONETOWER
numbers found [0, 1, 2]
注意

通过这种实现

  • 对于输入
    8
    ,您不会得到
    8
    的结果

说明:

enter string WEIGHFOXTOURIST
numbers found [2, 3, 4, 6, 8]
enter string OURNEONFOE
numbers found [1, 4]
enter string ETHER
numbers found [3]
enter string OZONETOWER
numbers found [0, 1, 2]
enter string WEIGHFOXTOURIST
numbers found [2, 3, 6]
enter string OURNEONFOE
numbers found [1, 1, 4]
enter string ETHER
numbers found [3]
enter string OZONETOWER
numbers found [0, 1, 2]
在这个拼图中,当在输入字符串中找到一个数字的所有字母时,它被认为是一个“匹配”

为此,我们通过在字符串的字符上创建一个标记来逐个检查每个字符:

>>> chrs = 'FOUR'
>>> [c for c in chrs]
['F', 'O', 'U', 'R']
对于我们用来检查是否在大写输入字符串中找到的每个字符:

>>> chrs = 'FOUR'
>>> supper = 'OURNEONFOE'
>>> [c in supper for c in chrs]
[True, True, True, True]
如果未找到字符,它将在相应位置生成一个
False

>>> chrs = 'FOUR'
>>> supper = 'OXRNEONFOE'
>>> [c in supper for c in chrs]
[True, True, False, True]
然后,使用检查列表中的所有项目是否为
True

>>> chrs = 'FOUR'
>>> supper = 'OURNEONFOE'
>>> all([c in supper for c in chrs])
True
>>> supper = 'OXRNEONFOE'
>>> all([c in supper for c in chrs])
False
由于我们可以在
all

>>> chrs = 'FOUR'
>>> supper = 'OURNEONFOE'
>>> all(c in supper for c in chrs)
True

嗨,谢谢你的回答,效果很好。。你能解释一下这句话的确切含义吗:-```如果全部(c在晚餐中代表c在chrs中):@shubham我在我的回答中添加了一个解释试图避免只使用代码的回答,并在你的回答中提供一些解释。