小编rag*_*ner的帖子

检查列表字典中是否有值的最佳方法?

所以我有一个像这样的列表字典:

dct = {'1': ['hello','goodbye'], '2': ['not here','definitely not here']}
Run Code Online (Sandbox Code Playgroud)

什么是检查'hello'是否在我的词典中的某个列表中的最快方法

python dictionary list python-3.x

8
推荐指数
1
解决办法
3100
查看次数

Python 3.x 列表推导 VS 元组生成器

是否有任何我想使用的内存、速度或其他原因:

tuple(i for i in range(5000))
Run Code Online (Sandbox Code Playgroud)

代替:

[i for i in range(5000)]
Run Code Online (Sandbox Code Playgroud)

如果我不介意元组的不变性

python performance list generator python-3.x

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

Python在列表或数组中的范围之间查找数字

我有一个包含数百万个数字的列表,这些数字总是在增加到最后,我需要找到并返回指定范围内的数字,例如大于X但小于Y的数字,列表中的数字可以改变,值是我的也在寻找变化

我一直在使用这种方法,请注意这是一个基本的例子,数字不一致或与我的程序中显示的相同

l = [i for i in range(2000000)]
nums = []
for element in l:
    if element > 950004:
        break
    if element > 950000:
        nums.append(element)
#[950001, 950002, 950003, 950004]
Run Code Online (Sandbox Code Playgroud)

虽然速度很快,但我需要它对我的程序运行速度要快一些,数字会发生很大变化,所以我想知道是否有更好的方法可以用pandas系列或numpy数组做到这一点?但到目前为止,我所做的只是在numpy中做一个例子:

a = numpy.array(l,dtype=numpy.int64)
Run Code Online (Sandbox Code Playgroud)

大熊猫系列会更实用吗?利用query()?使用数组而不是python对象的python列表来处理这个问题的最佳方法是什么

python arrays numpy list pandas

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

Python 打印缩进

在 Python 3.x 中你如何打印空格的缩进,我正在寻找的方法看起来像这样

level = 3
print ('% indent symbol' % * level, 'indented text')
Run Code Online (Sandbox Code Playgroud)

应该打印:

            indented text
Run Code Online (Sandbox Code Playgroud)

重复下面的答案:感谢@sudo_coffee \t

print ('\t' * level, 'indented text')
Run Code Online (Sandbox Code Playgroud)

或者可能:

print ((' ' * 4) * level, 'indented text')
Run Code Online (Sandbox Code Playgroud)

python printing string

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

在不使用类/对象的情况下递归创建树层次结构

我在 Python 3 中创建树层次结构时遇到问题。我希望能够在不使用类的情况下做到这一点。

我需要开始的数据没有顺序和格式['ID','Parent']

data=[['E1', 'C1'],['C1', 'P1'],['P1', 'R1'],['E2', 'C2'],['C2', 'P2'],['P2', 'R1'],['C3', 'P2'],['E3', 'C4'],['C4', 'P3'],
  ['P3', 'R2'],['C5', 'P3'],['E4', 'C6'],['C6', 'P4'], ['P4', 'R2'],['E5', 'C7'],['C7', 'P5'],['P5', 'R3'],['E6', 'C9'],['C9', 'P6'],['P6', 'R3'],
  ['C8', 'P6'],['E7', 'C10'],['C10', 'P7'],['P7', 'R4'],['C11', 'P7'],['E8', 'C12'],['C12', 'P8'],['P8', 'R4']]
Run Code Online (Sandbox Code Playgroud)

我想在不使用类的情况下创建 (Tree) 字典变量,最终得到如下结果:

Tree={'R1':{'P1':{},'P2':{}},'R2':{}} etc
Run Code Online (Sandbox Code Playgroud)

或者

Tree={'R1':[{'P1':[],'P2':[]}],'R2':[]} etc
Run Code Online (Sandbox Code Playgroud)

显然 R1 和 R2 有更多的孩子,但也许这就是树结构的样子?

python tree recursion hierarchy python-3.x

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

Python 3如果不是条件简化

这两个条件检查是否相同?我想不出如何检查它们是否相同

l1 = []
l2 = []

if not l1 and not l2:
    print ('y')

if not (l1 and l2):
    print ('y')
Run Code Online (Sandbox Code Playgroud)

感谢所有回复的人,我已经做了一些基本的时机,看看哪个更快

import time
l1 = []
l2 = []

st = time.time()
for i in range(100000000):
    if not l1 and not l2:
        pass
end = time.time()
print ('if not l1 and not l2: '+str(end-st))

st = time.time()
for i in range(100000000):
    if not (l1 or l2):
        pass
end = time.time()
print ('if not (l1 or l2): '+str(end-st))
Run Code Online (Sandbox Code Playgroud)

打印: …

python if-statement python-3.x

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

Python检查列表是否只包含空元素或空格

我想检查一个列表是否只包含空元素或空格,如:

l = ['','   ','\n']
if all(whitespace or empty for element in l):
    return True
Run Code Online (Sandbox Code Playgroud)

有人知道怎么做吗?

python whitespace boolean list

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

Python tkinter treeview获取/返回所选项目的iid

iid它的目的是当用户单击某个项目并将其打印出来时获取树视图项目的 ,但由于某种原因identify()没有收到 event.y 变量,也许?

import tkinter as tk
from tkinter import ttk

class App:
    def __init__(self):
        self.root = tk.Tk()
        self.tree = ttk.Treeview()
        self.tree.pack(side="top", fill="both")
        self.tree.bind("<<TreeviewSelect>>", self.tree_click_event)

        for i in range(10):
            self.tree.insert("", "end", text="Item %s" % i)

        self.root.mainloop()

    def tree_click_event(self, event):
        iid = self.tree.identify(event.x,event.y)
        print (iid)

if __name__ == "__main__":
    app = App()
Run Code Online (Sandbox Code Playgroud)

单击树视图中的项目后,错误为:

TypeError: identify() missing 1 required positional argument: 'y'
Run Code Online (Sandbox Code Playgroud)

为了响应 @TessellationHeckler 的评论,基于链接对代码进行的编辑不会产生错误,但仍然不会打印出iid

def tree_click_event(self, event):
    item = self.tree.identify('item', event.x,event.y)
    print (item)
Run Code Online (Sandbox Code Playgroud)

感谢您接受@CommonSense的答案,总结看来我需要使用 self.tree.bind('<1>', …

python treeview tkinter

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

Python tkinter treeview获取/返回所选项的父名称

我试图在选择事件时返回tkinter树视图选择的父级,所以如果选择更改为"child",我希望它打印"parent",下面的工作示例,目前它打印选择,而不是父级选择:

try:
    import tkinter as tk
    import tkinter.ttk as ttk
except ImportError:
    import Tkinter as tk
    import ttk

class App:
    def __init__(self):
        self.root = tk.Tk()
        self.tree = ttk.Treeview(selectmode='browse')
        self.tree.pack(side="top", fill="both")
        self.tree.bind('<<TreeviewSelect>>', self.tree_select_event)
        self.parent_iid = self.tree.insert("", "end", text="Parent")
        self.child_iid = self.tree.insert(self.parent_iid, "end", text="Child")

        self.root.mainloop()

    def tree_select_event(self, event):
        print (self.tree.item(self.tree.selection()[0])['text'])

if __name__ == "__main__":
    app = App()
Run Code Online (Sandbox Code Playgroud)

目前在选择Child时打印:

"Child"
Run Code Online (Sandbox Code Playgroud)

选择孩子时所需的输出:

"Parent"
Run Code Online (Sandbox Code Playgroud)

python treeview tkinter

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

确定列表中列的最长字符串长度的最快方法

我有一个包含5000个键的字典,其值是列表,我需要快速确定所有字典值中给定索引的最长字符串的长度

index = 1
d = {'foo': ['abc', 'defg'],
     'bar': ['hij', 'klmno']}
#m = len(max(d.values()[index],key=len))?
Run Code Online (Sandbox Code Playgroud)

预期输出:5,因为索引1处的所有值('defg'和'klmno',后者是最长的).

python string dictionary

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