我试图创建一个正则表达式,它允许任意数量的辅音,或任意数量的元音,或辅音和元音的混合,这样我们在开头只有任意数量的辅音,然后只有任意数量的元音,没有元音之后应该允许辅音,从例子中会更清楚:
以下情况应该通过:
TR, EE, TREE, Y, BY.
Run Code Online (Sandbox Code Playgroud)
但以下不应通过表达式:
TROUBLE, OATS, TREES, IVY, TROUBLES, PRIVATE, OATEN, ORRERY.
Run Code Online (Sandbox Code Playgroud)
所以通常它可以被形象化为: [C] [V]
C - 辅音
V - 元音
[ ] - 其中方括号表示其内容的任意存在。
我达到了这段代码:
import re
def find_m(word):
if re.match("[^aeiou]*?[aeiou]*?",word):
print "PASS"
else:
print "FAIL"
find_m("tr")
find_m("ee")
find_m("tree")
find_m("y")
find_m("by")
find_m("trouble")
find_m("oats")
find_m("trees")
find_m("ivy")
find_m("aaabbbbaaa")
Run Code Online (Sandbox Code Playgroud)
但它适用于所有情况,我需要一个正确的表达式来提供所需的结果。
问题:
我有一个列表列表,rowData其中包含表格格式的其他列表,即
rowData = [strLabel, intA, intB]
Run Code Online (Sandbox Code Playgroud)
我想rowData根据值和的总和进行排序intAintB
因此,如果
rowData = [ ['hi', 0, 1],
['how', 0, 0],
['ru', 2, 2] ]
Run Code Online (Sandbox Code Playgroud)
我想结束
rowData = [ ['how', 0, 0],
['hi', 0, 1],
['ru', 2, 2] ]
Run Code Online (Sandbox Code Playgroud)
当前解决方案
目前,我有一个单独的列表sums对应于对应于第二和第三个"列"的总和intA及intB.我的解决方案
[rowData[i] for i in [sums.index(j) for j in sorted(sums)]]
Run Code Online (Sandbox Code Playgroud)
经过一段时间的搜索后,这是对我有意义的唯一方法,但我对其效率低下充满信心,并且想知道与其他(希望是短期的)替代方案相比效率是多么低效.
我有这段代码生成以下输出:
result = []
with open('fileA.txt') as f:
for line in f:
if line.startswith('chr'):
label = line.strip()
elif line[0] == ' ':
# short sequence
length = len(line.strip())
# find the index of the beginning of the short sequence
for i, c in enumerate(line):
if c.isalpha():
short_index = i
break
elif line[0].isdigit():
# long sequence
n = line.split(' ')[0]
# find the index of the beginning of the long sequence
for i, c in enumerate(line):
if c.isalpha():
long_index = i …Run Code Online (Sandbox Code Playgroud) import pdb
print("program started")
c=100
d=200
pdb.set_trace()
def fun(a,b):
print a,b
return a+b
fun(c,d)
for i in [1,2,3,4,5]:
print 10/i
print ("other statements in program")
print ("program ended")
Run Code Online (Sandbox Code Playgroud) 我想制作包含n个元素的嵌套列表,其中包含给定列表的所有排列.
预期输出如下:
n=3
print perm(n, [1,2])
[[1,1,1],[1,1,2],[1,2,1],[1,2,2],[2,1,1],[2,1,2],[2,2,1],[2,2,2]]
Run Code Online (Sandbox Code Playgroud)
我怎么能写一个python代码来做同样的事情.
我正在开发一个新类,并发现长度不起作用.这是代码和建议:
class Queue:
def __init__(self):
self.queue = []
self.out_stack = []
def enqueue(self, other='string'):
self.queue.append(other)
def __len__(self):
len(self.queue)
Run Code Online (Sandbox Code Playgroud) 我对 Python 很陌生,正在尝试使用 Python 在 Raspberry Pi2 中制作一个小项目
目前我有 2 个代码文件run1.py和run2.py
我想写一个if-else条件Project.py,但我不知道如何正确编写代码......
if (condition is true) ----> run the code from file "run1.py"
else ----> run the code from file "run2.py"
Run Code Online (Sandbox Code Playgroud)
__main__是关于“ ”或“ ”的主题import os吗?我也想了解它是如何工作的,但还没有真正理解。
谢谢
我正在使用python 2.7和opencv.
我正在尝试使用:
cv2.resize(src, dst, Size(), 0.5, 0.5, interpolation=cv2.INTER_LINEAR);
Run Code Online (Sandbox Code Playgroud)
取自这里.但是,当我运行此代码时,我得到了NameError: global name 'Size' is not defined.
你能帮我吗?
lst = [[170,True],[210,False],[410,True],[170,True]...]
Run Code Online (Sandbox Code Playgroud)
从这个列表我需要提取.
sublist1 = [170,210,410,170,..]
sublist2 = [True, False, True, True..]
Run Code Online (Sandbox Code Playgroud)
我怎么能得到这个?
python ×9
list ×2
python-2.7 ×2
class ×1
if-statement ×1
methods ×1
opencv ×1
pdb ×1
raspberry-pi ×1
regex ×1
sorting ×1