我想在字符串数组上使用numpy.where函数.但是,我没有成功.有人可以帮我解决这个问题吗?
例如,当我numpy.where在以下示例中使用时,我收到一个错误:
import numpy as np
A = ['apple', 'orange', 'apple', 'banana']
arr_index = np.where(A == 'apple',1,0)
Run Code Online (Sandbox Code Playgroud)
我得到以下内容:
>>> arr_index
array(0)
>>> print A[arr_index]
>>> apple
Run Code Online (Sandbox Code Playgroud)
但是,我想知道字符串数组中的索引,A其中字符串'apple'匹配.在上面的字符串中,这发生在0和2.但是,np.where只返回0而不是2.
那么,我该如何numpy.where处理字符串呢?提前致谢.
我正在使用带有boto3的版本化S3存储桶.如何检索给定密钥的所有版本(甚至所有密钥的所有版本)?我可以做这个:
for os in b.objects.filter(Prefix=pref):
print("os.key")
Run Code Online (Sandbox Code Playgroud)
但这只给我每个键的最新版本.
非常感谢,
我有以下字典:
fruits = {
"apple": 5,
"Apple": 5,
"orange": 5,
"strawberry": 3,
"blueberry": 1
}
Run Code Online (Sandbox Code Playgroud)
我需要打印出具有最高值的两个键的列表.关系需要按字母顺序打开AZ,大写字母优先于小写字母.通过计数运行以下排序,但不会破坏关系:
popular_fruits = sorted(fruits, key=fruits.get, reverse=True)
print(popular_fruits[0:2])
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?
我正在编写一些代码,以便在for循环的每次迭代中运行一个将数据写入文件的函数,如下所示:
int main()
{
int i;
/* Write data to file 100 times */
for(i = 0; i < 100; i++) writedata();
return 0;
}
void writedata()
{
/* Create file for displaying output */
FILE *data;
data = fopen("output.dat", "a");
/* do other stuff */
...
}
Run Code Online (Sandbox Code Playgroud)
我如何得到它,这样,当我运行它,它会在程序开始删除文件的内容,但在那之后,将数据添加到该文件?我知道使用"w"标识符fopen()将打开一个空的新文件,但我希望能够在每次通过writedata()函数时将数据'追加'到文件中,因此使用"a"标识符.
你们知道一种更聪明的方法吗?
os.path.split(os.path.split(os.path.split(os.getcwd())[0])[0])[0]
Run Code Online (Sandbox Code Playgroud)
os.getcwd()例如:
/my/path/to/my/directory
Run Code Online (Sandbox Code Playgroud)
我应该得到
/my/path/
Run Code Online (Sandbox Code Playgroud)
这确实是一种夸张和重复os.path.split()
谢谢
我想使用循环查看绘图如何随不同的值而变化。我想在同一个情节上看到它。但我不想保留图中先前的情节。在 MATLAB 中,这可以通过创建一个图形并在同一图形上绘制来实现。当循环结束时关闭它。
喜欢,
fh = figure();
%for loop here
%do something with x and y
subplot(211), plot(x);
subplot(212), plot(y);
pause(1)
%loop done
close(fh);
Run Code Online (Sandbox Code Playgroud)
我无法在 matplotlib 中找到等价物。通常所有的问题都与在同一个图上绘制不同的系列有关,这在 matplotlib 上似乎很自然,通过使用绘制多个系列plt.plot()然后最终使用plt.show(). 但我想刷新剧情。
我用 python 写了一个这样的类
class Vector(object):
def __init__(self, coordinates):
try:
if not coordinates:
raise ValueError
self.coordinates = tuple(coordinates)
self.dimension = len(coordinates)
except ValueError:
raise ValueError('The coordinates must be nonempty')
except TypeError:
raise TypeError('The coordinates must be an iterable')
def __add__(self,v):
v1 = np.array(self.coordinates)
v2 = np.array(v.coordinates)
result = v1 + v2
return result.tolist()
def __sub__(self, other):
v1 = np.array(self.coordinates)
v2 = np.array(other.coordinates)
result = v1 - v2
return result.tolist()
def __mul__(self, other):
return other * np.array(self.coordinates)
def multiply(self,other):
v = Decimal(str(other)) …Run Code Online (Sandbox Code Playgroud) 对Python来说很新,试图创建一个可以创建任意数量的军队的游戏,但是每个军队都会预先渲染士兵的名字.
我想我需要使用超级init来真正减少重复的代码,但我不能为我的生活弄清楚如何让它工作.根据我的理解,我的类Army应该是Parent类,with RedArmy和Scoutas子类.我只是在努力去理解它super().__init__()应该进入哪里?
class Army:
def __init__(self):
self.color = None
self.scoutname = None
self.demomanname = None
self.medicname = None
def train_scout(self, weapon):
return Scout(self.color, self.scoutname, weapon)
class RedArmy(Army):
def __init__(self):
self.color = "Red"
self.scoutname = "Yankee"
self.demomanname = "Irish"
self.medicname = "Dutch"
class BlueArmy(Army):
pass
class Scout:
specialization = "fast captures"
def __init__(self, color, scoutname, weapon):
self.color = color
self.scoutname = scoutname
self.weapon = weapon
def introduce(self):
return (f'Hi I\'m {self.scoutname}, I do …Run Code Online (Sandbox Code Playgroud) 是否有内置的python等效std::find_if于查找给定条件的列表的第一个元素true?换句话说,类似于index()列表的功能,但具有任意的一元谓词,而不仅仅是对相等性的测试.
我不想使用列表理解,因为我想到的特定谓词计算起来有些昂贵.
我有一堆结构如下的数据框
df = pd.DataFrame(
[[1, 'A', 10], [2, 'A', 20], [3, 'A', 30],
[1, 'B', 20], [2, 'B', 20], [3, 'B', 10],
[1, 'M', 20], [2, 'M', 30], [3, 'M', 30]],
columns=['foo', 'bar', 'buzz']
)
Run Code Online (Sandbox Code Playgroud)
数据框最初按列排序,bar并且foo可以从中获得
df.sort_values(['bar', 'foo'])
Run Code Online (Sandbox Code Playgroud)
我需要df按foo一个bar来排序。显而易见的解决方案是
df.sort_values(['foo', 'bar'])
Run Code Online (Sandbox Code Playgroud)
这给了我
foo bar buzz
0 1 A 10
3 1 B 20
6 1 M 20
1 2 A 20
4 2 B 20
7 2 M 30
2 …Run Code Online (Sandbox Code Playgroud)