小编kla*_*aus的帖子

使用matplotlib创建随机形状/轮廓

我试图使用python生成随机轮廓的图像,但我找不到一个简单的方法来做到这一点.

这是我想要的一个例子:

在此输入图像描述

最初我尝试使用matplotlib和高斯函数来做它,但我甚至无法接近我展示的图像.

有一个简单的方法吗?

我感谢任何帮助

python bezier matplotlib shapes contour

7
推荐指数
3
解决办法
3733
查看次数

使用数学运算重命名文件

我试图使用bash命令重命名一些文件,但我不知道如何在regex表达式中添加算术表达式/数学运算.

输入:

a000.png
a001.png
...
Run Code Online (Sandbox Code Playgroud)

输出继电器:

a010.png
a011.png
...
Run Code Online (Sandbox Code Playgroud)

我想在名字中添加10.

我试过的一些事情:

rename -n -e 's/a(\d+).png/a$1 + 10.png/' *
rename -n -e 's/a(\d+).png/a{$1 + 10}.png/' *
rename -n -e 's/a(\d+).png/a$($1 + 10).png/' *
Run Code Online (Sandbox Code Playgroud)

有一个简单的方法吗?

bash perl batch-rename

6
推荐指数
1
解决办法
451
查看次数

Python函数修改字符串

有一次我被要求创建一个给定字符串的函数,从字符串中删除几个字符。

可以在 Python 中做到这一点吗?

这可以为列表完成,例如:

def poplist(l):
    l.pop()

l1 = ['a', 'b', 'c', 'd']

poplist(l1)
print l1
>>> ['a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)

我想要的是为字符串做这个函数。我能想到的唯一方法是将字符串转换为列表,删除字符,然后将其连接回字符串。但那时我将不得不返回结果。例如:

def popstring(s):
    copys = list(s)
    copys.pop()
    s = ''.join(copys)

s1 = 'abcd'

popstring(s1)

print s1
>>> 'abcd'
Run Code Online (Sandbox Code Playgroud)

我明白为什么这个功能不起作用。问题更多的是是否可以在 Python 中执行此操作?如果是,我可以在不复制字符串的情况下进行吗?

python string python-2.7

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

在文件开头使用 os.chdir(os.path.dirname(__file__)) 是一个好习惯吗?

我有一个在其目录中加载大量文件的脚本,并且可以从任何位置调用该脚本。我想知道os.chdir(os.path.dirname(__file__))在文件的开头使用是否可以使事情不那么冗长。

脚本目录:

script/path/
  do_stuff.py
  src/
    ..some files
Run Code Online (Sandbox Code Playgroud)

该脚本是这样调用的:

$ python3 script/path/do_stuff.py
Run Code Online (Sandbox Code Playgroud)

所以,而不是做

with open(os.path.join(os.path.dirname(__file__), 'src/..')) as f:
    ...
Run Code Online (Sandbox Code Playgroud)

os.chdir(os.path.dirname(__file__))在导入后添加并执行:

with open('src/..') as f:
    ...
Run Code Online (Sandbox Code Playgroud)

这被认为是一个好习惯吗?或者我应该避免它以避免在更复杂的项目中出现并发症?

python

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

标签 统计

python ×3

bash ×1

batch-rename ×1

bezier ×1

contour ×1

matplotlib ×1

perl ×1

python-2.7 ×1

shapes ×1

string ×1