是否可以从同一个字典中的字典条目中获取值?我想建立一个目录列表,同时引用以前添加的目录..
common_dirs = {
'root': '/var/tmp',
'java_path': os.path.join(dict.get('root'), 'java'),
'application_path': os.path.join(dict.get('java_path'), 'my_app')
}
Run Code Online (Sandbox Code Playgroud) 我试图想出一个函数,它接受一个输入x并将一个大的列表与元素x*x的数量分成x个较小的列表,每个列表中包含x个元素,例如:
big_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
def split_list (x):
big_list = pairs (x)
small_list = [big_list[0:x] for x in range (x)]
Run Code Online (Sandbox Code Playgroud)
我的输出必须是:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
Run Code Online (Sandbox Code Playgroud)
但是我没有得到它,你推荐什么?
我正在尝试在python中创建一个方法,它接受1-n个参数,在每个参数上调用相同的方法并返回结果.例如;
(注意这是伪代码,我只是在运行时输入这些方法/语法 - 对python来说是新的)
def get_pid(*params):
pidlist = []
for param in params:
pidlist.add(os.getpid(param))
return pidlist
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想做点什么
x, y = get_pid("process1", "process2")
Run Code Online (Sandbox Code Playgroud)
我可以在哪里添加尽可能多的参数 - 并且方法可以像'pythonic'一样紧凑.我认为可能有一种比循环参数和附加到列表更好的方法吗?
有什么建议/提示吗?
目前我正在为我的班级功能编写单元测试.
def test_getitem(self):
test1 = List(3)
for i in range(3):
test1.append(i)
self.assertEqual(test1[2], 2)
test1 = List(3)
for i in range(3):
test1.append(i)
self.assertRaises(IndexError, test1[4])
Run Code Online (Sandbox Code Playgroud)
我现在遇到的问题是self.assertRaises我的代码部分.我不确定这是不是这样做但是当我运行时unittest,它会产生错误Index out of range.顺便说一下,它应该是"OK".
List是我的类,并List(3)创建一个基于数组的列表.所以,当我test1.append(i),它现在[0,1,2].
test1[2]是一种getitem在我的类中调用函数的方法self[index].
我想知道我是否断言正确提升?self.assertEqual很好.
我想keyboard在Python 3.5.3.
pip,import我的程序在Windows. 在我的 Raspberrypip install作品和pip list节目中keyboard。
但是,当我尝试运行时
import keyboard
,出现错误:“ImportError: No module named 'keyboard'”
我什至尝试sudo import按照键盘文档的建议使用,结果相同。
我错过了什么?
我有以下代码:
from numpy import random
class Person():
def __init__(self, name, age=random.randint(18,65)):
self.name = name
self.age = age
Run Code Online (Sandbox Code Playgroud)
我希望年龄是 18 到 65 之间的随机数,除非明确指定。但是,当我创建此类的不同实例时,如下所示:
p1 = Person('Bob')
p2 = Person('Sue')
p3 = Person('Jeff')
Run Code Online (Sandbox Code Playgroud)
每个人的年龄总是相同的。我怎样才能解决这个问题?
假设我要在'Core.dll'之后删除'git pull',所以我写了一个钩子。
import os
dir = os.path.dirname(__file__)
try:
os.remove(os.path.abspath(dir+os.sep+".."+os.sep+".."+os.sep+"Assets"+os.sep+"Plugins"+os.sep+"Core.dll"))
except OSError:
pass
Run Code Online (Sandbox Code Playgroud)
假设挂钩路径为'E:\client\.git\hooks',我要删除的文件位于'E:\client\Assets\Plugins\Core.dll'.
我认为我的方法很愚蠢,是否有任何优雅的方法来获得相对路径?
我在一个文件中给了一串希伯来字符(以及其他一些阿拉伯字符.我都不知道它们)
צוֹר
当我从Python3中的文件加载此字符串时
fin = open("filename")
x = next(fin).strip()
Run Code Online (Sandbox Code Playgroud)
长度x似乎是5
>>> len(x)
5
Run Code Online (Sandbox Code Playgroud)
它的unicode utf-8编码是
>>> x.encode("utf-8")
b'\xd7\xa6\xd7\x95\xd6\xb9\xd7\xa8\xe2\x80\x8e'
Run Code Online (Sandbox Code Playgroud)
但是,在浏览器中,很明显这些希伯来字符的长度为3.
如何正确地获得长度?为什么会发生这种情况?
我知道Python 3默认是unicode所以我没想到会出现这样的问题.
我正在尝试使用 os 模块验证作为用户输入接收的目录是否存在
这就是我接受输入的方式:
directory = input("Hi ! \n please type a directory, thanks !")
Run Code Online (Sandbox Code Playgroud)
这个想法是我想确保用户将输入一个现有的目录而不是别的
Python代码:
b=['six', 'small', 'he', 'foxes', 'saw']
b.sort(key=len)
b
Run Code Online (Sandbox Code Playgroud)
输出:
['he', 'six', 'saw', 'small', 'foxes']
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我按元素的长度排序并得到了上面的输出.
我想要的输出看起来像这样:['他','锯','六','小','狐狸']
所以我希望按长度进行第一次排序.然后对于具有相同长度的元素,我想按字母顺序排序.
我的问题:如果没有编写自定义函数,有没有办法做到这一点?也许是这样的:
b.sort(key=[len,sorted]) # this doesn't work but I think it explains the idea.
Run Code Online (Sandbox Code Playgroud)
谢谢!
python ×10
python-3.x ×4
list ×2
dictionary ×1
input ×1
numpy ×1
python-os ×1
random ×1
raspberry-pi ×1
sorting ×1
unit-testing ×1
validation ×1