Swi-Prolog从列表列表中选择列表

时间:2018-05-05 11:10:12

标签: prolog

你好,我正在做一个关于prolog的项目,并且仍然掌握着这门语言。 我目前有一个列表列表,其中子列表有坐标。

我的目的是选择具有特定坐标X的子列表,然后将该列表从第一个元素切割为X.(只有一个列表)

我认为首先问题是选择带成员的子列表。所以我试着这么做。

sublist([],_, []).
sublist([H|R],Pos, [H|Aux]) :-
    member(Pos,H), !,
    sublist(R,Pos, Aux).
sublist([H|R],Pos, Aux) :-
    sublist(R,Pos,Aux).

这不起作用:

?- sublist([[(1,2),(1,3),(3,5)],[(1,5),(1,2)]], (1,5), X).
X = [].

第一阶段我想要的是:

?- sublist([[(1,2),(1,3),(3,5)],[(1,5),(1,2)]], (1,5), X).
X = [(1,5),(1,2)].

(之后,当我选择了子列表时,我计划做一个类似的事情,我将元素添加到空列表中,直到元素等于X)。

最终结果将是:

?- sublist([[(1,2),(1,3),(3,5)],[(1,5),(1,2)]], (1,5), X).
X = [(1,5)].

我做错了什么?我还在学习Prolog的思考,所以我为明显的逻辑错误道歉。

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题:

Traceback (most recent call last):
  File "C:\Users\tlsdy\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 2525, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '(df["Unnamed: 13"] == "3") & (df["Unnamed: 15"] == "")'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/python_project_ab/amazon/property.py", line 201, in <module>
    print(df[find_key_and_val])
  File "C:\Users\tlsdy\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 2139, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\tlsdy\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 2146, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\tlsdy\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\generic.py", line 1842, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\tlsdy\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals.py", line 3843, in get
    loc = self.items.get_loc(item)
  File "C:\Users\tlsdy\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 2527, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '(df["Unnamed: 13"] == "3") & (df["Unnamed: 15"] == "")'

sublist(L, Elem, M) :- member(M, L), member(Elem, M). 包含一个列表L,其中包含和元素M

给出了结果:

Elem