1

The goal is: using the list generator (and not using the code outside of it), convert the string according to the following logic: for each character of the string, create a string in the final list containing copies of the character in an amount equal to the character number calculated from the end of the source string. For instance: 'abcd' - > ['aaaa', 'bbb', 'cc', 'd']. So, first i have tried to solve this problem without the list generator:

s='abcd'
a=' '.join(s)
b=a.split()
for i in range(len(b)):
 b[i]*=len(b)-i
print(b)

Then i tried list generator and find the problem:

a=[input() for i in range(len(input()))]
print(a)

1)How to enclose input() in a variable and how to correctly convert a string to a list in a single line( i mean without using cod outside of generator)?2) How to write the body of the "for" loop, if the general form of the generator is as follows: [expr for targets in iterable]?

asked Jun 20, 2021 at 11:20
6
  • 1
    Note that there is no such thing as a "list generator" in Python. [expr for targets in iterable] is a list comprehension, and (expr for targets in iterable) is a generator expression (the parentheses are optional in some cases). Commented Jun 20, 2021 at 11:24
  • 1
    @MisterMiyagi Thank you for informing. So, but i correctly use the expression for generator. Do you want to add some other comments about this expression "not using the code outside of it"? Commented Jun 20, 2021 at 11:32
  • Sorry, I don't understand what "and not using the code outside of it" means exactly. You should store the input outside of the comprehension if you want to refer to it in multiple places. There is an idiomatic way to achieve the desired result without code outside of the comprehension, but it would not match the requirements (e.g. "enclose input() in a variable" implies there must be an assignment). Commented Jun 20, 2021 at 11:47
  • 2
    Would a = [s * i for i,s in enumerate(reversed(input()), start=1)][::-1] match your requirements? Commented Jun 20, 2021 at 11:51
  • [char*(len(s)-i) for i, char in enumerate(s)] Commented Jun 20, 2021 at 12:10

1 Answer 1

2

If I understand your requirement correctly the following one liner should satisfy your constraints-

print([character * (idx + 1) for idx, character in enumerate(input()[::-1])][::-1])

Its very un-idiomatic and you should consider a more readable solution - unless of course this is the purpose of the exercise.

answered Jun 20, 2021 at 11:53
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.