假设我有一个字符串模板,例如,
string="This is a {object}"
Run Code Online (Sandbox Code Playgroud)
现在我通过格式化这个字符串来创建两个(或更多)字符串,即,
string.format(object="car")
=>"This is a car"
string.format(object="2020-06-05 16:06:30")
=>"This is a 2020-06-05 16:06:30"
Run Code Online (Sandbox Code Playgroud)
现在我以某种方式丢失了原始字符串。有没有办法使用我现在拥有的 2 个新字符串找出原始字符串?
注意:我有这些字符串的数据集,这些字符串是从模板创建的,但原始模板因编辑而丢失。从新模板创建新字符串并放入相同的数据集中。我曾尝试使用一些基于 ML 的方法,但在一般情况下似乎不起作用。我正在寻找一种算法来返回原始字符串,它可以是一个或一组字符串,以防模板已多次更改。
我定义了一系列 python 对象,class dofiles其中有一个名称和一些空列表。
class dofile:
name = ""
lines = []
inputs = []
outputs = []
intermediates = []
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码来循环dofile调用这些对象的列表dofiles(在我运行填充我知道可以正常工作的每个对象的行列表的代码之后)。有问题的代码检测到短语 using 及其后面的单词,然后应该将该单词附加到每个对象的输入列表中。
for dofile in dofiles:
for line in dofile.lines:
# using
using_tups = re.findall(r'(using)\s([^\s,]+)', line)
for tuple in using_tups:
dofile.inputs.append(tuple[1])
Run Code Online (Sandbox Code Playgroud)
但是,我最终将单词附加到 dofiles 中所有 dofile 对象的输入列表。(例如:print(dofiles[0].inputs)和print(dofiles[1].inputs)虽然扫描不同的文件,但返回相同的内容)。
我是否遗漏了 Python 中对象、列表或循环的工作原理?
我试过了,(dofile.inputs).append(tuple[1])但似乎没什么区别。
谢谢。
I want to define a function that takes a list as its argument and then returns the elements in order.
For example:
def iterator(lis):
for e in range(len(lis)):
return lis[e]
l=input("Enter list elements:").split()
x=iterator(l)
print(x)
Run Code Online (Sandbox Code Playgroud)
But this just returns the first value of the list as:
Enter list elements:12 23 34
12
Run Code Online (Sandbox Code Playgroud)
How can I print all the values in successive lines?
我在 Python 中有一个单词列表:
['apple', 'ball', 'app']
Run Code Online (Sandbox Code Playgroud)
我想删除所有不以'app'.
如何生成列表['apple', 'app']?
我知道使用startswith,但我不确定列表理解会是什么样子。