join string lists - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: join string lists (/thread-27572.html) |
join string lists - redminote4dd - Jun-11-2020 Hello! I have a lists which I want to join with the second item of itself for exmaple: 1=[A, B, C, D] I want python to output: 1=[AB, BC, CD] how can I manage that? RE: join string lists - menator01 - Jun-11-2020 lst = ['a','b','c','d'] joinlst = ''.join(lst) newlist = [joinlst[:2],joinlst[1:3], joinlst[2:4]] print(newlist)
RE: join string lists - DPaul - Jun-11-2020 If you start with: 1=[A, B, C, D] you are going to have a hard time making this work. - You need a legal name for your list - If A,B,C,... are strings , they seem to have no value (or is it 'A'...). We need a better starting point. Paul RE: join string lists - redminote4dd - Jun-11-2020 (Jun-11-2020, 02:06 PM)DPaul Wrote: If you start with: ok, so lets use this as the staring case: anylist=['A', 'B', 'C', 'D'] i want to output: newlist=['AB', 'BC', 'CD'] RE: join string lists - menator01 - Jun-11-2020 Another way lst = ['a','b','c','d'] joinlst = ''.join(lst) newlist = [] for i in range(len(joinlst)): newlist.append(joinlst[i:i+2]) newlist.pop() print(newlist)
RE: join string lists - GOTO10 - Jun-11-2020 I'm curious to hear from some of the more experienced coders on the forum what they think is the best way to do this. I understand that it's generally considered desirable to avoid using range(len(sequence)) , but I'm having a hard time doing so without adding lines of code or checks I wouldn't need otherwise.Here is what I came up with: l = ['a', 'b', 'c', 'd'] newl = [] for i in range(len(l) -1): newl.append(''.join(l[i] + l[i+1])) print(newl) RE: join string lists - perfringo - Jun-11-2020 There is built-in module textwrap - Text wrapping and filling which have function wrap >>> import textwrap >>> lst = ['A', 'B', 'C', 'D'] >>> textwrap.wrap(''.join(lst), 2) ['AB', 'CD'] >>> lst = ['A', 'B', 'C', 'D', 'E'] >>> textwrap.wrap(''.join(lst), 2) ['AB', 'CD', 'E'] RE: join string lists - redminote4dd - Jun-11-2020 (Jun-11-2020, 02:33 PM)GOTO10 Wrote: I'm curious to hear from some of the more experienced coders on the forum what they think is the best way to do this. I understand that it's generally considered desirable to avoid using that worked perfectly for me. thank you RE: join string lists - pyzyx3qwerty - Jun-11-2020 Not the best approach, but if you wanted, you could do list1 = ["a","b","c","d"] str1 = list1[0] + list1[1] str2 = list1[1] + list1[2] str3 = list1[2] + list1[3] list2 = [str1,str2,str3] print(list2)And output : You could also not define list2 and print it, like:print([str1,str2,str3]) RE: join string lists - Yoriz - Jun-11-2020 Assuming that if you only pass a list with one item it returns an empty list. from string import ascii_uppercase def adjacent_list(iterabe): stored_character = None for character in iterabe: if stored_character: yield ''.join((stored_character, character)) stored_character = character for index in range(1, 27): letters = (ascii_uppercase[:index]) print(list(adjacent_list(letters)))
|