小编daw*_*awg的帖子

Python相当于find2perl

Perl有一个名为find2perl的可爱小实用工具,它将(非常忠实地)将Unix find实用程序的命令行转换为Perl脚本来执行相同操作.

如果您有这样的find命令:

find /usr -xdev -type d -name '*share'

                         ^^^^^^^^^^^^  => name with shell expansion of '*share'
                 ^^^^ => Directory (not a file)
           ^^^ => Do not go to external file systems
     ^^^ => the /usr directory (could be multiple directories
Run Code Online (Sandbox Code Playgroud)

它找到share以下结尾的所有目录/usr

现在运行find2perl /usr -xdev -type d -name '*share'它会发出一个Perl脚本来做同样的事情.然后,您可以修改脚本以供您使用.

Python os.walk()当然具有所需的功能,递归目录列表,但存在很大差异.

以简单的find . -type f -print方式查找并打印当前目录下的所有文件.一个天真的实现使用os.walk()将是:

for path, dirs, files in os.walk(root):
    if files: …
Run Code Online (Sandbox Code Playgroud)

python find os.walk

14
推荐指数
1
解决办法
1306
查看次数

Python:有没有办法保持从int到long int的自动转换?

Python比其他脚本语言更强类型.例如,在Perl中:

perl -E '$c=5; $d="6"; say $c+$d'   #prints 11
Run Code Online (Sandbox Code Playgroud)

但是在Python中:

>>> c="6"
>>> d=5
>>> print c+d
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)

Perl将检查字符串并转换为数字,并且+ - / * **运算符按预期使用数字.PHP类似.

Python用于+连接字符串,因此尝试的操作c+d失败,因为c是一个字符串,d是一个int.Python 比Perl 具有更强的数字类型感.好的 - 我可以解决这个问题.

但考虑一下:

>>> from sys import maxint
>>> type(maxint)
<type 'int'>
>>> print maxint
9223372036854775807
>>> type(maxint+2)
<type 'long'>
>>> print maxint+2
9223372036854775809
>>> type((maxint+2)+maxint)
<type 'long'>
>>> …
Run Code Online (Sandbox Code Playgroud)

python performance integer long-integer

13
推荐指数
2
解决办法
3828
查看次数

Xcode 4中的Cocoa绑定

Objective-c是可学习的; 可可是可以学习的; 我发现Interface Builder及其后代Xcode 4是完全不可思议的!没有文本(作为C或Obj-c源代码的等价物)来引用.所有描述都是"拖到这里;连接那个; Ctl拖到那里",我仍然迷路....

我一直试图通过关键技术的各种Apple示例.我一直在Apple开发人员的示例NSTableViewBinding中查看Cocoa Bindings和示例文件.为了尝试理解它,我一直试图复制它.

现在参考awakeFromNib文件MyWindowController.m中方法顶部的注释我如何在Xcode 4中建立这些连接???

在此输入图像描述

  1. Object调用TableArray的对象库或源代码中都不存在; 它来自哪里?

  2. Referencing Bindings右边; 这些是如何创造的?

  3. awakeFromNib方法顶部提到的各种键/值对; 这些是如何创造的?

我知道Xcode 4应该是一个更直接的改进,但我对IB的替换感到非常困惑.网上的所有IB资料都指的是完全不同的早期版本,所以我找不到太多帮助.

跟进

我确实成功地在Xcode 4中找出了Cocoa Bindings.我能够在几行代码中复制示例程序的功能.

以下是我花了一段时间才弄明白的问题:

  1. TableArrayArray Controller Object从对象库拖到XIB中的对象列表,然后重命名.(Apple注意:在右键单击HUD视图中,基础对象类会很好)

  2. Stephen Poletto的答案是连接它的宝贵指南.

  3. 我没有立即明白连接界面如何在右键单击HUD中工作: 图2

  4. 存储阵列myContentArray隐含在实例化中Array Controller Object

  5. 单击并从TableArray的HUD拖动到.h文件中的AppDelegate定义,以在那里创建连接.

macos objective-c interface-builder xcode4

13
推荐指数
1
解决办法
8970
查看次数

使用python将csv文件转换为元组列表

我将采用4列的csv:品牌,价格,重量和类型.

类型有橙色,苹果,梨,李子.

参数:我需要选择最可能的重量,但选择1个橙子,2个梨子,3个苹果和1个李子,不超过20美元的预算.我不能重复相同水果的品牌(比如选择同一品牌的苹果3次等).

我可以通过Python打开并读取csv文件,但我不确定如何从csv文件创建字典或元组列表?

为了更清楚,这里是数据的概念.

Brand, Price, Weight, Type
brand1, 6.05, 3.2, orange
brand2, 8.05, 5.2, orange
brand3, 6.54, 4.2, orange
brand1, 6.05, 3.2, pear
brand2, 7.05, 3.6, pear
brand3, 7.45, 3.9, pear
brand1, 5.45, 2.7, apple
brand2, 6.05, 3.2, apple
brand3, 6.43, 3.5, apple
brand4, 7.05, 3.9, apple
brand1, 8.05, 4.2, plum
brand2, 3.05, 2.2, plum
Run Code Online (Sandbox Code Playgroud)

这就是我现在所拥有的一切:

import csv
test_file = 'testallpos.csv'
csv_file = csv.DictReader(open(test_file, 'rb'), ["brand"], ["price"], ["weight"], ["type"])
Run Code Online (Sandbox Code Playgroud)

python csv dictionary tuples

13
推荐指数
2
解决办法
3万
查看次数

你如何在条件中使用OR,AND?

我已经发现自己很多次,我需要全部或至少一个等于某事的东西,我会写出类似的东西:

if a==1 and b==1:
   do something
Run Code Online (Sandbox Code Playgroud)

要么

if a==1 or b==1:
   do something
Run Code Online (Sandbox Code Playgroud)

如果事情的数量很小就可以了,但它仍然不是很优雅.所以,对于大量的事情,是否有更好的方法,做上述事情?谢谢.

python conditional

12
推荐指数
1
解决办法
322
查看次数

Numpy longdouble算术似乎没有转换的长双倍

我一直在玩C99的四倍精度长双.据我所知,(平台特定的)numpy支持long double和128bit float.

我遇到了一些我无法解释的事情.

鉴于:

>>> import numpy as np
Run Code Online (Sandbox Code Playgroud)

计算需要超过64位但小于128位的数字表示为整数:

>>> 2**64+2
18446744073709551618          # note the '8' at the end
>>> int(2**64+2)
18446744073709551618          # same obviously
Run Code Online (Sandbox Code Playgroud)

如果我在C99 128位长双倍中计算相同的数字,我得到18446744073709551618.000000

现在,如果我使用numpy long double:

>>> a=np.longdouble(2)
>>> b=np.longdouble(64)
>>> a**b+a
18446744073709551618.0              # all good...
Run Code Online (Sandbox Code Playgroud)

这些不正确的结果怎么样:

>>> np.longdouble(2**64+2)
18446744073709551616.0             # Note '6'; appears 2**64 not done in long double
>>> np.longdouble(int(2**64+2))
18446744073709551616.0             # can't force the use of a Python long
>>> n=int(2**64+2)
>>> np.longdouble(n)
18446744073709551616.0 …
Run Code Online (Sandbox Code Playgroud)

python floating-point numpy

12
推荐指数
1
解决办法
4056
查看次数

递归 glob。** 或 */** globstar 在 zsh、Bash、Python 和 Ruby 上不同

假设您有这个目录树:

\n
$ tree /tmp/test\n/tmp/test\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir_a\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir a\\012file with CR\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir a file with spaces\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 sub a directory\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 with a file in it\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir_b\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir b\\012file with CR\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 dir b file with spaces\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir_c\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 \\012\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir c\\012file with CR and *\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 dir c file with space and *\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file_1\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file_2\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 file_3\n\n4 directories, 11 files\n
Run Code Online (Sandbox Code Playgroud)\n

是一个生成该脚本的脚本。它\\012使\\n脚本编写更具挑战性。.hidden那里也有一个文件。)

\n

Bash 5.1、zsh 5.8、Python pathlib 5.10、启用递归的 Python …

ruby python bash zsh glob

12
推荐指数
1
解决办法
1676
查看次数

Python相当于Perl文件测试可读(-r),可写(-w)和可执行(-x)运算符

我一直在谷歌搜索尝试在Python中找到一些Perl的文件测试操作符.

大多数文件测试操作符只是底层操作系统stat调用的直接Python化.例如,os.stat('file').st_ctime只需将inode更改时间读取为*nix stat实用程序或ls -l将执行.

一些Perl文件测试操作符我在Python中找不到等价物.例如,我有一个由各种应用程序创建的85,000个图像文件的数据树.某些文件具有有效的UID设置,其方式很麻烦,并且修改因权限问题而失败.所以对于那些我需要运行的文件:

$ find . -type f -print0 | perl -0 -lnE 'say unless -w' | change euid...
Run Code Online (Sandbox Code Playgroud)

由于我没有在Python中找到等价物,因此我必须向Perl发现这些文件.我发现这张表显示没有直接的等价物.真正?

python perl operators

11
推荐指数
2
解决办法
1万
查看次数

Python 3生成器理解生成包括last的块

如果您在Python 3.7中有一个列表:

>>> li
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Run Code Online (Sandbox Code Playgroud)

您可以n使用两种常见的Python习语之一将其转换为每个长度的块列表:

>>> n=3
>>> list(zip(*[iter(li)]*n))
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
Run Code Online (Sandbox Code Playgroud)

因为(9,10)不是长度,所以丢弃最后一个不完整的元组n

你也可以这样做:

>>> [li[i:i+n] for i in range(0,len(li),n)]
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10]]
Run Code Online (Sandbox Code Playgroud)

如果你想要最后一个子列表,即使它有少于n元素.

假设我现在有一个发电机,gen未知长度或终端(所以呼叫list(gen))sum(1 for _ in gen)不明智)我想要每个块.

我能够想出的最好的生成器表达式是这样的:

from itertools import zip_longest
sentinel=object()             # for use in filtering out ending …
Run Code Online (Sandbox Code Playgroud)

python generator python-3.x

11
推荐指数
1
解决办法
359
查看次数

用于file.read()的多字节请求的Python EOF

file.read()上的Python文档说明An empty string is returned when EOF is encountered immediately.文档进一步指出:

请注意,此方法可能会多次调用基础C函数fread(),以尽可能接近大小字节.另请注意,在非阻塞模式下,即使未给出大小参数,也可能返回的数据少于请求的数据.

我相信Guido已经提出了不添加f.eof()PERFECTLY CLEAR的观点,所以需要使用Python方式!

然而,我不清楚的是,如果你是一个确定的测试,如果你从读取中得到的字节数少于所要求的字节,那么你已经达到了EOF,但你确实得到了一些.

即:

with open(filename,'rb') as f:
    while True:
        s=f.read(size)
        l=len(s) 
        if l==0: 
            break     # it is clear that this is EOF...
        if l<size:
            break      # ? Is receiving less than the request EOF???
Run Code Online (Sandbox Code Playgroud)

break如果您收到的呼叫数少于呼叫中请求的字节数,那么这是一个潜在的错误file.read(size)吗?

python eof

10
推荐指数
1
解决办法
1万
查看次数