我有字符串列表,需要找到遵循此逻辑的所有内容
twodigits.twodigits
Example| Correct?
----------- | ------
03.38 | Yes
10.59 | Yes
10.23 m3/h | No
50.8 mm | No
我只想找“。”但是没有用......
for i in elements:
id = i.Id
if i.Text[2:3] == '.':
i.Text= ''
atext1.append(i.Text)
我发现了这个,但不确定如何在我的代码中使用它?
str_extract_all(text, "\\d{2}.\\d{2}") %>%
我目前的答案代码:
for i in elements:
id = i.Id
match = search('\d{2}\.\d{2}$',i.strip())
if match:
atext1.append(match.string)
i.Text= ''
#atext1.append(i.Text)
else:
atext2.append(i.Text)
atext.append(i.Text)
另一次尝试但仍然无效
for i in elements:
id = i.Id
k = i.Text
atext3.append(k)
match=search('\d{2}\.\d{2}$',k.strip())
if match: atext4.append(match.string)
i.Text= ''
这一个也没有成功
for i in elements:
id = i.Id
k = i.Text
atext3.append(k)
if k.split('.')[0:2].isdigit():
i.Text= ''
atext3.append(i.Text)
我最终得到了这个。谢谢大家的帮助
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
from re import search
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
#The inputs to this node will be stored as a list in the IN variable.
#from re import search
atext3 = []
atextTrue = []
atextFalse = []
atextAll = []
#unwrap all elements to use with API
elements = []
for i in IN[0]:
elements.append(UnwrapElement(i))
TransactionManager.Instance.EnsureInTransaction(doc)
for i in elements:
atext3 = i.Text
match = search('\d{2}\.\d{2}$', atext3.strip())
if match:
i.Text = 'deletedTEXT'
atextTrue.append(i.Text)
else:
atextFalse.append(i.Text)
atextAll.append(i)
TransactionManager.Instance.TransactionTaskDone()
#Assign your output to the OUT variable
OUT = atextTrue, atextFalse, atextAll
答案 0 :(得分:0)
def fits_pattern(s):
s = s.strip()
return len(s) == 5 and s[2] == '.' and (s[:2] + s[-2:]).isdigit()
for test_string in ['03.38', '10.59 ', '10.23 m3/h', '50.8 mm',
'ab.cd', '12.3d', '1234', '12345', '', ' 12.34 ']:
print(repr(test_string))
print(fits_pattern(test_string))
输出:
'03.38'
True
'10.59 '
True
'10.23 m3/h'
False
'50.8 mm'
False
'ab.cd'
False
'12.3d'
False
'1234'
False
'12345'
False
''
False
' 12.34 '
True
正则表达式的解决方案是:
import re
def fits_pattern(s):
return re.match(r'\s*\d\d\.\d\d\s*', s)
\s*
:0个或更多空格(包括换行符,制表符等)
\d
:一个数字
\.
:文字.
,因为.
本身就是不同的东西。
r'...'
:原始字符串文字,对正则表达式非常有用,可避免在字符串文字中加载\
个字符。
答案 1 :(得分:0)
from re import search
l=['03.38', '10.59 ', '10.23 m3/h', '50.8 mm']
atext1=[]
for i in l:
match=search('\d{2}\.\d{2}$',i.strip())
if match: atext1.append(match.string)
print(atext1) #['03.38', '10.59']
\d{2}\.\d{2}$
会将l
中结束的任何元素与两位数,一个句点和两位数匹配。 i.strip()
删除了前导空格和尾随空格 - '10.59 '
将成为'10.59'
并匹配此正则表达式。
答案 2 :(得分:0)
for i in elements:
if not len(list(i.Text)) > 5:
if len(i.split('.')) > 0:
if i.split('.')[0:2].isdigit():
if i.split('.')[3:].isdigit():
print("Yes")
答案 3 :(得分:0)
# being that you provided no details on how this info is stored, other than "a list of strings", I'm assuming it's stored like so:
lst_info = ['11.11','1.11','22.11','22.22']
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def function(p):
new_lst = []
piece = []
for e in p:
if len(e) > 4:
if (is_number(e[0:2]), is_number(e[3:]), e[2] == '.'):
piece.append([str(e)])
piece.append([': Yes'])
new_lst.append(piece)
piece = []
else:
piece.append([str(e)])
piece.append([': No'])
new_lst.append(piece)
piece = []
result = ''
for e in new_lst:
result += (' ' * (9 - len(str(e[0])))) + (str(e[0]) + ' | ' + str(e[1]) + '\n')
return result
print function(lst_info)
# prints
['11.11'] | [': Yes']
['1.11'] | [': No']
['22.11'] | [': Yes']
['22.22'] | [': Yes']