小编Gil*_*een的帖子

Regex.Replace中的MatchEvaluator如何工作?

这是输入字符串23x * y34x2.我希望" * "在每个数字之后插入(由空格包围的星形)后跟字母,并在每个字母后跟数字.所以我的输入字符串看起来像这样:23 * x * y * 34 * x * 2.

这是完成这项工作的正则表达式:@"\d(?=[a-z])|[a-z](?=\d)".这是我写的插入的函数" * ".

Regex reg = new Regex(@"\d(?=[a-z])|[a-z](?=\d)");   
MatchCollection matchC;
matchC = reg.Matches(input);
int ii = 1;
foreach (Match element in matchC)//foreach match I will find the index of that match
{
    input = input.Insert(element.Index + ii, " * ");//since I' am inserting " * " ( 3 characters )
    ii += 3;                                        //I must …
Run Code Online (Sandbox Code Playgroud)

.net c# regex replace matchevaluator

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

Python:如何获得itertools _grouper的长度

我正在使用Python itertools并使用groupby按最后一个元素对一堆对进行排序.我已经把它排序了,我可以很好地遍历组,但我真的很想能够得到每个组的长度,而不必遍历每个组,增加一个计数器.

该项目是集群的一些数据点.我正在使用(numpy.array,int)对,其中numpy数组是一个数据点,整数是一个簇标签

这是我的相关代码:

data = sorted(data, key=lambda (point, cluster):cluster)
for cluster,clusterList in itertools.groupby(data, key=lambda (point, cluster):cluster):
    if len(clusterList) < minLen:
Run Code Online (Sandbox Code Playgroud)

在最后一行:if len(clusterList) < minLen:,我得到一个错误

'itertools._grouper'类型的对象没有len()

我查找了可用的操作_groupers,但找不到任何似乎提供组长度的内容.

python group-by python-itertools

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

Linq中的Row_number(分区xxx)?

我有一个DataTable具有这种结构和数据:

id |   inst   |   name
------------------------
 1 |  guitar  |  john
 2 |  guitar  |  george
 3 |  guitar  |  paul
 4 |  drums   |  ringo
 5 |  drums   |  pete

我可以检索这样的记录:

IEnumerable <Beatle>...

class Beatle
{
  int id;
  string inst;
  string name;
}
Run Code Online (Sandbox Code Playgroud)

我想获得那些演奏不同乐器的人的内部秩序.在MSSQL我会用

SELECT 
    *
    ,Row_Number() OVER (PARTITION BY inst ORDER BY id) AS rn
FROM Beatles
Run Code Online (Sandbox Code Playgroud)

此查询返回

id |   inst   |   name  | rn
-----------------------------
 1 |  guitar  |  john   | 1
 2 | …

c# linq .net-4.0

36
推荐指数
3
解决办法
3万
查看次数

Linq - 左连接多个(OR)条件

我需要在条件为ORs而不是ANDs的多个条件下进行左连接.我已经找到了很多后者的样本,但我正在努力为我的场景找到正确的答案.

from a in tablea
join b in tableb on new { a.col1, a.col2 } equals new { b.col1, b.col2 }
group a by a into g
select new () { col1 = a.col1, col2 = a.col2, count = g.Count() }
Run Code Online (Sandbox Code Playgroud)

适用于所有条件必须匹配的连接.我需要让联接匹配on a.col1 = b.col1 OR a.col2 = b.col2.

我知道这一定很容易,但我在这个问题上一片空白!

编辑:

为了提供更多信息,查询的目的是获得包含"a"中所有字段的投影以及"b"中匹配记录的计数.我修改了上面的示例,试图说明我追求的是什么.当我使用上述方法运行时,Jon Skeet已经注意到我从a获得了所有记录的计数,而不是b中相关记录的计数.

基本左连接工作正常:

from a in tablea
from b in tableb
.Where( b => ( a.col1 == b.col1 || a.col2 == b.col2))
.DefaultIfEmpty() …
Run Code Online (Sandbox Code Playgroud)

c# linq-to-sql

27
推荐指数
2
解决办法
3万
查看次数

最佳实践:将LINQ查询结果转换为DataTable而不进行循环

将LINQ-Query结果转换为新结果的最佳做法是什么DataTable
我能找到比foreach每个结果项更好的解决方案吗?

编辑 AnonymousType

var rslt = from eisd in empsQuery
           join eng in getAllEmployees()
           on eisd.EMPLOYID.Trim() equals eng.EMPLOYID.Trim()
           select new
           {
               eisd.CompanyID,
               eisd.DIRECTID,
               eisd.EMPLOYID,
               eisd.INACTIVE,
               eisd.LEVEL,
               eng.EnglishName
           };
Run Code Online (Sandbox Code Playgroud)

编辑2: 我有例外:

除Contains()运算符外,本地序列不能用于查询运算符的LINQ to SQL实现.

当我尝试执行查询并在此处找到解决方案时IEnumerable.Except不会工作,所以我该怎么办?
需要linq的帮助

c# datatable linq-to-sql

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

参数异常"已添加具有相同密钥的项目"

我一直收到以下代码的错误:

Dictionary<string, string> rct3Features = new Dictionary<string, string>();
Dictionary<string, string> rct4Features = new Dictionary<string, string>();

foreach (string line in rct3Lines) 
{
    string[] items = line.Split(new String[] { " " }, 2, StringSplitOptions.None);

    rct3Features.Add(items[0], items[1]);

    ////To print out the dictionary (to see if it works)
    //foreach (KeyValuePair<string, string> item in rct3Features)
    //{
    //    Console.WriteLine(item.Key + " " + item.Value);
    //}

}
Run Code Online (Sandbox Code Playgroud)

错误抛出一句ArgumentException谚语,

"已经添加了具有相同键的项目."

我不确定几个谷歌搜索如何解决这个问题.

稍后在代码中我需要访问字典以获得比较函数:

Compare4To3(rct4Features, rct3Features);

public static void Compare4To3(Dictionary<string, string> dictionaryOne, Dictionary<string, string> dictionaryTwo)
{
    //foreach (string …
Run Code Online (Sandbox Code Playgroud)

c# dictionary exception

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

Int to string:无法从'method group'转换为'string'

我的表单上有一个listView.我希望在程序运行期间添加内容.

这是我使用的代码

public void FillList(string[] Name,int[] empty,int[] Population,int[] Max,int[] Check,int size)
{
    if (this.InvokeRequired)
    {
        this.Invoke((MethodInvoker)delegate
        {
            for (int i = 0; i < size; i++)
            {
                ListViewItem item = new ListViewItem(Name[i]);

                item.SubItems.Add(empty[i].ToString); //error
                item.SubItems.Add(Population[i].ToString); //error
                item.SubItems.Add(Max[i].ToString);   //error

                if (Check != 1)
                    item.SubItems.Add("No");
                else
                    item.SubItems.Add("Yes");
                listView1.Items.Add(item);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

参数必须是字符串,我试过.ToString,但我明白了:

参数'1':无法从'方法组'转换为'字符串'

c# string int listview winforms

21
推荐指数
2
解决办法
5万
查看次数

SQL ANY&ALL运算符

我已经开始使用sql并且已经听说过很多关于ANYALL运算符的内容.有人可以向我解释他们使用的查询类型以及它们的工作方式吗?

mysql sql

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

使用Linq除了不按我的想法工作

List1包含项目{ A, B }List2包含项目{ A, B, C }.

我需要的是{ C }在使用Except Linq扩展时返回.相反,我得到了返回{ A, B },如果我在表达式中翻转列表,结果是{ A, B, C }.

我是否误解了Except的观点?还有其他我没有看到使用的扩展吗?

到目前为止,我已经查看并尝试了很多不同的帖子,但没有成功.

var except = List1.Except(List2); //This is the line I have thus far
Run Code Online (Sandbox Code Playgroud)

编辑:是的我正在比较简单的对象.我从未使用过IEqualityComparer,了解它很有趣.

谢谢大家的帮助.问题不是实现比较器.链接的博客文章和下面的示例有帮助.

.net c# linq list except

16
推荐指数
3
解决办法
3万
查看次数

HttpContext.Current.Session不明确的行为布尔值

我有一个奇怪的行为试图获取存储在HttpContext.Current.Session的布尔属性的值.

该对象是一个布尔值.首先,我试图检查对象是否存在,如果存在,则使用它.

我试图在?:运算符中使用它,但它表现得很奇怪.这是我的Watch窗口: 来自VisualStudio的观察者

前提

  • "ExistingKey"键存在并且具有值为false(如果键不存在返回false).

结果

  1. 检查!=null它是否返回false(第一件事很奇怪).
  2. 当使用?:运算符时,除了条件为假之外,它还返回第一个表达式4(第二个奇怪的东西).

有人能解释一下这种行为吗?


注意:我不是要求绕过这种情况的替代方案.只是问为什么这样的工作原理.

c# asp.net session httpcontext conditional-operator

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