Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, connecting vertical lists

I have this for example this characters:

.....U......
L...L.######
.S....#.....
....L.......

which i stored in a list

chars = ['.....U......', 'L...L.######', '.S....#.....', '....L.......']

I used this to store it in the chars:

for x in range(0, N):
    g = input()
    chars.append(g)

Now the problem is i want to turn all of the dots between letters L into # but vertically, so like this:

.....U......
L...L.######
.S..#.#.....
....L.......

I have been trying for a few hours now and i can't think of anything. Help much appreciated.

EDIT: I used this to connect them horizontally. And it works.

while y != N:
    modchars0 = list(chars[y])

    if modchars0.count('L') == 0:
        y += 1

    else:
        for k in range(0, M):
            if 'L' in modchars0[k]:
                start = k + 1
                break

        for l in range(M-1, 0, -1):
            if 'L' in modchars0[l]:
                end = l
                break

        for h in range(start, end):
            if 'L' in modchars0[h]:
                pass
            else:
                modchars0[h] = '#'

        modchars1 = modchars1.join(modchars0)
        chars[y] = modchars1
        y += 1
like image 942
duk Avatar asked Jan 19 '15 00:01

duk


People also ask

How to display each element vertically of a list in Python?

Write a Python program to display vertically each element of a given list, list of lists. Sample Solution: Python Code: text = ["a", "b", "c", "d","e", "f"] print("Original list:") print( text) print("nDisplay each element vertically of the said list:") for i in text: print( i) nums = print("Original list:") ...

How to implement a vertical line in Python programs?

The ‘colors’ and ‘label’ parameters sets the different colors and heights of vertical lines. The ‘label’ height is full or partial height as desired. We have discussed various ways of implementing a vertical line in python programs. We first start by importing matplotlib library to use the matplotlib vertical line function.

How to create a list in Python?

Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage. Lists are created using square brackets: Example. Create a List: thislist = ["apple", "banana", "cherry"] print(thislist) Try it Yourself ».

How to combine lists in Python?

The easiest way to combine Python lists is to use either list unpacking or the simple + operator. Let’s take a look at using the + operator first, since it’s syntactically much simpler and easier to understand. Let’s see how we can combine two lists: We can see here that when we print out third list that it combines the first and second.


2 Answers

As @U2EF1 mentioned in comments you can take the transpose of the list using zip(*chars) and then using regex you can convert dots between 'L' to '#'. And then in the end zip(*) the new items again to get desired output:

>>> import re                                                             
>>> r = re.compile(r'(?<=L).*(?=L)')                     
>>> def rep(m):                                           
    return m.group().replace('.', '#')
... 
>>> zipped = (r.sub(rep, ''.join(x)) for x in zip(*chars))
>>> for x in zip(*zipped):
    print ''.join(x)
...     
.....U......
L...L.######
.S..#.#.....
....L.......
like image 154
Ashwini Chaudhary Avatar answered Oct 13 '22 00:10

Ashwini Chaudhary


Late Answer

This one probably doesn't deserve best answer, but is an alternative nevertheless.

First define a function that will rotate the list from horizontal to vertical:

def invert(chars):
    inverted = []
    for i in range(len(chars[0])):
        newStr=""
        for j in range(len(chars)):
            newStr+=chars[j][i]
        inverted.append(newStr)
    return inverted

Then you may use it in this manner:

def main():
    chars = ['.....U......', 'L...L.######', '.S....#.....', '....L.......']
    invChars = invert(chars)

    for i in range(len(invChars)):
        invChars[i] = re.sub(r'(?<=L)(\..*?)L', lambda k: k.group().replace('.','#'), invChars[i])

    chars = invert(invChars)
like image 36
buydadip Avatar answered Oct 12 '22 23:10

buydadip