我想将当前项目(字符串表示形式)添加到 tqdm 进度栏。
\n这是没有任何附加文本的代码(非常简短):
\nfrom tqdm import tqdm\nimport time\n\nanimals = ["cat", "dog", "bird", "fish", "insect"]\n\nfor animal in tqdm(animals):\n time.sleep(1)\nRun Code Online (Sandbox Code Playgroud)\n80%|\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88 | 4/5 [00:04<00:01, 1.14s/it]\nRun Code Online (Sandbox Code Playgroud)\n以下是显示正在处理的项目(此处为动物名称)的两种方法:
\n80%|\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88 | 4/5 [00:04<00:01, 1.14s/it]\nRun Code Online (Sandbox Code Playgroud)\n40%|\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88 | 2/5 [00:02<00:04, 1.46s/it, bird]\nRun Code Online (Sandbox Code Playgroud)\n和
\nprogress = tqdm(animals)\nfor animal in progress:\n progress.set_postfix_str(animal)\n time.sleep(1)\nRun Code Online (Sandbox Code Playgroud)\n60%|\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88 | 3/5 [00:03<00:02, 1.04s/it, fish]\nRun Code Online (Sandbox Code Playgroud)\n这两种方法都添加了相当多的额外代码,并且需要指定进度条对象。我还没有找到任何更简单的方法来实现这一目标。我是否错过了 tqdm 提供的一些东西?
\n特别是,我一直在寻找一种方法来告诉 tqdm 输出__str__()其迭代器返回的项目的字符串(使用 say )。例如,
40%|\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88 | 2/5 [00:02<00:04, 1.46s/it, bird]\n …Run Code Online (Sandbox Code Playgroud) 习惯于老式的程序化C编程,我现在正在学习Java,并且可能已经明显地认识到硬件是设计而不是语法.
在想到如何填充我的动物园后,我花了几个小时的时间去了解想法:
我有一个class Zoo和一个抽象的class Animal.有几个非抽象的子类Animal叫Lion,Giraffe,Zebra,Penguin,等.甲Zoo对象将包含的每个子类中的恰好一个实例Animal,并且每个这样的实例包含于唯一的参考Zoo实例它属于.
我想以特定的顺序在动物园迭代动物(当你沿着一条小径行走时),以及在动物园里用字典查找动物.更确切地说,在某些时候,我将解析包含动物名称的文本文件(例如,对于字符串"LION",我想获得唯一的Lion实例).字符串与动物的一对一映射.我已经决定使用了LinkedHashMap<String, Animal>.
我想编写可管理的代码,以便将来轻松添加更多动物.到目前为止,我最好的方法如下.
在Zoo类中,我定义了一个enum反映我想要的动物的顺序,它的元素完全对应于我将在文本文件中解析的字符串.
private enum Species { LION, GIRAFFE, ZEBRA, PENGUIN };
Run Code Online (Sandbox Code Playgroud)
在Zoo我也有创建的动物对象的方法:
private Animal makeAnimal(Species species)
{
switch (species)
{
case LION:
// create a Lion object;
break;
case GIRAFFE:
// ...
}
// return the Animal object created above;
} …Run Code Online (Sandbox Code Playgroud) 在Java中定义常量的位置的问题在论坛中出现了很多次,但我很难找到一个我觉得舒服的解决方案.
为简单起见,假设我有两个类:WriteMyData和ReadMyData.None是另一个的子类.两个类共享两个对其操作至关重要的常量:String DELIMITER和int LENGTH.将来我可能想要更改这些常量的值,以便在适当的地方定义它们.
这种共识似乎常常有利于这种enum类型.但是,在我的情况下没有什么可以枚举,所以我最终只有一个项目enum,我打电话DEFAULT:
public enum DataSettings {
DEFAULT(",", 32);
private final String delimiter;
private final int length;
DataSettings(String delmiter, int length) {
this.delimiter = delimiter;
this.length = length;
}
public String getDelimiter() { return delimiter; }
public int getLength() { return length; }
}
Run Code Online (Sandbox Code Playgroud)
因此,在我的两个类中,我通过DataSettings.DEFAULT.getDelimiter()和访问常量DataSettings.DEFAULT.getLength().
这是非常好的OO风格吗?是否使用enum过度杀伤?如果我不使用enum,我该怎么做?为常量创建一个接口似乎不受欢迎,而且我的类似乎没有自然的超类来源.初学者错误是只有一个默认项目enum吗?
我有一个pandas.DataFrame df并且想添加一个col具有一个值的新列"hello"。我希望此列的数据类型为category单一类别"hello"。我可以做以下事情。
df["col"] = "hello"
df["col"] = df["col"].astype("category")
Run Code Online (Sandbox Code Playgroud)
df["col"] 三遍才能达到这个目的吗?df在新列转换为分类之前中间数据框可能会占用大量空间。(数据帧相当大,有数百万行,并且该值"hello"实际上是一个更长的字符串。)是否有任何其他直接的、“简短而敏捷”的方法来实现这一目标,同时避免上述问题?
另一种解决方案是
df["col"] = pd.Categorical(itertools.repeat("hello", len(df)))
Run Code Online (Sandbox Code Playgroud)
但它需要itertools使用len(df),而且我不确定内存的使用情况。
据我所知,每个枚举类型的预定义常量只有一个实例.那是对的吗?现在,假设有一个名为valueenum 的实例变量,假设ONE是一个预定义的常量.如果value为(的)实例修改了,ONE则会影响所有引用的变量ONE.但是什么value时候垃圾收集器摆脱枚举的所有实例的价值呢?他们甚至被扔掉了吗?
例
下面是一个简单的例子,其中有两个变量A和B,既指ONE.在第一次使用value之前,该值被否定B.如果垃圾收集器已经摆脱了该ONE行之前的实例Number B = Number.ONE;,那么value我认为该值将被"重置"回到1.它是否正确?
枚举:
public enum Number
{
ONE(1), TWO(2), THREE(3);
int value;
Number(int value) { this.value = value; }
void negate() { value = -value; }
@Override
public String toString() { return "value: " + value; }
}
Run Code Online (Sandbox Code Playgroud)
主要方法:
public static void main(String[] args)
{
Number A …Run Code Online (Sandbox Code Playgroud) 在 Python(3.7 及更高版本)中,我想获得对 dict key的引用。更准确地说,让我们d成为一个字典,其中键是字符串。在以下代码中, of 的值k可能存储在内存中的两个不同位置(一个由 dict 指向,另一个由 指向k),而 of 的值v仅存储在一个位置(由 dict 指向的位置) .
# d is a dict
# k is a string dynamically constructed, in particular not from iterating over d's keys
if k in d:
v = d[k]
# Now store k and v in other data structures
Run Code Online (Sandbox Code Playgroud)
就我而言,字典非常大,字符串键非常长。为了减少内存使用量,我想k用一个指向相应字符串的指针替换,d然后再存储k到其他数据结构中。有没有一种直接的方法可以做到这一点,即使用 dict 的键作为字符串池?
(脚注:这似乎是过早的优化,也许确实如此,但作为一个老派的 C 程序员,我在晚上做“记忆技巧”时睡得更好。抛开玩笑,出于好奇,我真的很想知道答案,我确实要在 Raspberry …
java ×3
python ×3
enums ×2
categories ×1
constants ×1
dataframe ×1
dictionary ×1
pandas ×1
pool ×1
progress-bar ×1
python-3.x ×1
tqdm ×1