从列表中获取重复项<t>

时间:2016-05-26 09:52:20

标签: c# linq

我有一个下面给出的模型列表

import sys
import json
from datetime import datetime
import itertools
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

if __name__ == '__main__':
    colours = itertools.cycle(['r', 'b', 'g', 'm', 'c', 'y', 'k'])

    # A bit of faff for clean printing with globbed arguments:
    filenames = [name.split('.')[0] for name in sys.argv[1:]]

    fig = plt.figure()

    axes = plt.axes()
    axes.set_ylabel('Events')

    for file in filenames:
        data = json.loads(open(file + '.json').read())['results']['data']

        # Unzip to a tuple of lists ([x], [y])
        x, y = zip(*[(datetime.utcfromtimestamp(int(d['t']/1000)), d['x']) for d in data])
        axes.plot(x, y, colours.next(), label=file)

    fig.autofmt_xdate()
    plt.savefig('plot_{0}.png'.format('_'.join(filenames)))

此列表中的示例值为:

string cellValue;
String cellAddress;

我想要的是创建所有重复值的列表 比如处理后应该有两个从这个数据生成的列表

cellValue ="txt" ; CellAddres="A1";
cellValue ="txt" ; CellAddres="A2";
cellValue ="txt" ; CellAddres="A3";
cellValue ="txt" ; CellAddres="B1";
cellValue ="num" ; CellAddres="B2";
cellValue ="num" ; CellAddres="B3";
cellValue ="num1" ; CellAddres="B4";
cellValue ="txt1" ; CellAddres="C3";
1)  cellValue ="txt" ; CellAddres="A1";
    cellValue ="txt" ; CellAddres="A2";
    cellValue ="txt" ; CellAddres="A3";
    cellValue ="txt" ; CellAddres="B1";

2 个答案:

答案 0 :(得分:2)

一般情况下,您尝试做的是分组原始列表cellValue。这将为您提供IGrouping<string,YourObject>

var result = list.GroupBy(x => x.cellValue);

这将为您提供4个结果(txtnumtxt1&amp; num1为关键字)。每个分组都是IEnumerable<YourObject>,因此您可以枚举每个组,就好像它是自己的列表一样。

如果您想要排除那些只有一个孩子的群组,您可以在Count()内使用Where

var result = list.GroupBy(x => x.cellValue)
                .Where(g => g.Count() > 1);

现在只会将您指定的2个组作为所需输出。

实例:http://rextester.com/RSN67321

答案 1 :(得分:1)

似乎你想要GroupBy(),像这样:

List<MyItem> source = ...

var result = source
  .GroupBy(item => item.cellValue) 
  .Where(chunk => chunk.Count() > 1) // duplicates 
  .Select(chunk => String.Join("; ", chunk));