简体   繁体   中英

Select first word of list of strings and concat last char of the rest of items on list in linq

Having a python code like:

reads = ['ACCGA', 'CCGAA', 'CGAAG', 'GAAGC', 'AAGCT']
k=len(reads[0])
rest = ""
for r in reads[1:]:
    rest += (r[k-1:])
print reads[0] + rest

The code gets first item on list ACCGA then concats the last char of each item on the rest of the list like

the rest = ['CCGAA', 'CGAAG', 'GAAGC', 'AAGCT']

and take 'CCGA A ', 'CGAA G ', 'GAAG C ', 'AAGC T '

so result ACCGA + A + G + C + T

result = ACCGAAGCT

What would be the way to do it on using linq?

I understand that We can select first item doing

string result = myList.Select(arr => arr.FirstOrDefault());

then to concat the rest

var rest = from p in myList.Items
                 where ....  
                select p;

Do not know how to complete it

new []{"ACCGA", "CCGAA", "CGAAG", "GAAGC", "AAGCT"}.Aggregate((a,b)=>a+b.Last())

For very large arrays, this would perform better:

var arr=new []{"ACCGA", "CCGAA", "CGAAG", "GAAGC", "AAGCT"};
var builder=new StringBuilder(arr.First());
var res=arr.Skip(1).Aggregate(builder,(a,b)=>a.Append(b.Last())).ToString();

For Lists or Arrays that are very large (Any IEnumerable that implements a native Count/Length), this would perform slightly better yet, preallocating the size of the StringBuilder, feel free to remove the -1 to waste an extra byte of memory in exchange for one less decrement:

var arr=new []{"ACCGA", "CCGAA", "CGAAG", "GAAGC", "AAGCT"};
var builder=new StringBuilder(arr.First(),arr.First().Length+arr.Length-1);
var res=arr.Skip(1).Aggregate(builder,(a,b)=>a.Append(b.Last())).ToString();

Here's a faster version, if you really need the extra speed:

var arr=new []{"ACCGA", "CCGAA", "CGAAG", "GAAGC", "AAGCT"};
var builder=new StringBuilder(arr.First(),arr.First().Length+arr.Length-1);
var res=arr.Skip(1).Aggregate(builder,(a,b)=>a.Append(b[b.Length-1])).ToString();

Even faster:

var res=arr[0].Substring(0,arr[0].Length-1)+String.Join("",arr.Select(b=>b[b.Length-1]));

And even more faster:

var arr=Enumerable.Range(0,1000000).Select(x=>"ABCDEFGHIJKLMNOPQ").ToArray();
var len=arr.Length;
var builder=new StringBuilder(arr[0],arr[0].Length+len-1);
for(var x=1;x<len;x++)
{
  builder.Append(arr[x][arr[x].Length-1]);
}
var res=builder.ToString();

Why do you need linq for it?

Basically all you need is one line of python code:

result = reads[0] + ''.join([item[-1] for item in reads[1:]])

Does this help?

string[] reads = new string[] { "ACCGA", "CCGAA", "CGAAG", "GAAGC", "AAGCT" };
string firstword = reads[0];
reads.Where(r => !r.Equals(firstword)).ToList().ForEach(s => firstword = firstword + s.Last());

Not sure why you want to do it, but this should do the trick using overload of string ctor that excepts char[] :

string[] myList = {"ACCGA", "CCGAA", "CGAAG", "GAAGC", "AAGCT"};

string result = myList[0] + new String(myList.Where(s=> s != myList[0]).Select(s2=> s2.Last()).ToArray());

Output : ACCGAAGCT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM