我在预定义列表和查询字符串中有元素.我想检查query_str预定义列表中是否有任何值,如果是,请将它们作为单独的元素附加到新列表中.
predefined_lst = ['hello', 'goodbye', 'see you later']
query_str = 'hello | are you having a nice day? see you later |'
new_lst = []
Run Code Online (Sandbox Code Playgroud)
我有将字符串与列表中的值进行比较的语法,但是我无法将字符串中出现的值作为列表中的单个元素附加到新列表中.在上面的例子中,new_lst应该是new_lst = ['hello', 'see you later'].
我现在所拥有的只是在打印时产生True new_lst.
predefined_lst = ['hello', 'goodbye', 'see you later']
query_str = 'hello | are you having a nice day? see you later |'
new_lst = []
match = if any(string in query_str for string in predefined_lst)
new_lst.append(match)
print(new_lst)
Run Code Online (Sandbox Code Playgroud) 似乎numpy.resizenumba不支持.
numba.jit在nopython模式下使用动态增长数组的最佳方法是什么?
到目前为止,我能做的最好的事情是在jitted函数之外定义和调整数组,是否有更好(更整洁)的选项?
我试图与调试的问题abc.ABCMeta-特别是子类的支票,并没有按预期方式工作,我想通过简单地添加一个启动print的__subclasscheck__方法(我知道有调试代码更好的方法,但假装的缘故这个问题别无选择)。但是,在 Python 崩溃之后启动 Python 时(如分段错误),我收到此异常:
Fatal Python error: Py_Initialize: can't initialize sys standard streams
Traceback (most recent call last):
File "C:\...\lib\io.py", line 84, in <module>
File "C:\...\lib\abc.py", line 158, in register
File "C:\...\lib\abc.py", line 196, in __subclasscheck__
RuntimeError: lost sys.stdout
Run Code Online (Sandbox Code Playgroud)
所以把它放在那里显然不是一个好主意print。但异常究竟从何而来?我只更改了 Python 代码,应该不会崩溃吧?
有人知道这个异常来自哪里以及我是否/如何避免它但仍然print在abc.ABCMeta.__subclasscheck__方法中放入一个?
我使用的是 Windows 10、Python-3.5(以防万一它可能很重要)。
我有2个不同长度的数字列表,例如:
list1 = [1, 2, -3, 4, 7]
list2 = [4, -6, 3, -1]
Run Code Online (Sandbox Code Playgroud)
我需要使用函数迭代这些:
final_list = []
for index in range(???):
if list1[index] < 0:
final_list.insert(0, list1[index])
elif list1[index] > 0:
final_list.insert(len(final_list), list1[index])
if list2[index] < 0:
final_list.insert(0, list2[index])
elif list2[index] > 0:
final_list.insert(len(final_list), list2[index])
return final_list
Run Code Online (Sandbox Code Playgroud)
但无法弄清楚如何处理范围,因为如果我使用max长度,较短的列表将变得"超出范围" .有关如何克服这个或如何改变我的功能的任何想法?
我有这个代码:
class weapon():
def __init__(self, Name, Type, Description):
self.Name=Name
self.Type=Type
self.Description=Description
WEAPONS = { "starterSword":weapon("Starter Sword", "Sword", "A short, stunt steel sword."),
"basicSword":weapon("Basic Sword", "Sword", "A basic steel sword.")
}
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情:
for item in WEAPONS:
print(self.Name)
Run Code Online (Sandbox Code Playgroud)
我将如何在Python 3中进行此操作?
我的字典有问题。我正在使用Python3。我敢肯定我只是看不到一些简单的东西。
我正在从文件中读取行以创建字典。每行的前3个字符用作键(它们是唯一的)。从那里,我根据该行其余部分的信息创建一个列表。每4个字符组成一个列表。创建列表后,我将目录作为值写入目录,并将行的前三个字符作为键。
问题是,每次我向字典中添加新的key:value对时,它似乎都会覆盖(或更新)先前编写的字典条目中的值。键很好,只是更改了值。因此,最后,所有键的值都等于文件最后一行的列表。
我希望这很清楚。任何想法将不胜感激。
下面的代码片段
formatDict = dict()
sectionList = list()
for usableLine in formatFileHandle:
lineLen = len(usableLine)
section = usableLine[:3]
x = 3
sectionList.clear()
while x < lineLen:
sectionList.append(usableLine[x:x+4])
x += 4
formatDict[section] = sectionList
for k, v in formatDict.items():
print ("for key= ", k, "value =", v)
formatFileHandle.close()
Run Code Online (Sandbox Code Playgroud) 关于这两个选项:
try:
userid = get_userid()
except:
userid = ""
Run Code Online (Sandbox Code Playgroud)
与
userid = ""
try:
userid = get_userid()
except:
pass
Run Code Online (Sandbox Code Playgroud)
是否有任何区别,特别想知道如果userid仅在try块中设置命名空间将如何工作?它们是否都具有相同的命名空间范围?
一个比另一个更受欢迎吗?
我不完全确定这种行为是否符合预期,但这绝对是很奇怪的。当你有这样的代码时:
def a_function():
if a_list != some_other_list:
print(a_list)
Run Code Online (Sandbox Code Playgroud)
它工作得很好,我没有遇到任何问题。但是,如果将其更改为:
def a_function():
if a_list != some_other_list:
a_list = some_other_list
Run Code Online (Sandbox Code Playgroud)
突然,出现了一个问题,提示a_list第 2 行是未解析的引用。为什么if语句中的内容会影响能否a_list解决呢?这种事情正常吗?这可能是 Python 3.6.1 或 PyCharm(社区版 2017.1.5)中的错误吗?任何澄清这一点的帮助将不胜感激。
我有一个关于列表浅复制的问题。
在这两个示例中,我都修改了列表的一个元素,但在示例 1 中,列表b发生了更改,而在示例 2 中,列表d没有更改。我很困惑,因为在这两个示例中,我修改了列表的元素。
有什么不同?
示例1:
a=[1,2,[3,5],4]
b=list(a)
a[1]=0
print(a) # [1, 0, [3, 5], 4]
print(b) # [1, 2, [3, 5], 4]
Run Code Online (Sandbox Code Playgroud)
示例2:
c=[1,2,[3,5],4]
d=list(c)
c[2][0]=0
print(c) # [1, 2, [0, 5], 4]
print(d) # [1, 2, [0, 5], 4]
Run Code Online (Sandbox Code Playgroud) 自引入以来bool,它一直是子类int,并且bool可以隐式地"转换"为整数:
>>> issubclass(bool, int)
True
>>> ['one', 'two'][False]
'one'
>>> ['one', 'two'][True]
'two'
>>> True/20
0.05
Run Code Online (Sandbox Code Playgroud)
这是出于历史原因:与2.3之前的API的兼容性; 我明白它保持在2.3到2.7之间.(这在2011年的这个问题中得到了解决)
但是,为什么它仍然适用于Python 3?我认为没有任何优势.并且没有理由为了向后兼容而保持这一点:Python 3.0是一个突破性版本; 并且我认为任何2.3之前的API都不会存在.
python ×10
list ×4
python-3.x ×4
dictionary ×2
boolean ×1
cpython ×1
debugging ×1
function ×1
iteration ×1
loops ×1
numba ×1
numpy ×1
pycharm ×1
python-2.7 ×1
scope ×1
shallow-copy ×1
string ×1