是否有一种简单的方法来检测最后一次迭代,同时使用iteritems()?
假设我有一个列表列表或元组列表,无论哪个都可以更有效地解决我的问题.例如:
student_tuples = [
('john', 'A', 15),
('jane', 'B', 12),
('dave', 'B', 10),
]
Run Code Online (Sandbox Code Playgroud)
任务是基于作为内部列表或元组的任何元素的键在主列表中查找元素.例如:
使用上面的列表:
find(student_tuples, 'A')
Run Code Online (Sandbox Code Playgroud)
要么
find(student_tuples, 15)
Run Code Online (Sandbox Code Playgroud)
都会回来的
('john', 'A', 15)
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种有效的方法.
我需要存储具有数字(> 2)整数成员变量的对象,并使用任何成员变量作为搜索键进行快速查找.
为了便于说明,假设对象是3个整数的元组,我需要使用元组的任何元素作为此类元组列表中的键进行快速查找:
collection = [(1, 200, 9),
(2, 300, 8),
(3, 400, 7)]
Run Code Online (Sandbox Code Playgroud)
查找将是:
collection.lookup_by_first_element(1) # Return (1, 200, 9)
collection.lookup_by_second_element(300) # Return (2, 300, 8)
collection.lookup_by_third_element(250) # Return None
Run Code Online (Sandbox Code Playgroud)
我需要查找快速/高效.到目前为止,我最好的选择是使用内存sqlite数据库,其中三列为三个元组,并在所有三列上放置索引.
搜索树也可以,但是那些有一个查找键,我不知道如何根据多个键进行查找.你会怎么做?
这是一个随机整数的例子:
a, b, c, d = 79412623, 56529819571, 10431, 30461
t = (79412623, 56529819571, 10431, 30461)
Run Code Online (Sandbox Code Playgroud)
它们的尺寸:
import sys
sys.getsizeof(t) # 88
aa, bb, cc, dd = sys.getsizeof(a), sys.getsizeof(b), sys.getsizeof(c), sys.getsizeof(d)
sum([aa,bb,cc,dd]) # 96
Run Code Online (Sandbox Code Playgroud)
为什么元组占用的空间更少?
假设我有names一个包含任意顺序名称元组的元组列表:
names = [(1,"Alice"), (2,"Bob")]
Run Code Online (Sandbox Code Playgroud)
以及genders以任意顺序包含性别元组的另一个元组列表:
genders = [(2,"male"), (1,"female")]
Run Code Online (Sandbox Code Playgroud)
如何通过使用元组的第一个元素作为获取的键来有效地匹配这两个列表:
result = [("Alice","female"), ("Bob","male")]
Run Code Online (Sandbox Code Playgroud) 设置:containerView在屏幕内有一个UIViewController(为了简单起见,我们说containerView占用整个屏幕).
问题:创建并添加overlayView一个asView作为子视图containerView并为其设置动画,以便从右侧动画到位置作为对用户操作的响应.
UIView *overlayView = [[UIView alloc] initWithFrame:containerView.frame];
overlayView.translatesAutoresizingMaskIntoConstraints = NO;
[containerView addSubview:overlayView]; // Can't add constraints before inserting into view hierarchy
Run Code Online (Sandbox Code Playgroud)
做法:领带的前沿overlayView到的前沿containerView与我会打电话的约束overlayLeadingConstraint.设置overlayLeadingConstraint.constant为宽度containerView,使其最初位于屏幕右侧.
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[overlayView(width)]" options:0 metrics:metrics views:views];
[containerView addConstraints:constraints];
NSLayoutConstraint *overlayViewLeadingConstraint = [constraints objectAtIndex:0];
overlayViewLeadingConstraint.constant = containerView.frame.size.width;
// Height constraint not shown for simplicity
Run Code Online (Sandbox Code Playgroud)
动画:现在到了真正的问题; overlayView从右边开始动画.第一种方法是这样的:
overlayViewLeadingConstraint.constant = 0.0;
[UIView animateWithDuration:1.0 …Run Code Online (Sandbox Code Playgroud)