C# 合并列表,而不将新列表的填充内容放在新行上
我想将两个列表合并在一起,但我发现的方法将列表的填充添加到新行中,如下所示C# 合并列表,而不将新列表的填充内容放在新行上,c#,list,gtfs,C#,List,Gtfs,我想将两个列表合并在一起,但我发现的方法将列表的填充添加到新行中,如下所示 parts on list one, parts on list one, parts on list one, parts on list two, parts on list two, parts on list two, parts on list one,parts on list two parts on list one,parts on list two parts on list one,parts o
parts on list one,
parts on list one,
parts on list one,
parts on list two,
parts on list two,
parts on list two,
parts on list one,parts on list two
parts on list one,parts on list two
parts on list one,parts on list two
但是,我希望它看起来像这样
parts on list one,
parts on list one,
parts on list one,
parts on list two,
parts on list two,
parts on list two,
parts on list one,parts on list two
parts on list one,parts on list two
parts on list one,parts on list two
这就是我必须合并它们的原因(到目前为止列表本身)
static void Main(字符串[]args)
{
迭代器输出(@“C:\Users\peepeapoo\gtfs\routes.txt”,“C:\Users\peepeapoo\gtfs\calendar.txt”);
//log.ForEach(Console.WriteLine);
}
公共静态列表日志=新列表();
公共静态列表日期std=new List();
public static List datesEnd=new List();
公共静态void迭代器输出(字符串rtname、字符串std)
{
foreach(File.ReadAllLines(rtname))中的var路由//迭代文件行
{
var elements=route.Split(',');//将当前行拆分为元素
添加(元素[0]+”、“+元素[2]+”、“+”20214262099231)”;//将第一个和第三个元素添加到日志
}
foreach(File.ReadAllLines(std)中的var路由)//迭代文件行
{
//var elements=route.Split(',');//将当前行拆分为元素
//log.Add(“,”+元素[8]);//将第一个和第三个元素添加到日志
}
//datesStd.ForEach(Console.WriteLine);
//datesEnd.ForEach(Console.WriteLine);
log.AddRange(datesStd);
log.ForEach(Console.WriteLine);
}
我该怎么做?要加入文件,可以使用zip:
IEnumerable<string> fileOne = File.ReadLines("one.txt");
IEnumerable<string> fileTwo = File.ReadLines("two.txt");
// For each line in fileOne, take a line from fileTwo
// call these lines l1 and l2 respectively, and produce
// a merged result.
IEnumerable<string> joined = fileOne.Zip(fileTwo, (l1, l2) => $"{l1}{l2}");
// write the result to a file
File.WriteAllLines("out.txt", joined);
// or materialize it to a list
List<string> lines = joined.ToList();
和另一个文件:
D
E
F
那么结果就是
AD
BE
CF
。请注意,我在这里用我自己的假版本替换了
File.ReadLines
,因为我使用的网站不支持读取文件。如果你像这样使用foreach
,你应该使用File.ReadLines
而不是File.ReadAllLines
File.ReadLines
将根据需要加载文件的下一行,而File.ReadAllLines
将在循环之前将整个内容读入内存。@Llama速度快多了,谢谢你的提示。另外,@Llama(第一,第二)
在你提供的资源中代表什么?在第一次迭代中,first=1;second=“one”
,在第二个first=2;second=“two”
,依此类推(基于该答案中的样本数据)。