我想根据我拥有的三个列表创建一个字典:
Hier = ['A', 'C']
values = [10, 20]
All = ['A', 'B', 'C', 'D']
Run Code Online (Sandbox Code Playgroud)
结果应如下所示:
{"A", 10, "B": "", "C": 20, "D":""}
Run Code Online (Sandbox Code Playgroud) 多年来,我每天都使用 Pandas,并且经常(但不是那么频繁)使用 Numpy。大多数时候我会做类似的事情:
import pandas as pd
import numpy as np
Run Code Online (Sandbox Code Playgroud)
但是[编辑:在 pandas 2.0 之前] 还可以选择直接从 Pandas 使用 Numpy:
df['value'] = pd.np.where(df['date'] > '2020-01-01', 1, 0)
Run Code Online (Sandbox Code Playgroud)
有谁知道这些选项中的任何一个是否比另一个选项的性能明显更高?
在学习 udemy 课程后,我陷入了“'str object is not callable”错误。我基本上已经复制并粘贴了给我带来问题的代码片段,但它仍然产生相同的错误。还没有发现任何与我遇到的确切问题相关的内容,但如果这是一个简单得可笑的修复,我不会感到惊讶!
问题在于“ops_function”位抛出“str”对象不可调用错误。
如果您需要更多信息,请告诉我,感谢您的见解!
from art import logo
print(logo)
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
return a / b
operations = {
"+": "add",
"-": "subtract",
"*": "multiply",
"/": "divide"
}
first = int(input("Enter a number: "))
second = int(input("Enter another number: "))
for op in operations:
print(op)
op_choice = input("Select an operator from the list above: …Run Code Online (Sandbox Code Playgroud) 我有一份物品清单。在 for 循环中,我检查该项目是否为 3。如果是 3,则应将 3 更改为 2。这是我想到的使用 for 循环的两种方法。但只有最后一个有效。
有没有办法让第一个 for 循环工作而不失去其“pythonic”风格?
a = [1, 2, 3]
for num in a:
if num == 3:
num = 2
# -> [1, 2, 3]
for i in range(len(a)):
if a[i] == 3:
a[i] = 2
# -> [1, 2, 2]
Run Code Online (Sandbox Code Playgroud) 我有一个不同大小的列表列表,但我想让它们都具有相同的长度。例如,如果长度小于 5,则通过用零填充来使它们的长度为 5;如果长度大于 5,则剪切列表。例如,我有一个列表:
foo = [
[1, 2, 3],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5, 6, 7]]
Run Code Online (Sandbox Code Playgroud)
result = [
[1, 2, 3, 0, 0],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]]
Run Code Online (Sandbox Code Playgroud)
如果列表列表很大,您是否有最佳且快速的解决方案的想法?
两个都
x = 1
f"{x}"
Run Code Online (Sandbox Code Playgroud)
和
x = '1'
f"{x}"
Run Code Online (Sandbox Code Playgroud)
给
'1'
Run Code Online (Sandbox Code Playgroud)
作为输出。我如何获得"1"and"'1'"作为输出?
a = {
1: {'abc': 50, 'def': 33, 'xyz': 40},
2: {'abc': 30, 'def': 22, 'xyz': 45},
3: {'abc': 15, 'def': 11, 'xyz': 50}
}
Run Code Online (Sandbox Code Playgroud)
我想迭代这个嵌套字典,删除子键(或提取子键值),但保留主键。第二步是将字典变成列表的列表:
b = [
[1, 50, 33, 40],
[2, 30, 22, 45],
[3, 15, 11, 50]
]
Run Code Online (Sandbox Code Playgroud)
我浏览了这里无数关于提取键和值的帖子,但找不到足够接近的示例来满足我的需要(这仍然是新的):到目前为止,我有这个:
for key in a.keys():
if type(a[key]) == dict:
a[key] = a[key].popitem()[1]
Run Code Online (Sandbox Code Playgroud)
这给出了这个 - 每个键中第三个子键的值:这是一个开始,但不完整或我想要的
{1: 40, 2: 45, 3: 50}
Run Code Online (Sandbox Code Playgroud) 我想反转二维数组,但在使用[::-1][::-1]和[::-1, ::-1]索引时得到两个不同的结果。下面是一个示例。我不太明白它是如何不同地解释的。
values = [
[5, 6, 5, 5, 8, 9, 9],
[9, 5, 1, 4, 5, 9, 7],
[3, 9, 6, 2, 1, 2, 3],
[1, 7, 6, 7, 1, 7, 5],
[2, 1, 3, 8, 7, 8, 8],
[2, 9, 3, 6, 4, 6, 4]
]
x = np.array(values)
reverse_2d_1 = x[::-1][::-1]
reverse_2d_2 = x[::-1, ::-1]
Run Code Online (Sandbox Code Playgroud)
[[5 6 5 5 8 9 9]
[9 5 1 4 5 9 7]
[3 9 6 2 …Run Code Online (Sandbox Code Playgroud) 我想根据从 API 获取的 .csv 文件创建一个简单的绘图图表。
我导入库,传递数据帧,然后收到错误:
TypeError: <class 'numpy.typing._dtype_like._SupportsDType'> is not a generic class
Run Code Online (Sandbox Code Playgroud)
代码:
import plotly.express as px
df=pd.read_csv('file.csv')
Run Code Online (Sandbox Code Playgroud)
可能是什么问题,这个错误意味着什么?
完整的错误回溯:
import plotly.express as px
df=pd.read_csv('file.csv')
Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的 python 字符串。该字符串来自一家美国上市公司向 SEC 提交的文件。我试图使用unicodedata.normalise函数从字符串中删除一些烦人的字符,但这并没有删除所有字符。这种行为背后的原因可能是什么?
from unicodedata import normalize
s = 'GTS.Client.Services@JPMChase.com\nFacsimile\nNo.:\xa0 312-233-2266\n\xa0\nJPMorgan Chase Bank,\nN.A., as Administrative Agent\n10 South Dearborn, Floor 7th\nIL1-0010\nChicago, IL 60603-2003\nAttention:\xa0 Hiral Patel\nFacsimile No.:\xa0 312-385-7096\n\xa0\nLadies and Gentlemen:\n\xa0\nReference is made to the\nCredit Agreement, dated as of May\xa07, 2010 (as the same may be amended,\nrestated, supplemented or otherwise modified from time to time, the \x93Credit Agreement\x94), by and among\nHawaiian Electric Industries,\xa0Inc., a Hawaii corporation (the \x93Borrower\x94), the Lenders from time to\ntime party thereto and JPMorgan Chase Bank, …Run Code Online (Sandbox Code Playgroud) python unicode unicode-normalization python-3.x python-unicode