-1

I am just facing one tricky challenge to make a list combination in python 3.

I have one list that needs to used for combination. (given list)

['01101001', '01110110', '01100101', '01101011']

I want to get the list combined with each value like below. (result list)

['00001111', '11110100', '10010110', '01011011']

The first item in the result list is made by the combination of the first 4 strings and the second 4 strings of each item in turn in the given list. So 0000 are the first 4 strings of each item and 1111 are the second 4 strings of each item in the given list. Thus the first item of the result list is 00001111.

What is the optimized code written by Python 3? Thank you in advance!

Martin V
  • 82
  • 5
  • In addition to seemingly trying to use Stack Overflow as a code writing service, your question is unclear. I don't see how your second list is derived from the first. You say something about combining the first 4 strings and the second 4 strings, but your list only has 4 strings so "second 4 strings" has no obvious meaning. You seem to be talking about characters in the strings rather than strings themseleves, but then where did `'0000'` and `'1111` come from? They seem unrelated to your input list. I couldn't provide free code for you even if I wanted to since the problem is so unclear. – John Coleman May 29 '20 at 10:58
  • I meant the second strings of each item and the list has 4 items, so I can get 4 strings from each item like that. – Martin V May 29 '20 at 11:01
  • I explained pretty much detail, the first string of the first item is 0 and the first string of the second item is 0, the first string of the third item is 0, the first string of the fourth item is 0 in the given list. Thus if they're combined it's `0000` And also the second string of the first item is 1 and the second string of the second item is 1, the second string of the third item is 1, the second string of the fourth item is 1 in the given list. Thus if they're combined it's `1111` – Martin V May 29 '20 at 11:05
  • I am really a newbie in python. Now started to learn. I am not sure why @komatiraju mentioned like that. – Martin V May 29 '20 at 11:07
  • 1
    Stack Overflow is designed to help programmers solve their problems. Your question is "What is the optimized code written by Python 3?" -- which seems like you want someone to solve your problem for you rather than obtaining help in solving it. Describing your attempts in solving the problem would dispel that impression. Your question would be clearer if you talked about *characters* in the strings rather than *strings* in the strings. – John Coleman May 29 '20 at 11:14

3 Answers3

3

Idiomatic Python code makes heavy use of iterators. What you are trying to do is a form of parallel iteration over your strings, which is what zip can do.

If strings = ['01101001', '01110110', '01100101', '01101011']

Then [''.join(chars) for chars in zip(*strings)] evaluates to

['0000', '1111', '1111', '0100', '1001', '0110', '0101', '1011']

That is almost what you want. It would be nice if you could iterate over the list by pairs. You can do so in several ways, including by using this nice solution due to @mic_e:

#the following pairwise iterator is due to mic_e
#from https://stackoverflow.com/a/30426000/4996248

def pairwise(it):
    it = iter(it)
    while True:
        try:
            yield next(it), next(it)
        except StopIteration:
            # no more elements in the iterator
            return

Then simply:

new_strings = [a+b for a,b in pairwise(''.join(chars) for chars in zip(*strings))]

which is ['00001111', '11110100', '10010110', '01011011'].

John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • there is an even easier way in this case: `t = list(zip(*k))` to get the characters as tuples in order and then `s = [''.join(a+b) for a,b in zip(t[::2],t[1::2])]` to join them together. – Gábor Fekete May 29 '20 at 20:34
2

Here is the solution.

givenList = ['01101001', '01110110', '01100101', '01101011']
unitList = list()
result = list()

for i in range(8):
  for j in givenList:
    unitList.append(j[i])
  if i % 2 == 1:
    result.append(''.join(unitList))
    unitList = []

print(result)

This would be working for only the given list contains static 8 digits. You can update the 8 with len(givenList[0]).

Kevin Lee
  • 332
  • 2
  • 13
0

Simple Solution

lt = ['01101001', '01110110', '01100101', '01101011']

def list_combination(lt,new=[],num=""):
  for i in range(len(lt[0])):
    if len(num)==8:
      num = ""
    for j in lt:
      num += j[i]
    if len(num)==len(lt[0]):
      new.append(num)
  return new
print(list_combination(lt))
#['00001111', '11110100', '10010110', '01011011']
Krishna Singhal
  • 631
  • 6
  • 9