我如何在django过滤器中执行"或"操作.
基本上,我希望能够列出用户添加的项目(它们被列为创建者)或项目已被批准
所以我基本上需要选择
item.creator = owner or item.moderated = False
Run Code Online (Sandbox Code Playgroud)
我如何在django中执行此操作(最好使用过滤器/查询集)
在R(感谢magritrr)中,您现在可以通过更多功能管道语法执行操作%>%.这意味着不是编码:
> as.Date("2014-01-01")
> as.character((sqrt(12)^2)
Run Code Online (Sandbox Code Playgroud)
你也可以这样做:
> "2014-01-01" %>% as.Date
> 12 %>% sqrt %>% .^2 %>% as.character
Run Code Online (Sandbox Code Playgroud)
对我来说,这更具可读性,这扩展到数据框之外的用例.python语言是否支持类似的东西?
这个问题最初问(错误地)是什么"|" 在Python中,当实际问题是关于Django时.这个问题得到了我希望保留的Triptych的精彩答案.
我想知道是否有一种有效的方法来获得X的行数在行的子集的上下。我在下面创建了一个基本的实现,但是我敢肯定有更好的方法。我关心的子集是buyindex,它是具有购买信号的行的索引。我想在sellindex上方和下方获取几行,以验证我的算法是否正常运行。我如何有效地做到这一点?我的路好像回旋处。
buyindex = list(data2[data2['buy'] == True].index)
print buyindex [71, 102, 103, 179, 505, 506, 607]
buyindex1 = map(lambda x: x + 1, buyindex)
buyindex2 = map(lambda x: x - 1, buyindex)
buyindex3 = map(lambda x: x - 2, buyindex)
buyindex4 = map(lambda x: x + 2, buyindex)
buyindex.extend(buyindex1)
buyindex.extend(buyindex2)
buyindex.extend(buyindex3)
buyindex.extend(buyindex4)
buyindex.sort()
data2.iloc[buyindex]
Run Code Online (Sandbox Code Playgroud)
UPDATE-这是数据的结构。我有“购买”的索引。但我基本上想获得高于或低于购买量的几个指数。
VTI upper lower sell buy AboveUpper BelowLower date tokens_left
38 61.25 64.104107 61.341893 False True False True 2007-02-28 00:00:00 5
39 61.08 64.218341 61.109659 False True …Run Code Online (Sandbox Code Playgroud) 在交互式解释器中,如果您键入以下内容,您可以看到一些非常有趣的东西:
1) help()
2) modules
3) __builtin__
在阅读输出一段时间后,我遇到了以下几行class bool:
__or__(...)
x.__or__(y) <==> x|y
Run Code Online (Sandbox Code Playgroud)
然后是:
__ror__(...)
x.__ror__(y) <==> y|x
Run Code Online (Sandbox Code Playgroud)
最后一种方法似乎描述了反向或.为什么这种方法存在?什么可能导致__or__(...)返回不同的东西__ror__(...)?
我是 PyQt 的新手。我首先在Qt Designer中做了一个对话框,在一个QListWidget中实现了简单的增删改查操作。除了编辑之外,我的所有操作都很有魅力,所以我做了很多关于如何使项目可编辑的搜索,但结果并不令人满意。我可以使用openPersistentEditorand进行编辑closePersistentEditor,但它的行为方式并不是我想要的。我只是希望在双击或按下编辑按钮时可以编辑这些项目,就像在普通的 gui 中一样。
我的 Qt 设计器代码是:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'myDialog.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(358, 226)
self.widget = QtWidgets.QWidget(Dialog)
self.widget.setGeometry(QtCore.QRect(10, 10, 341, 201))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.mylist = …Run Code Online (Sandbox Code Playgroud) 我知道p=re.compile('aaa|bbb')可以使用,但是我想p = re.compile('aaa|bbb')使用变量进行重写,例如
A = 'aaa'
B = 'bbb'
p = re.compile(A|B)
Run Code Online (Sandbox Code Playgroud)
但这不起作用。我该如何重写它以便使用变量(并且可以工作)?
在用于列表和元组的python切片符号中,我注意到管道字符不会引发错误。我不确定它到底能做什么,因为结果似乎有些随机。
testA = [1,2,3,4,5,6,7,8,9]
Run Code Online (Sandbox Code Playgroud)
testA[0:3]
Run Code Online (Sandbox Code Playgroud)
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
testA[4:6]
Run Code Online (Sandbox Code Playgroud)
[5,6]
testA[0:3|4:6]
Run Code Online (Sandbox Code Playgroud)
[1,7]
有任何想法吗?
最近在学习apache beam,发现了一些像这样的python代码:
lines = p | 'read' >> ReadFromText(known_args.input)
# Count the occurrences of each word.
def count_ones(word_ones):
(word, ones) = word_ones
return (word, sum(ones))
counts = (lines
| 'split' >> (beam.ParDo(WordExtractingDoFn())
.with_output_types(unicode))
| 'pair_with_one' >> beam.Map(lambda x: (x, 1))
| 'group' >> beam.GroupByKey()
| 'count' >> beam.Map(count_ones))
Run Code Online (Sandbox Code Playgroud)
来自: https: //github.com/apache/beam/blob/master/sdks/python/apache_beam/examples/wordcount.py#L92
|python中and的语法和用法是什么>>?
当我在python shell中执行以下操作时!
>> print 2 | 4
>> 6
Run Code Online (Sandbox Code Playgroud)
为什么python中的管道符号会添加到整数?
python ×9
apache-beam ×1
built-in ×1
django ×1
pandas ×1
pipe ×1
pipeline ×1
pyqt ×1
python-2.7 ×1
qlistwidget ×1
qt ×1
regex ×1
stocks ×1
syntax-rules ×1