Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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/4/string/5.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_String_Algorithm_Parsing_Nodes - Fatal编程技术网

在Python中,如何检查一个字符串以查看其中是否有其他字符串的任何组合?

在Python中,如何检查一个字符串以查看其中是否有其他字符串的任何组合?,python,string,algorithm,parsing,nodes,Python,String,Algorithm,Parsing,Nodes,我有一个17万个单词的列表,我正在编写一个算法,使用一个图表来显示每个单词可能的最长单词链 单词链是一个单词列表,其中第i个单词是(i− 1) 带一个额外字符的st字,其他字符以任何方式排列 A->AN->CAN->CANE 现在我把所有的单词按字母顺序排列,比如CAT=ACT 当字符串2包含字符串1,再加上一个字符时,我说添加一条边 但是在 A->AT->ACT AT和ACT之间的边没有画出来,因为C分割了A和t,而我的if语句只有在找到“AT”时才起作用 我如何告诉python搜索字符串,以

我有一个17万个单词的列表,我正在编写一个算法,使用一个图表来显示每个单词可能的最长单词链

单词链是一个单词列表,其中第i个单词是(i− 1) 带一个额外字符的st字,其他字符以任何方式排列

A->AN->CAN->CANE

现在我把所有的单词按字母顺序排列,比如CAT=ACT

当字符串2包含字符串1,再加上一个字符时,我说添加一条边

但是在

A->AT->ACT

AT和ACT之间的边没有画出来,因为C分割了A和t,而我的if语句只有在找到“AT”时才起作用


我如何告诉python搜索字符串,以便字符的顺序无关紧要?

您可以创建一组两个字符串:

str1 = 'A'
str2 = 'T'
searchstring = 'ACT'

if str1 in searchstring and str2 in searchstring:
    print('it matched')


# bigger example

str1 = 'AT'
searchstring = 'ACT'
matches = [a for a in str1 if a in searchstring]
if len(matches) == len(searchstring):
    print('it matched')
set1 = set(string1)
set2 = set(string2)
然后查看
string1
是否包含
string2

set1.issubset(set2) # => returns True if set2 contains everything from set1
您可以使用这两个字符串并将其转换为它(它将计算字符串中的字母),然后您可以比较它们是否相等。范例-

s1 = 'ACT'
s2 = 'CAT'
from collections import Counter
if Counter(s1) == Counter(s2):
    #Do stuff
演示-

>>> s1 = 'ACT'
>>> s2 = 'CAT'
>>> from collections import Counter
>>> Counter(s1) == Counter(s2)
True

如果要检查一个字符串是否包含在另一个字符串中而不考虑顺序,可以使用
any()
内置函数,如下所示-

s1 = 'AXCT'
s2 = 'CAT'
A = Counter(s1)
B = Counter(s2)
if not any(count > A.get(b, 0) for b,count in B):
    #Do stuff.
或者您也可以执行以下操作(如所示)-


您可以将较长的字符串转换为正则表达式,然后进行匹配。一种简单的方法是使所有字符都是可选的,首先检查目标字符串是否长一个字符:

def can_reach(frm, to):
  if len(to) != len(frm) + 1: return False
  if not re.fullmatch(re.sub(r'(.)', r'\1?', to), frm): return False
  return True
如果您没有Python 3.4,则使用显式的
$
锚定:

def can_reach(frm, to):
  if len(to) != len(frm) + 1: return False
  if not re.match(re.sub(r'(.)', r'\1?', to) + '$', frm): return False
  return True

您关心字符串中的重复字符吗?例如,在比较caat和act时。您可以尝试按字母顺序对字母进行排序。如果顺序根本不重要,请使用a而不是字符串。我喜欢在python中整天使用集合,从来没有想过设置字符串。很好。请注意,这将匹配
CAAT
ACT
,不确定它们是否应该匹配。我在OP的上一个重复问题中建议了这种精确的方法,并且被正确地告知它不起作用。可能还想演示如何使用
检查子集(即
A&B==A
)。有趣,它对我有用。试试
计数器('ACT')&计数器('ACT')==计数器('ACT')
;我在3.4.3中得到了正确的答案。@Kevin噢,是的,
A
是子集。希望下一个投票人解释答案的错误,只有这样我才能修正答案。Python的哪个版本?构造
匹配
在字符串长度上是二次的,假设两个字符串的长度相似。其他答案更有效,我不会有任何争论。
def can_reach(frm, to):
  if len(to) != len(frm) + 1: return False
  if not re.match(re.sub(r'(.)', r'\1?', to) + '$', frm): return False
  return True