鉴于:
x = ['w', 'e', 's', 's', 's', 'z','z', 's']
Run Code Online (Sandbox Code Playgroud)
每次出现都s出现在以下指数:
1st:2
2nd:3
3rd:4
4th:7
如果我这样做,x.index('s')我将得到第一个索引.
我如何得到第四个指数s?
for i in range (0, 81):
output = send command
while True:
last_byte = last_byte - offset
if last_byte > offset:
output = send command
i+
else:
output = send command
i+
break
Run Code Online (Sandbox Code Playgroud)
我想在每次执行send命令时增加迭代器.现在它只在执行for循环时增加1.请指教
for i in range(0,10):
print(i)
i +=2
print("increased i", i)
Run Code Online (Sandbox Code Playgroud)
我运行了这段代码,它从0到9生成.我原以为它会将迭代器增加2.
我试图找到一个简洁的解决方案,但我正在以相同的方式切割几个相同形状的2D阵列.我通过定义一个包含'x,y'中心的列表尽可能地整理它,例如cpix = [161, 134] 我想要做的是不必像这样写三次切片:
a1 = array1[cpix[1]-50:cpix[1]+50, cpix[0]-50:cpix[0]+50]
a2 = array2[cpix[1]-50:cpix[1]+50, cpix[0]-50:cpix[0]+50]
a3 = array3[cpix[1]-50:cpix[1]+50, cpix[0]-50:cpix[0]+50]
Run Code Online (Sandbox Code Playgroud)
只是有预定义的东西(比如可能是面具?)所以我可以做一个
a1 = array1[predefined_2dslice]
a2 = array2[predefined_2dslice]
a3 = array3[predefined_2dslice]
Run Code Online (Sandbox Code Playgroud)
这是numpy支持的东西吗?
我想用两个创建一个类:collections.OrderedDict和collections.DefaultDict.这样我就可以获得一个有序的字典,并为访问的现有密钥设置一个默认值.有什么方法可以做到这一点?
我的解决方案是围绕我上面提到的两个类创建另一个类.由于我认为每个类中的方法具有相同的名称,这会导致错误吗?
from collections import defaultdict, OrderedDict
class owndic(OrderedDict, defaultdict):
pass
Run Code Online (Sandbox Code Playgroud)
生产
TypeError: multiple bases have instance lay-out conflict
Run Code Online (Sandbox Code Playgroud)
干杯!
考虑以下代码:
class Person(object):
def sayHello(self):
return 'Hello'
print(Person().sayHello is Person().sayHello)
Run Code Online (Sandbox Code Playgroud)
我希望它能显示出真实.为什么显示False?
import numpy as np
print max([np.nan, 1, 2, 3, 4])
print max([1, 2, 3, 4, np.nan])
print max([1, 2, 3, np.nan, 4])
Run Code Online (Sandbox Code Playgroud)
第一个将打印nan作为列表的最大值
第二个将打印4作为列表的最大值
第三个将打印4作为列表的最大值
有这个问题的解决方案吗?让所有数学函数都忽略南?
我正在尝试使用数据提供程序以降序日期顺序为每个用户显示数据库,这适用于控制器上的Yii 1:
$DataProvider = new CActiveDataProvider('ModelName', array(
'criteria' => array(
'condition' => 'user_id=' . Yii::app()->user->id,
'order' => 'submitted_dt DESC',
),
'pagination' => array(
'pageSize' => 20,
),
));
Run Code Online (Sandbox Code Playgroud)
我在Yii 2中尝试这个:
$DataProvider = new ActiveDataProvider([
'query' => ModelName::find(),
'criteria' => [
'condition' => 'user_id=' . Yii::$app->user->identity->id,
'order' => 'submitted_dt DESC',
],
'pagination' => [
'pageSize' => 20,
],
]);
// get posts in the current page
$Model= $DataProvider->getModels();
Run Code Online (Sandbox Code Playgroud)
我得到的错误是未知属性:yii\data\ActiveDataProvider::criteria.那么设置这个条件和顺序的方法是什么?欢迎所有建议
我试图按顺序排序数组.但是代码的错误高于:
a=[]
a=map(int, input().split(' '))
a.sort()
print (a)
Run Code Online (Sandbox Code Playgroud)
帮帮我吧
假设
A = [9, 5, 34, 33, 32, 31, 300, 30, 3, 256]
Run Code Online (Sandbox Code Playgroud)
我想只对列表中的特定部分进行排序.例如,在这里我只想排序,[300, 30, 3]以便整个列表变为:
A = [9, 5, 34, 33, 32, 31, 3, 30, 300, 256]
Run Code Online (Sandbox Code Playgroud)
假设B = [300, 30, 400, 40, 500, 50, 600, 60]在排序之后它应该是B = [30, 300, 40, 400, 50, 500, 60, 600].
主要想法if the leftmost digit is same 300, 30, 30和最正确的数字只包含zeros然后我们应该按递增顺序排列它.
另一个例子:
A = [100, 10, 1, 2000, 20, 2]
Run Code Online (Sandbox Code Playgroud)
排序后应该是 A = [1, 10, …
你知道一种更简单的方法来获得与此相同的结果吗?我有这个代码:
color1 = input("Color 1: ")
color2 = input("Color 2: ")
if ((color1=="blue" and color2=="yellow") or (color1=="yellow" and color2=="blue")):
print("{0} + {1} = Green".format(color1, color2))
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
if (color1 + color2 =="blueyellow" or color1 + color2 =="yellowblue")
Run Code Online (Sandbox Code Playgroud) python if-statement simplify conditional-statements python-3.x