以下列格式列出python词典.您如何进行搜索才能找到特定名称?
label = [{'date': datetime.datetime(2013, 6, 17, 8, 56, 24, 2347),
'name': 'Test',
'pos': 6},
{'date': datetime.datetime(2013, 6, 17, 8, 56, 24, 2347),
'name': 'Name 2',
'pos': 1}]
Run Code Online (Sandbox Code Playgroud)
以下不起作用:
if 'Test' in label[name]
'Test' in label.values()
Run Code Online (Sandbox Code Playgroud) 我能够整理一个简化的完整History.js示例,使用三个链接从整页加载内容片段,而无需更新页面和更新浏览器历史记录.
以下是相关的代码片段 - 这里有一个完整的工作示例http://jsfiddle.net/PT7qx/show
<a href="/page-header">Page Header</a>
<a href="/login-form">Login Form</a>
<a href="/nothing">Nothing</a>
<script>
var History = window.History;
if (!History.enabled) {
return false;
}
History.Adapter.bind(window, 'statechange', function() {
var State = History.getState();
History.log(State.data, State.title, State.url);
if (State.title == 'Page Header') {
$('#content').load('/user/login/ .pageHeader > *');
}
if (State.title == 'Login Form') {
$('#content').load('/user/login/ #common-form > *');
}
if (State.title == 'Nothing') {
$('#content').empty()
}
});
$('body').on('click', 'a', function(e) {
var urlPath = $(this).attr('href');
var Title = $(this).text();
History.pushState(null, Title, urlPath); …Run Code Online (Sandbox Code Playgroud) 请参阅以下示例:
(1) #outputs 1
Run Code Online (Sandbox Code Playgroud)
但是如果我添加逗号,根据Python文档:)它是正确的:)
(1,) #output (1,)
Run Code Online (Sandbox Code Playgroud)
这对我来说太奇怪了.有谁能解释一下?
一个相关的问题:Python是否有办法知道何时(1)应该是元组(1,而不是1)?
感谢您将来的回复.
有没有办法按类型删除嵌套列表中的项目(1 [2] 3(4 [5] 6))变为(1 3(4 6))如果我只想删除向量?
使用postwalk,我可以用nil替换所有向量,但我找不到删除它们的方法.
(clojure.walk/postwalk
#(if (vector? %) nil %) '(1 [2] 3 (4 [5] 6)))
=>(1 nil 3 (4 nil 6))
Run Code Online (Sandbox Code Playgroud) 假设你有三个arity 1,2和3的函数,如下所示:
(defn I [x] x)
(defn K [x y] x)
(defn S [x y z] (x z (y z)))
Run Code Online (Sandbox Code Playgroud)
clojure是否具有用于评估的评估函数或习语:
(I K S I I) as (I (K (S (I (I)))))
Run Code Online (Sandbox Code Playgroud)
返回arity 2的一个parital功能?
我正在考虑创建一个宏,它可以采用上面的简单函数定义,并将它们扩展为可以返回部分结果的多元函数.如果已经有内置或惯用的方法来实现这一点,我不想创建宏.
以下是扩展宏对上述函数的期望:
(defn I
([x] I x)
([x & more] (apply (I x) more)))
(defn K
([x] (partial K x))
([x y] x)
([x y & more] (apply (K x y) more)))
(defn S
([x] (partial S x))
([x y] (partial S x y))
([x y …Run Code Online (Sandbox Code Playgroud) 我可以将字符串转换为序列,然后将该序列转换为表示序列的字符串.
user=> (str (first (list (seq "(xy)z"))))
"(\\( \\x \\y \\) \\z)"
Run Code Online (Sandbox Code Playgroud)
我也可以在上面的表单中插入apply来获取原始字符串
user=> (apply str (first (list (seq "(xy)z"))))
"(xy)z"
Run Code Online (Sandbox Code Playgroud)
但有没有办法将表示序列的字符串转换为字符串表示的序列?如:
"(\\( \\x \\y \\) \\z)"
user=> (some-fn2 "(\\( \\x \\y \\) \\z)")
(\( \x \y \) \z \))
Run Code Online (Sandbox Code Playgroud) 从文档中,all相当于:
def all(iterable):
for element in iterable:
if not element:
return False
return True
Run Code Online (Sandbox Code Playgroud)
那为什么我得到这个输出:
# expecting: False
$ python -c "print( all( (isinstance('foo', int), int('foo')) ) )"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'foo'
Run Code Online (Sandbox Code Playgroud)
什么时候:
# expecting: False
$ python -c "print( isinstance('foo', int) )"
False
Run Code Online (Sandbox Code Playgroud) 假设我将x定义为符号函数foo
(defn foo [x] x)
(def x foo)
Run Code Online (Sandbox Code Playgroud)
如果只给出x,可以发现名称"foo"吗?
在foo中有没有办法查找函数x的名称 - 在这种情况下是"foo"?
(foo x)
Run Code Online (Sandbox Code Playgroud)
是否有可能创建一个功能,如:
(get-fn-name x)
foo
Run Code Online (Sandbox Code Playgroud) 我有一个列表列表,需要删除所有具有相同第三元素的列表,并保存1.
例如:
x=[[1,2,3],[1,3,3],[5,6,3],[2,4,6],[8,5,9],[10,5,9]]
Run Code Online (Sandbox Code Playgroud)
可以变成:
x=[[1,2,3],[2,4,6],[8,5,9]]
Run Code Online (Sandbox Code Playgroud)
我试过一个解决方案,使用lambda消除所有具有相同第二个索引的列表,但我不知道如何保存每个元素之一,如set()呢 - 我得到:
x=[[2,4,6]]
Run Code Online (Sandbox Code Playgroud) 我有一个列表,其格式与此类似:
list1 = ['random words go', 'here and','the length varies','blah',
'i am so confused', 'lala lala la']
Run Code Online (Sandbox Code Playgroud)
什么代码适合返回列表中的每个第3项,包括第一个单词?这是预期的输出:
["random", "here", "length", "i", "confused", "la"]
Run Code Online (Sandbox Code Playgroud)
我在想我应该使用split功能,但我不知道该怎么做.有人也可以解释我是如何做到这一点所以整个列表不是那样的"部分"吗?相反,如果有意义的话,我怎么能把它变成一个长列表.
python ×5
clojure ×4
list ×2
ajax ×1
arity ×1
combinators ×1
dictionary ×1
escaping ×1
function ×1
history.js ×1
jquery ×1
lambda ×1
name-lookup ×1
nested-forms ×1
partial ×1
python-3.x ×1
sequence ×1
string ×1
symbols ×1
tuples ×1