Posts: 5
Threads: 2
Joined: Jun 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?
Posts: 1,144
Threads: 114
Joined: Sep 2019
lst = ['a','b','c','d']
joinlst = ''.join(lst)
newlist = [joinlst[:2],joinlst[1:3], joinlst[2:4]]
print(newlist) Output: ['ab', 'bc', 'cd']
Posts: 741
Threads: 122
Joined: Dec 2017
Jun-11-2020, 02:06 PM
(This post was last modified: Jun-11-2020, 02:08 PM by DPaul.)
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Posts: 5
Threads: 2
Joined: Jun 2020
(Jun-11-2020, 02:06 PM)DPaul Wrote: 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
ok, so lets use this as the staring case:
anylist=['A', 'B', 'C', 'D']
i want to output:
newlist=['AB', 'BC', 'CD']
Posts: 1,144
Threads: 114
Joined: Sep 2019
Jun-11-2020, 02:17 PM
(This post was last modified: Jun-11-2020, 02:17 PM by menator01.)
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) Output: ['ab', 'bc', 'cd']
Posts: 150
Threads: 3
Joined: Apr 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)
Posts: 1,950
Threads: 8
Joined: Jun 2018
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']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 5
Threads: 2
Joined: Jun 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 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)
that worked perfectly for me. thank you
Posts: 353
Threads: 13
Joined: Mar 2020
Jun-11-2020, 04:41 PM
(This post was last modified: Jun-11-2020, 04:41 PM by pyzyx3qwerty.)
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 :
Output: ['ab', 'bc', 'cd']
You could also not define list2 and print it, like:
print([str1,str2,str3])
Posts: 2,168
Threads: 35
Joined: Sep 2016
Jun-11-2020, 07:04 PM
(This post was last modified: Jun-11-2020, 07:11 PM by Yoriz.)
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))) Output: []
['AB']
['AB', 'BC']
['AB', 'BC', 'CD']
['AB', 'BC', 'CD', 'DE']
['AB', 'BC', 'CD', 'DE', 'EF']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST', 'TU']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST', 'TU', 'UV']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST', 'TU', 'UV', 'VW']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST', 'TU', 'UV', 'VW', 'WX']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST', 'TU', 'UV', 'VW', 'WX', 'XY']
['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST', 'TU', 'UV', 'VW', 'WX', 'XY', 'YZ']
|