我有以下字典,我只需要打印带有奇数(1、3 ...)的字典。我将如何去做?
zen = {
1: 'Beautiful is better than ugly.',
2: 'Explicit is better than implicit.',
3: 'Simple is better than complex.',
4: 'Complex is better than complicated.',
5: 'Flat is better than nested.',
6: 'Sparse is better than dense.',
7: 'Readability counts.',
8: 'Special cases aren't special enough to the rules.',
9: 'Although practicality beats purity.',
10: 'Errors should never pass silently.'
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有:
for c in zen:
print (c , zen[c][:])
Run Code Online (Sandbox Code Playgroud) 我努力理解什么&^和&^=运营商在围棋的意思.我无法在文档中找到答案(说明它有点清晰的操作符,但它对我没有多大帮助)或者通过试验.
特别是,我想知道Python中是否存在等价物.
为什么Python解释器返回<class 'type of the variable'>的type(_).
为什么解释器没有显示错误消息,_但是在它给出的其他特殊字符的情况下SyntaxError.
>>> type(_)
<class 'type'>
>>> type($)
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud) 我想打印数字和一些字符串,例如:
print("root: " + rootLeaf + " left:" + leftLeaf + "sum: " +(rootLeaf+leftLeaf) )
Run Code Online (Sandbox Code Playgroud)
这里“root”,“left”和“sum”是字符串,其中rootLeaf和leftleaf是整数,我想找到它们的总和。
我在这里检查了帖子,但无法实现整数之和(字符串连接中的数学运算)
我制作了这个计算器来尝试在 python 中计算三角函数。我刚刚开始学习如何使用这个程序,到目前为止我还没有找到任何对我有意义的东西。问题在于,对于我的科学计算器上出现的答案,我不断得到不同的答案。
while True:
print('type "sine1" to find the value of the Opposite')
print('type "sine2" to find the value of the Hypotenuse')
print('type "cosine1" to find the value of the Adjacent')
print('type "cosine2" to find the value of the Hypotenuse')
print('type "tangent1" to find the value of the Opposite')
print('type "tangent2" to find the value of the Adjacent')
user_input = input(": ")
from math import sin, cos, tan
if user_input == 'sine1':
degrees = float(input('Enter the degrees: '))
hypotenuse …Run Code Online (Sandbox Code Playgroud) 我想从同一个列表中选择两个单独的随机选项.有没有办法做到这一点,不包括制作单独的名单?
import random
cards = [2,3,4,5,6,7,8,9,10,'King','Queen','Jack']
cards = random.choice(cards)
suits = ['Clubs', 'Hearts', 'Spades', 'Diamonds']
suits = random.choice(suits)
first_card = ("your first card is the {} of {}") .format(cards,suits)
second_card = ("your second card is the {} of {}") .format(cards,suits)
print first_card
print second_card
Run Code Online (Sandbox Code Playgroud)
your first card is the 10 of Spades
your second card is the 10 of Spades
Run Code Online (Sandbox Code Playgroud)
我希望输出相同,但最后一张牌与第一张牌不同; 两张单独的卡片
我有这个代码:
reclass=""
for x in xrange(1,32):
if x <=30:
reclass+="remap"+str(x)+"+"
else:
reclass+="remap"+str(x)
print reclass
Run Code Online (Sandbox Code Playgroud)
哪个印刷品:
remap1+remap2+remap3+remap4+...
Run Code Online (Sandbox Code Playgroud)
但我想要打印字符串:
'remap1'+'remap2'+'remap3'+...
Run Code Online (Sandbox Code Playgroud)
我该如何做到这一点?
在__builtin__Python中的杂波模块开发,有很多的函数和类具有非常通用名称命名空间(例如max,sum,id,hash,往往得到变量命名的方式,当一个上下文感知IDE之外的人可以意外覆盖一个等)名字没有注意到.
有没有办法阻止从某个文件隐式访问此模块并需要显式导入?
有点像:
from __builtins__ import hash as builtin_hash
hash = builtin_hash(foo)
Run Code Online (Sandbox Code Playgroud)
我知道这是不好的做法.
我有这个方法从SQL查询中检索一个值:
def get_base_price(self, date):
sql = """
SELECT TimeSeriesValue
FROM dbo.TimeSeriesPosition
WHERE
TimeSeriesTypeID = {0} AND
FundID = {1} AND
SecurityMasterID = 45889 AND
EffectiveDate = '{2}'""".format(self.LAST_PRICE_ID, self.FUND_ID, date)
with self.job.rap.connect() as conn:
with conn.cursor() as cursor:
price = cursor.execute(sql).fetchone()[0]
if price is NoneType:
return 100
else:
return price # Returns base price value
Run Code Online (Sandbox Code Playgroud)
我有一个测试,但如果price是的话,我无法返回100 None.我的运行测试只返回NoneType对象不可订阅'
我也尝试过:100 if price is None else price那没用.我错过了什么?