我想一次遍历一个Python列表并处理2个列表项。在另一种语言中是这样的:
for(int i = 0; i < list.length(); i+=2)
{
// do something with list[i] and list[i + 1]
}
实现这一目标的最佳方法是什么?
我想一次遍历一个Python列表并处理2个列表项。在另一种语言中是这样的:
for(int i = 0; i < list.length(); i+=2)
{
// do something with list[i] and list[i + 1]
}
实现这一目标的最佳方法是什么?
元组和列表在元素的实例化和检索方面有什么性能差异吗?
我正在学习/试验Rust,在我发现这门语言的所有优雅之处中,有一个特点让我困惑,似乎完全不合适。
Rust在进行方法调用时自动解除对指针的引用。我做了一些测试来确定准确的行为:
struct X { val: i32 }
impl std::ops::Deref for X {
type Target = i32;
fn deref(&self) -> &i32 { &self.val }
}
trait M { fn m(self); }
impl M for i32 { fn m(self) { println!("i32::m()"); } }
impl M for X { fn m(self) { println!("X::m()"); } }
impl M for &X { fn m(self) { println!("&X::m()"); } }
impl M for &&X { fn m(self) { println!("&&X::m()"); } }
impl M for &&&X { fn m(self) { println!("&&&X::m()"); } }
trait RefM { fn refm(&self); }
impl RefM for i32 { fn refm(&self) { println!("i32::refm()"); } }
impl RefM for X { fn refm(&self) { println!("X::refm()"); } }
impl RefM for &X { fn refm(&self) { println!("&X::refm()"); } }
impl RefM for &&X { fn refm(&self) { println!("&&X::refm()"); } }
impl RefM for &&&X { fn refm(&self) { println!("&&&X::refm()"); } }
struct Y { val: i32 }
impl std::ops::Deref for Y {
type Target = i32;
fn deref(&self) -> &i32 { &self.val }
}
struct Z { val: Y }
impl std::ops::Deref for Z {
type Target = Y;
fn deref(&self) -> &Y { &self.val }
}
#[derive(Clone, Copy)]
struct A;
impl M for A { fn m(self) { println!("A::m()"); } }
impl M for &&&A { fn m(self) { println!("&&&A::m()"); } }
impl RefM for A { fn refm(&self) { println!("A::refm()"); } }
impl RefM for &&&A { fn refm(&self) { println!("&&&A::refm()"); } }
fn main() {
// I'll use @ to denote left side of the dot operator
(*X{val:42}).m(); // i32::m() , Self == @
X{val:42}.m(); // X::m() , Self == @
(&X{val:42}).m(); // &X::m() , Self == @
(&&X{val:42}).m(); // &&X::m() , Self == @
(&&&X{val:42}).m(); // &&&X:m() , Self == @
(&&&&X{val:42}).m(); // &&&X::m() , Self == *@
(&&&&&X{val:42}).m(); // &&&X::m() , Self == **@
println!("-------------------------");
(*X{val:42}).refm(); // i32::refm() , Self == @
X{val:42}.refm(); // X::refm() , Self == @
(&X{val:42}).refm(); // X::refm() , Self == *@
(&&X{val:42}).refm(); // &X::refm() , Self == *@
(&&&X{val:42}).refm(); // &&X::refm() , Self == *@
(&&&&X{val:42}).refm(); // &&&X::refm(), Self == *@
(&&&&&X{val:42}).refm(); // &&&X::refm(), Self == **@
println!("-------------------------");
Y{val:42}.refm(); // i32::refm() , Self == *@
Z{val:Y{val:42}}.refm(); // i32::refm() , Self == **@
println!("-------------------------");
A.m(); // A::m() , Self == @
// without the Copy trait, (&A).m() would be a compilation error:
// cannot move out of borrowed content
(&A).m(); // A::m() , Self == *@
(&&A).m(); // &&&A::m() , Self == &@
(&&&A).m(); // &&&A::m() , Self == @
A.refm(); // A::refm() , Self == @
(&A).refm(); // A::refm() , Self == *@
(&&A).refm(); // A::refm() , Self == **@
(&&&A).refm(); // &&&A::refm(), Self == @
}
(游乐场)
所以,看起来,或多或少:
The compiler will insert as many dereference operators as necessary to invoke a method. The compiler, when resolving methods declared using &self (call-by-reference): First tries calling for a single dereference of self Then tries calling for the exact type of self Then, tries inserting as many dereference operators as necessary for a match Methods declared using self (call-by-value) for type T behave as if they were declared using &self (call-by-reference) for type &T and called on the reference to whatever is on the left side of the dot operator. The above rules are first tried with raw built-in dereferencing, and if there's no match, the overload with Deref trait is used.
确切的自动解引用规则是什么?有人能给出这样一个设计决策的正式理由吗?
比较两个庞大(>50.000项)的最快(和最少资源密集型)的方法是什么,从而得到如下所示的两个列表:
在第一个列表中出现但在第二个列表中没有出现的项目 出现在第二个列表中但不在第一个列表中的项目
目前,我正在使用列表或IReadOnlyCollection,并在linq查询中解决这个问题:
var list1 = list.Where(i => !list2.Contains(i)).ToList();
var list2 = list2.Where(i => !list.Contains(i)).ToList();
但这并不像我想的那样好。 有什么想法使这更快和更少的资源密集,因为我需要处理很多列表?
我发现他们在文档的前两章中定义语言的方法和方式特别有趣。所以我决定试一试,从“你好,世界!”开始。
顺便说一句,我是在Windows 7 x64上这么做的。
fn main() {
println!("Hello, world!");
}
发布cargo build并在目标\调试中查看结果,我发现结果的.exe为3MB。经过一番搜索(cargo命令行标志的文档很难找到…)我找到了——release选项并创建了发布版本。令我惊讶的是,.exe的大小只变小了一点:2.99MB而不是3MB。
我的期望是系统编程语言会产生一些紧凑的东西。
谁能详细解释一下Rust编译的目的是什么,它是如何从一个3行程序生成如此巨大的图像的?它是否编译到虚拟机?是否有我错过的strip命令(发布版本中的调试信息?)?还有什么能让我们理解的吗?
我有几个方法返回不同的泛型列表。
在。net中存在任何类静态方法或将任何列表转换为数据表?我唯一能想到的是使用反射来做到这一点。
如果我有这个:
List<Whatever> whatever = new List<Whatever>();
(这下代码当然不工作,但我想有这样的可能性:
DataTable dt = (DataTable) whatever;
我有几个方法返回不同的泛型列表。
在。net中存在任何类静态方法或将任何列表转换为数据表?我唯一能想到的是使用反射来做到这一点。
如果我有这个:
List<Whatever> whatever = new List<Whatever>();
(这下代码当然不工作,但我想有这样的可能性:
DataTable dt = (DataTable) whatever;
这是我一开始的资料。
我的清单
L = [0, 23, 234, 89, None, 0, 35, 9]
当我运行这个:
L = filter(None, L)
我得到了这个结果
[23, 234, 89, 35, 9]
但这不是我需要的,我真正需要的是:
[0, 23, 234, 89, 0, 35, 9]
因为我计算的是数据的百分位数0有很大的不同。
如何从列表中删除无值而不删除0值?
2016年12月7日在GitHub博客上宣布的一项功能,引入了向Pull Request添加评审员的选项
您现在可以明确地向合作者请求审查,从而更容易指定您希望谁审查您的pull请求。 您还可以在拉请求页面侧栏中看到您正在等待审阅的人员的列表,以及已经离开审阅的人员的审阅状态。
然而,PR的显式审查员设置已经通过分配人员(受让人选项)完成。
现在两个选项都有了,既然它们都有相同的最终目标,那么每个选项的作用是什么呢?
我试图将一个列表拆分为一系列较小的列表。
我的问题:我的拆分列表功能没有将它们拆分为正确大小的列表。它应该把它们分成大小为30的列表,但它却把它们分成大小为114的列表?
如何让我的函数将一个列表分割成X个大小为30或更小的列表?
public static List<List<float[]>> splitList(List <float[]> locations, int nSize=30)
{
List<List<float[]>> list = new List<List<float[]>>();
for (int i=(int)(Math.Ceiling((decimal)(locations.Count/nSize))); i>=0; i--) {
List <float[]> subLocat = new List <float[]>(locations);
if (subLocat.Count >= ((i*nSize)+nSize))
subLocat.RemoveRange(i*nSize, nSize);
else subLocat.RemoveRange(i*nSize, subLocat.Count-(i*nSize));
Debug.Log ("Index: "+i.ToString()+", Size: "+subLocat.Count.ToString());
list.Add (subLocat);
}
return list;
}
如果我在144大小的列表上使用该函数,那么输出是:
指数:4,尺寸:120 指数:3,尺寸:114 指数:2,尺寸:114 指数:1,尺寸:114 索引:0,大小:114