- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 python 的新手,正在尝试生成语法中所有可能的句子。这是语法:
#set of non terminals
N = ('<subject>', '<predicate>', '<noun phrase>', '<noun>', '<article>', '<verb>', '<direct object>')
#set of teminals
T = ('the', 'boy', 'dog', 'bit')
#productions
P = [ ('Sigma', ['<subject>', '<predicate>']), \
('<subject>', ['<noun phrase>']), \
('<predicate>', ['<verb>']), \
('<predicate>', ['<verb>','<direct object>']), \
('<noun phrase>', ['<article>','<noun>']), \
('<direct object>', ['<noun phrase>']), \
('<noun>', ['boy']), \
('<noun>', ['dog']), \
('<article>', ['the']), \
('<verb>', ['bit']) ]
这是我的尝试,我正在使用一个队列类来有条不紊地实现它,
# language defined by the previous grammar.
Q = Queue()
Q.enqueue(['Sigma'])
found = 0
while 0 < len(Q):
print "One while loop done"
# Get the next sentential form
sf = Q.dequeue()
sf1 = [y for y in sf]
for production in P:
for i in range(len(sf1)):
if production[0] == sf1[i]:
sf[i:i+1] = [x for x in production[1]]
Q.enqueue(sf)
Q.printQ()
我进入了无限循环,而且我还面临一些浅深复制的问题,如果我更改 sf 的一个副本,队列中的所有内容也会更改。任何帮助表示赞赏,任何指示,提示都会很棒
这是预期的输出:
The dog bit the boy
The boy bit the dog
The boy bit the boy
The dog bit the dog
The dog bit
The boy bit
最佳答案
I am facing some issue with shallow-deep copy, if I change one copy of sf, everything in queue changes too
是的。在 Python 中,列表是具有自己标识的对象。所以:
Q.enqueue(['Sigma'])
创建一个(单元素)列表并将对它的引用排入队列。
sf = Q.dequeue()
从 Q 弹出该引用并将其分配给变量“sf”。
sf[i:i+1] = ...
对该列表(“sf”所指的列表)进行更改。
Q.enqueue(sf)
将对同一列表的引用排入队列。
所以只涉及一个列表对象,Q 只包含对它的多个引用。
相反,您可能希望 Q 中的每个条目都是对单独列表(句子形式)的引用,因此您必须为每次调用 Q.enqueue 创建一个新列表。
根据您修复该问题的方式,代码中可能存在也可能不存在其他问题。考虑:
(1) 每个句子都有多个推导,你只需要'找到'一个(例如,最左边的推导)。
(2) 通常,虽然不是在您的示例语法中,但产生式的 RHS 可能不止一次出现非终结符(例如,如果 COND 然后 STMT 否则 STMT),并且这些出现不需要派生相同的子 -表格。
(3) 一般来说,一个文法可以生成无限多的句子。
顺便说一句,在Python中复制一个列表,而不是说
copy = [x for x in original]
说起来更简单:
copy = original[:]
关于python - 试图生成一个简单形式语法的所有句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18951189/
这是一个新手理论问题 - 我刚刚开始使用 Python 并研究 Django 和 orm。问题:如果我开发我的对象并通过额外的开发修改基础对象结构、继承等 - Django 的 ORM 解决方案会自动
我正在使用带有服务器端处理器的 JavaScript 表单,并且我希望能够让表单根据下拉列表转到不同的电子邮件。我已经根据其他表格尽了最大努力,但似乎无法通过电子邮件。我已在电子邮件地址的选项标签下添
一个简单的问题:给定定义,(来自 Haskell SOE) do x — el; el\ ...; en => el »= \x — do e2\ ...; en 和: do let d
我是 Angular 5 的新手。我目前正在研究 Angular Reactive 表单。我有一个下面的 JSON 结构,我需要在从 FORM 获取值后发回 REST API。 JSON 结构: {
我是 Angular 5 的新手。我目前正在研究 Angular Reactive 表单。我有一个下面的 JSON 结构,我需要在从 FORM 获取值后发回 REST API。 JSON 结构: {
我有一个类型(称之为 A),我想创建一个 A -> A、A -> A -> A、A -> A -> A -> ... 等类型的函数的类型类.这不起作用: {-# LANGUAGE FlexibleIn
我正在使用 java 线程同时管理多个 (3) 程序。1 用于 Java swing 表单(绘制 UI 以进行输入),1 用于在系统托盘上设置图标(从 UI 获取输入后立即启动),1 用于处理输入并将
在当前的元素中,我在表单中遇到了一个问题。表单中标签的字体大小可能大于默认值。如果我把它举起来,那么右边的输入必须垂直居中。 我查看了 Bootstrap 和 Foundation,但都没有解决这个问
为了好玩,我使用了一段从 friend 那里得到的代码,并尝试创建一个包含用户名和密码的登录字段,但我很难获得单词旁边的字段。 username 这个词和你输入的框之间有很大的差距。密码也是如此。 这
我的表单中有一个嵌套的控制组,我想访问它们的表单状态值(如原始和有效)以动态显示验证错误。 是这样动态构建的 controlMap['password'] = this.password; contr
发送后我试图重置我的表单,但只有值设置为空。 component.html {{note.value?.length || 0}}/10
我正在尝试自定义 Stripe 结帐表单,但我不知道如何添加输入。我想添加“电话号码”和“姓名”以创建费用和客户。你知道我该怎么做吗? 这是我应该自定义的代码。 最佳答案 您将无法使用
所以我有这个需求,我想以表格的形式提交一个由五个记录组成的表单。这就是它的样子表: 这是对应的代码: Section Q.No Question
我有一个使用 react 形式和输入文本的情况。 我需要: 当用户输入时,根据输入的内容建议一个列表(我使用的是 ngx bootstrap typeahead); 仅当用户失去输入焦点时才验证输入字
我希望重构我的 Angular 项目中的大量组件,以具有强类型的 FormGroups、FormArrays 和 FormControls。 我只是在寻找一种实现强类型 react 形式的好方法。任何
我有事件表格: 'horizontal', 'fieldConfig' => [ 'template' => "{input}\n{hint}\n{error}",
是否有关于如何实现多选和响应式表单的示例? 我正在尝试在 multiselect-dropdown 上设置所选项目(从数据库中检索),它会更新显示的项目( View ),但会引发以下错误: core.
我想在表单中添加按钮以动态添加输入。但是我发现,如果我在表单中添加了一个仅记录到控制台的按钮(并且当我尝试添加输入时),它将记录日志,然后表单中断。我的Electron应用程序的前端窗口崩溃(不退出但
我有一个这样的表格 此表单位于指令内: angular.module('crowdcoreApp').directive('investorForm',function(){
我在 angularjs Controller 中调用的 $mdDialog 中有一个表单,如下所示: actions-controller.js function callForm() {
我是一名优秀的程序员,十分优秀!