我试图以...的形式执行查询
SELECT col2 FROM tab WHERE col1 IN (val1, val2, val3...)
Run Code Online (Sandbox Code Playgroud)
...值存储在任意长度的Python列表/元组中.我似乎无法找到一种"干净"的方式来做到这一点.
>>> db = connect(":memory:")
>>> db.execute("CREATE TABLE tab (col1 INTEGER, col2 TEXT)")
>>> db.execute("INSERT INTO tab VALUES(1,'one')")
>>> db.execute("INSERT INTO tab VALUES(2,'two')")
>>> db.execute("INSERT INTO tab VALUES(3,'three')")
>>> db.execute("INSERT INTO tab VALUES(4,'four')")
>>> db.execute("INSERT INTO tab VALUES(5,'five')")
>>> db.commit()
# Expected result
>>> db.execute("SELECT col2 FROM tab WHERE col1 IN (1,3,4)").fetchall()
[(u'one',), (u'three',), (u'four',)]
>>> vals = (1,3,4)
>>> db.execute("SELECT col2 FROM tab WHERE col1 IN (?)", vals).fetchall() …Run Code Online (Sandbox Code Playgroud) 我目前正在手动执行bash命令,方法是在python代码中输入shell.
如何用pythonic方式做到这一点?
我目前正在使用os.system函数执行命令,如;
os.system('sudo add-apt-repository ppa:ondrej/php')
os.system('sudo apt-get update')
os.system('sudo apt-get install php7.0 php5.6 php5.6-mysql php-gettext php5.6-mbstring php-mbstring php7.0-mbstring php-xdebug libapache2-mod-php5.6 libapache2-mod-php7.0')
os.system('sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart')
Run Code Online (Sandbox Code Playgroud) Matplotlib 或 Seaborn 箱线图给出了第 25 个百分位数和第 75 个百分位数之间的四分位距。有没有办法为 Boxplot 提供自定义四分位距?我需要得到箱线图,使得四分位距介于 10% 和 90% 之间。在谷歌和其他来源上查找,开始了解在箱线图上获取自定义胡须,而不是自定义四分位距。希望能在这里得到一些有用的解决方案。
如何根据每个列表的第一个元素对列表列表进行排序?
例如,给出这个未排序的列表:
[[1,4,7],[3,6,9],[2,59,8]]
Run Code Online (Sandbox Code Playgroud)
排序结果应为:
[[1,4,7],[2,59,8],[3,6,9]]
Run Code Online (Sandbox Code Playgroud) Python Collection Counter.most_common(n)方法返回前n个元素及其计数。但是,如果两个元素的计数相同,如何返回按字母顺序排序的结果?
例如:对于像这样的字符串BBBAAACCD,对于“最常见的2个”元素,我希望结果是指定的n = 2:
[('A', 3), ('B', 3), ('C', 2)]
Run Code Online (Sandbox Code Playgroud)
并不是:
[('B', 3), ('A', 3), ('C', 2)]
Run Code Online (Sandbox Code Playgroud)
请注意,虽然A与B具有相同的频率,A来之前B,因为它到来之前在结果列表中B按字母顺序排列。
[('A', 3), ('B', 3), ('C', 2)]
Run Code Online (Sandbox Code Playgroud)
我该如何实现?
我想为站的每个唯一值创建一个新的数据框。
我在下面尝试过,它只给了我在数据框中更新的最后一个站数据 = tai_new.i
tai['station'].unique() has 500 values.
for i in tai['station'].unique():
tai_new.i = tai[tai_2['station'] ==i]
Run Code Online (Sandbox Code Playgroud)
另一种方法是创建一个单独的列表
tai_stations = tai['station'].unique()
Run Code Online (Sandbox Code Playgroud)
然后创建两个循环,但是我不想输入 500 (IF) 条件。
我有从服务提供商那里得到的样本响应。他们定义了已经定义的标签(我已经从FIXimate检查过)。
8=FIXT.1.19=108135=IB49=NSECM56=MTBM34=1924=1115=21828=61826=Nifty CPSE1815=2294.51816=2310.151817=2286.551818=2276.751819=2294.21820=1967852601488651821=262231822=257851823=0.771824=2799.551825=2139.51827=-1826=Nifty GrowSect 151815=7037.751816=7070.351817=7023.651818=6990.61819=7042.91820=7768216282514231821=782041822=750561823=0.751824=7354.551825=5942.151827= 1826=Nifty50 Value 201815=5444.71816=5465.61817=5417.41818=5413.61819=5425.91820=1.90667550936387E+151821=1348851822=1341791823=0.231824=5563.81825=4171.651827=-1826=Nifty Mid Liq 151815=4133.551816=4206.551817=4133.551818=4107.21819=4205.41820=1573956262705091821=428041822=442911823=2.391824=4677.051825=3770.551827=-1826=Nifty Pvt Bank1815=15385.21816=15415.51817=15339.751818=15248.81819=15377.71820=9015155094048031821=589741822=592681823=0.851824=16152.151825=13298.31827=+1826=NIFTY MIDCAP 1001815=19184.851816=19326.31817=19184.851818=19046.551819=19314.251820=7127426889917151821=1858871822=1884631823=1.411824=21840.851825=17700.91827=-10=174
问题是我无法阅读重复组。我创建一个自定义组
noIndexRecords = int(message.getField(1828))
if noIndexRecords > 0:
group = fix.Group(1828, 1826, )
message.getGroup(num=1, group=group)
Run Code Online (Sandbox Code Playgroud)
但这向我显示了一个FieldNotFound错误。我对规格文件进行了如下更改FIX50SP2.xml:
新增栏位:
<field number='1815' name="OpenIndex" type="PRICE"/>
<field number='1816' name="HighIndex" type="PRICE"/>
<field number='1817' name="LowIndex" type="PRICE"/>
<field number='1818' name="CloseIndex" type="PRICE"/>
<field number='1819' name="IndexValue" type="PRICE"/>
<field number='1820' name="MarketCapitalization" type="DOUBLE"/>
<field number='1821' name="NoOfDownMoves" type="INT"/>
<field number='1822' name="NoOfUpMoves" type="INT"/>
<field number='1823' name="PercentageChange" type="DECIMAL"/>
<field number='1824' name="YearlyHigh" type="PRICE"/> …Run Code Online (Sandbox Code Playgroud) 我想测试 Python 代码是否可以正确使用符号链接。如何os.symlink()在基于pathlib.PathPython2/3 兼容方式的伪造文件系统中创建符号链接(例如相当于如何使用)?
我正在尝试使用cvxpy优化包解决数独。我对优化和 cvxpy 都很陌生。
约束条件是:
所以,这是我的代码:
from cvxpy import *
import numpy as np
x = Variable((9, 9), integer=True)
obj = Minimize(sum(x)) #whatever, if the constrains are fulfilled it will be fine
const = [x >= 1, #all values should be >= 1
x <= 9, #all values should be <= 9
sum(x, axis=0) == 45, # sum of all rows should …Run Code Online (Sandbox Code Playgroud) 所以我试图创建一个函数,将列表与可调整比率的值分开。
为了将列表分成两半,我有这个函数:
def list_splitter(list_to_split):
half = len(list_to_split) // 2
return list_to_split[:half], list_to_split[half:]
Run Code Online (Sandbox Code Playgroud)
其中 list_to_split 有 1000 个对象。但我想做这样的事情:
def list_splitter(list_to_split, ratio):
part1 = len(list_to_split) * ratio
part2 = 1 - ratio
return list_to_split[:part1], list_to_split[part2:]
Run Code Online (Sandbox Code Playgroud)
例如,我希望能够设置比率 = 0.75,以便在第一部分中添加 0.75%(750 个对象),在另一部分中添加 250 个。
python ×10
python-3.x ×2
algorithm ×1
bash ×1
counter ×1
cvxpy ×1
fix-protocol ×1
list ×1
loops ×1
matplotlib ×1
optimization ×1
pathlib ×1
quickfix ×1
seaborn ×1
split ×1
sqlite ×1
sudoku ×1