GVKun编程网logo

c# – .NET中的哪些数据结构在Contains()调用中执行O(1)?(c#中提供了哪几种数据类型)

18

以上就是给各位分享c#–.NET中的哪些数据结构在Contains()调用中执行O(1)?,其中也会对c#中提供了哪几种数据类型进行解释,同时本文还将给你拓展.net–LinqwithLeftJoin

以上就是给各位分享c# – .NET中的哪些数据结构在Contains()调用中执行O(1)?,其中也会对c#中提供了哪几种数据类型进行解释,同时本文还将给你拓展.net – Linq with Left Join on SubQuery contains Count、.net – Windsor Container:在Code vs Xml中注册内容、.net 大数据量,查找 Where 优化(List 的 Contains 与 Dictionary 的 ContainsKey 的比较)、63.Python中contains和icontains等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

c# – .NET中的哪些数据结构在Contains()调用中执行O(1)?(c#中提供了哪几种数据类型)

c# – .NET中的哪些数据结构在Contains()调用中执行O(1)?(c#中提供了哪几种数据类型)

我在这里画一个空白;我找不到它,除非我真的忽略了我鼻子下的东西.

我正在尝试在数据结构中存储一个int列表.
但是在我添加它们之后,我将在稍后检查列表中是否存在int.

通用List< int>使用Contains()执行O(n)操作.
我想要的东西和Dictionary<>的Contains()一样快,它执行O(1)操作,因为它散列了键.

我知道答案很简单,我今天工作的时间太长了,我记不起来了.

救命!

解决方法

HashSet<T>会为你工作吗?

.net – Linq with Left Join on SubQuery contains Count

.net – Linq with Left Join on SubQuery contains Count

我在将sql转换为 linq语法时遇到困难.

我有2个表(Category和CategoryListing),它们使用CategoryID互相引用.我需要在CategoryListing表中获取Category Category中所有CategoryID的列表和CategoryID中所有相应匹配的CountID.如果CategoryListing中不存在CategoryID,则仍应返回CategoryID – 但频率为0.

以下SQL查询演示了预期的结果:

SELECT c.CategoryID,COALESCE(cl.frequency,0) as frequency
FROM Category c
LEFT JOIN (
    SELECT cl.CategoryID,COUNT(cl.CategoryID) as frequency 
    FROM CategoryListing cl
    GROUP BY cl.CategoryID
) as cl
ON c.CategoryID = cl.CategoryID
WHERE c.GuideID = 1

解决方法

没有经过测试,但这应该可以解决问题:
var q = from c in ctx.Category
        join clg in 
            (
                from cl in ctx.CategoryListing
                group cl by cl.CategoryID into g
                select new { CategoryID = g.Key,Frequency = g.Count()}
            ) on c.CategoryID equals clg.CategoryID into cclg
        from v in cclg.DefaultIfEmpty()
        where c.GuideID==1
        select new { c.CategoryID,Frequency = v.Frequency ?? 0 };

.net – Windsor Container:在Code vs Xml中注册内容

.net – Windsor Container:在Code vs Xml中注册内容

从我读到的关于Windsor / Microkernel的内容来看,理论上可以使用带代码的xml文件来完成所有工作.事实上 – 如果我错了,请纠正我 – 似乎Windsor层的主要贡献是为Microkernel已经可以做的事情添加xml配置.

但是,我最近一直在努力寻找如何在代码中实现一些稍微复杂的功能(即how to assign a default constructor argument value).现在,当我要在我的生产版本中使用xml时,我正在为我的测试注册代码中的组件,这就变得非常棘手.不幸的是他们的文档状态以及我能找到的唯一文章专注于xml注册这一事实并没有帮助.

有没有人知道一个源代码,它列出了如何在代码中注册事物(最好用xml等价物)?除了存在之外,是否有人只是知道一个开源/示例项目,其中有很多非xml使用Castle Windsor / Microkernel?

解决方法

我总是发现单元测试是学习如何使用开源项目的最佳方式. Castle具有流畅的界面,允许您在代码中执行所有操作.从 WindsorDotNet2Tests测试用例:

[Test]
    public void ParentResolverIntercetorShouldNotAffectGenericComponentInterceptor()
    {
        WindsorContainer container = new WindsorContainer();
        container.AddComponent<MyInterceptor>();

        container.Register(
            Component.For<ISpecification>()
                .ImplementedBy<MySpecification>()
                .Interceptors(new InterceptorReference(typeof(MyInterceptor)))
                .Anywhere
            );
        container.AddComponent("repos",typeof(IRepository<>),typeof(TransientRepository<>));

        ISpecification specification = container.Resolve<ISpecification>();
        bool isProxy = specification.Repository.GetType().FullName.Contains("Proxy");
        Assert.IsFalse(isProxy);
    }

有关更多信息,请查看ComponentRegistrationTestCase和AllTypesTestCase

还有一个用于执行此操作的DSL,这是我的首选选项,因为它真正简化了事物并提供了很多易于扩展的功能. DSL被称为Binsor,您可以在这里阅读更多信息:http://www.ayende.com/Blog/archive/7268.aspx但是,forfor的最佳位置是单元测试.这是binsor可能的代码示例:

for type in AllTypesBased of IController("Company.Web.Controller"):
    component type

这两行将注册将IController接口继承到容器中的类型:D

.net 大数据量,查找 Where 优化(List 的 Contains 与 Dictionary 的 ContainsKey 的比较)

.net 大数据量,查找 Where 优化(List 的 Contains 与 Dictionary 的 ContainsKey 的比较)

 最近优化一个 where 查询条件,查询时间很慢,改为用 Dictionary 就很快了。

 

 一、样例

 

假设:listPicsTemp 有 100w 条数据,pictures 有 1000w 条数据。

 

使用第 1 段代码执行超过 2 分钟。

var listPicsTemp = new List<string>();

pictures = pictures.AsParallel().Where(d => listPicsTemp.Contains(d.Pic)).ToList();

 

使用第 2 段代码执行十几毫秒。

var listPicsTemp = new List<string>();

var dicPicsTemp = listPicsTemp.Where(d => d != null).Distinct().ToDictionary(d => d);//使用Dictionary类型,速度快很多

pictures = pictures.AsParallel().Where(d => dicPicsTemp.ContainsKey(d.Pic)).ToList();

 

二、为什么 Dictionary 这么快呢?查看了一下微软官方文档。

 

下面截图来源:https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.7.2

 

 

三、查看源码

 

List 的源码:https://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,cf7f4095e4de7646

List 的 Contains,是循环 for 查找的。

 

Dictionary 的源码: https://referencesource.microsoft.com/#mscorlib/system/collections/generic/dictionary.cs,bcd13bb775d408f1

Dictionary 的 ContainsKey,是通过 hash 查找的。

 

 

 四、小结:

1、Dictionary<TKey,TValue> 类实现为哈希表。ContainsKey () 内部是通过 Hash 查找实现的,查询的时间复杂度是 O (1)。所以,查询很快。(List 的 Contains 是通过 for 查找的)

2、Dictionary 不是线程安全的。(查看微软官方文档,确实能学到很多知识盲区。)

63.Python中contains和icontains

63.Python中contains和icontains

1. contains: 进行大小写敏感的判断,某个字符串是否包含在指定的字段中,这个判断条件使用大小写敏感进行判断,因此在被翻译成“SQL”语句的时候,会使用“like binary”, 而“like binary”就是使用大小写敏感进行判断。

2. icontains: 进行大小写不敏感的判断,某个字符串是否包含在指定的字段中,这个判断条件使用大小写不敏感进行判断,因此在被翻译成“SQL”语句的时候,会使用“like”, 而“like”就是使用大小写不敏感进行判断。其中icontains前面的i指的就是ignore(忽略)。

具体实例,示例代码如下:
models.py文件中模型的定义,示例代码如下:
from django.db import models


class Article(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()

# 重写类的__str__(self)方法
    def __str__(self):
        return "<(Article: id: %s,title: %s, content: %s)>" % (self.id, self.title, self.content)

# 重新定义表的一些属性,注意:此处的类的名字一定要为Meta
    class Meta:
        db_table = ''article''
1. contains: views.py文件中视图函数的示例代码如下:
from django.http import HttpResponse
from .models import Article


def index(request):
# 注意:此处是使用filter(),只有使用filter()之后才能够在article(QuerySet)上调用query属性,查看django底层转化的sql语句。
	article = Article.objects.filter(title__contains=''hello'')
	print(article)
	print(article.query)
	return HttpResponse(''success !'')
在mysql数据库中article表的数据信息如下:

在这里插入图片描述

因为contains进行的是大小写敏感的查找,所以不能找到匹配的数据,则会返回QuerySet为空。并且我们可以看到执行我们的查找条件的时候,django底层翻译成的sql语句中的:WHERE article.title LIKE BINARY %hello%,这里的LIKE BINARY 就可以保证进行的查找为大小写敏感的查找,并且hello字符串的两边会有%(代表hello字符串前面可以含有别的字符,后面也可以含有别的字符),这就可以验证我们上面的解释。

在这里插入图片描述

2.icontains: views.py文件中视图函数示例代码如下:
from django.http import HttpResponse
from .models import Article


def index(request):
    # icontains进行查找title中包含“hello”的字符串
    article = Article.objects.filter(title__icontains=''hello'')
    print(article)
    print(article.query)
    return HttpResponse("success")
因为使用icontains采用的是大小写不敏感的的查找方式,所以会返回两条满足条件的QuerySet,并且重以下打印的结果中可以看出,我们的查询条件被翻译成了:WHERE article.title LIKE %hello%,这里的LIKE进行的查找就是大小写不敏感的查找。

在这里插入图片描述

总结:iexact和exact进行的是准确性的查找,只有完全匹配我们的值“hello”的时候,才能够被找到。而icontains和contains中的查询条件,在进行查找的时候,会被翻译成sql语句中包含“%hello%”,在这里%意味着,前面可以含有n个字符,后面也可以含有n个字符。只要数据中包含hello字符串,就满足条件。

今天关于c# – .NET中的哪些数据结构在Contains()调用中执行O(1)?c#中提供了哪几种数据类型的分享就到这里,希望大家有所收获,若想了解更多关于.net – Linq with Left Join on SubQuery contains Count、.net – Windsor Container:在Code vs Xml中注册内容、.net 大数据量,查找 Where 优化(List 的 Contains 与 Dictionary 的 ContainsKey 的比较)、63.Python中contains和icontains等相关知识,可以在本站进行查询。

本文标签: