我正在通过继承来制作数据集类torch.utils.data.Dataset,并遇到了以下问题。
与以前的函数不同,它返回固定类型的值,__getitem__但不会。例如,
class MyExample:
def __init__(self, some_list: list[int]):
self.some_list = some_list
def __getitem__(self, index):
return self.some_list[index]
Run Code Online (Sandbox Code Playgroud)
MyExample[<index>]将返回int,同时MyExample[<slice>]将返回slice of int。VScode智能感知自动T_co为其类型注释编写,但我不明白这意味着什么。
假设我有一个作为字符串的函数调用,例如"log(2, floor(9.4))". 我想以一种允许我访问第一个调用的函数名称和参数的方式解构调用,并准确地推断出作为参数的函数调用是否是参数。
例如,解构上面的字符串时的参数将变为[2, floor(9.4)]
我已经尝试使用一些字符串解析技术(例如逗号分割),但它似乎不起作用。
我想应该已经有这样的问题了,但是我还没有找到。这可能是因为我不知道我正在寻找的确切概念/单词,但这里是一个例子:
我有这个代码:
group_1 = ['Hello', 'world', '!']
group_2 = [1,23,4,2,5,2]
group_3 = ['A', 'K', 'L']
all_groups = [group_1, group_2, group_3]
for i in all_groups:
print(i, ':', len(i))
Run Code Online (Sandbox Code Playgroud)
它给出了这个输出:
['Hello', 'world', '!'] : 3
[1, 23, 4, 2, 5, 2] : 6
['A', 'K', 'L'] : 3
Run Code Online (Sandbox Code Playgroud)
这是预期的输出:
'group_1' : 3
'group_2' : 6
'group_3' : 3
Run Code Online (Sandbox Code Playgroud)
group_1正如您所看到的,我正在尝试打印可调用对象、group_2和 的名称group_3。有什么建议么?
我正在编写一个返回 0 到 0.25 之间的值的程序。我想将此值舍入到最接近的预定义值列表。
例如给定一个列表
round_to = [0,0.1,0.15,0.2,0.25]
Run Code Online (Sandbox Code Playgroud)
值 0.004 应四舍五入为 0,值 0.09 -> 0.1,值 0.126 -> 0.15,值 0.22 -> 0.2,依此类推。
有谁知道一种优雅的方法来实现这一点?
非常感谢任何见解!
我有一个 Python 字典,如下所示:
data = {
"shape" : "rectangle",
"dimensions" : [
{
"length" : 15,
"breadth": 20,
},
{
"length" : 20,
"breadth": 10,
}
]
}
Run Code Online (Sandbox Code Playgroud)
在我的用例中,将有一百多个这样的矩形及其尺寸。
我写这个是为了找到该区域:
length = data['dimensions'][0]['length']
breadth = data['dimensions'][0]['breadth']
area = length * breadth
print(area)
Run Code Online (Sandbox Code Playgroud)
运行上面的代码将得到 300 的结果,因为它乘以字典中的前两个长度和宽度数据 (15 * 20)。
我还如何获得其他“长度”和“宽度”数据的乘积并将它们与某种循环相加?同样,在我的用例中,将有数百个这样的“长度”和“宽度”数据条目。
比较以下内容。
示例 1:装饰器之前的文档字符串。
@app.route("/")
"""
summary
"""
def hello() -> str:
return "Hello World"
Run Code Online (Sandbox Code Playgroud)
对比示例 2:装饰器后的文档字符串:
"""
summary
"""
@app.route("/")
def hello() -> str:
return "Hello World"
Run Code Online (Sandbox Code Playgroud) 我正在尝试为 Yahtzee(一种骰子游戏)建模。
第一步,我尝试枚举同时掷出 5 个骰子的所有可能组合。我只想要独特的组合(例如5,5,5,4,4相同等5,5,4,5,4)。在 Python、C++ 或 Mathematica 中是否有一种简单的方法可以做到这一点?
我有一本以下格式的字典:
data = {
'Bob': {
'age': 12,
'weight': 150,
'eye_color': 'blue'
},
'Jim': {
'favorite_food': 'cherries',
'sport': 'baseball',
'hobby': 'running'
},
'Tom': {
'strength': 'average',
'endurance': 'high',
'heart_rate': 'low'
}
}
Run Code Online (Sandbox Code Playgroud)
将 dict 中的所有字典连接成一个新字典的最 Pythonic 方法是什么,这样我最终会得到如下所示的结果:
new_dict = {
'age': 12,
'weight': 150,
'eye_color': 'blue',
'favorite_food': 'cherries',
'sport': 'baseball',
'hobby': 'running',
'strength': 'average',
'endurance': 'high',
'heart_rate': 'low'
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试打印以下两个列表中的每个项目:
lNames = ['John','David','Michael']
lAges = [45,14,32]
with the format: "Person 0, Name: John, Age 34".
I tried adding another list:
```py
lPersons = [0, 1, 2]
Run Code Online (Sandbox Code Playgroud)
尝试了这个代码:
lNames = ['John','David','Michael']
lAges = [45,14,32]
lPersons = [0, 1, 2]
for a in lNames:
for b in lAges:
for c in lPersons:
print("Person: " + c + ", Name: " + a + ", Age: " + b)
Run Code Online (Sandbox Code Playgroud)
这给出了TypeError,因为我在打印时错误地将整数与字符串组合在一起。为了达到预期的结果,我缺少什么?
我有一个元组列表:
res=[(0, 0, 255, 0, 0),(1, 0, 255, 0, 0),(0, 1, 255, 0, 0),(1, 1, 255, 0, 0),
(4, 4, 0, 255, 0),(5, 4, 0, 255, 0),(4, 5, 0, 255, 0),(5, 5, 0, 255, 0)]
Run Code Online (Sandbox Code Playgroud)
这是我的想法:
keys = [l[2:] for l in res]
values = [l[:2] for l in res]
d=dict(zip(keys, values))
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
{(255, 0, 0): (1, 1), (0, 255, 0): (5, 5)}
Run Code Online (Sandbox Code Playgroud)
我的输出是错误的,我需要这个:
{(255, 0, 0): [(0, 0),(1,0),(0,1),(1,1)],
(0, 255, 0): [(4,4),(5,4),(4,5),(5,5)]}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
python ×10
dictionary ×3
list ×2
string ×2
algorithm ×1
c++ ×1
combinations ×1
decorator ×1
docstring ×1
math ×1
pep8 ×1
pytorch ×1
rounding ×1
tuples ×1
type-hinting ×1