1

I try to understand how to write the code below using python list comprehension method. In the code I need to concatenate the string with itself times next element in the list assuming that the order is always as [string, int, string int]. Is there a way to rewrite the below code using list comprehension method? Thank you.

def array_translate(arr):
    i = 0
    mystring = ''
    while i < len(arr):
        pet = arr[i]
        if i + 1 < len(arr):
            num = arr[i+1]
        arr[i] = pet*num
        i += 2

    for i in arr:
        if not str(i).isdigit():
            mystring += "".join(i)
        else:
            continue
    return mystring

print (array_translate(["Cat", 2, "Dog", 3, "Mouse", 1])) # => "CatCatDogDogDogMouse"
blhsing
  • 91,368
  • 6
  • 71
  • 106
xolsno
  • 17
  • 4

4 Answers4

1

To pair items in a list, you can create an iterator from the list and zip the iterator with itself, so that you can iterate over the pairs of names and counts in a generator expression or list comprehension for str.join to join them into a string after repeating the strings by the respective counts:

def array_translate(arr):
    i = iter(arr)
    return ''.join(name * count for name, count in zip(i, i))

so that:

array_translate(["Cat", 2, "Dog", 3, "Mouse", 1])

returns:

'CatCatDogDogDogMouse'
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • this is really cool solution! Thank you so very much. Could you share any resource where I can learn about iter and zip in python? – xolsno Oct 30 '19 at 17:54
  • You're welcome. You can learn about `iter` and `zip` in the official [documentation](https://docs.python.org/3/library/functions.html). For this particular usage, however, I learned it from StackOverflow from answers such as this: https://stackoverflow.com/a/40565199/6890912 – blhsing Oct 30 '19 at 17:59
0

If you can format it as a list of tuples, like this: [("Cat", 2), ("Dog", 3), ("Mouse", 1)] then the following can work.

''.join([a[0] * a[1] for a in array])

What it does is create a list strings containing an animal x times. Then ''.join() concats them together.

Groger
  • 532
  • 3
  • 15
0

Yes, it's possible. If you want to iterate over pairs of values, use zip:

def array_translate(arr):
    pets = arr[0::2]
    nums = arr[1::2]
    return ''.join([ pet * num for pet, num in zip(pets, nums) ])

print(array_translate(["Cat", 2, "Dog", 3, "Mouse", 1]))

This works by first creating two new lists, pets containing just the strings, and nums containing just the numbers. The lists are created using slice notation: arr[0::2] is every 2nd element starting from index 0, and arr[1::2] is every 2nd element starting from index 1.

The ''.join method is used to join the resulting strings together.

kaya3
  • 47,440
  • 4
  • 68
  • 97
0

Given arr = ["Cat", 2, "Dog", 3, "Mouse", 1]

Here's a one liner solution without zip or iter:

print(''.join([arr[i]*arr[i+1] for i in range(0, len(arr), 2]))

Outputs:

CatCatDogDogDogMouse
Rithin Chalumuri
  • 1,739
  • 7
  • 19