Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I will take a random list from user by positive number(already done)
Then, I would like to double my numbers in a list.
for example, ['110, '212', '39839'] should be doubled to
['111100', '221122','3399883399'] so, I first converted to str()then use
lambda by multiply *2. But, I need to do this in every index so how can I do that?
My while statement does not work.
I am beginner so any helps will be appreciated. Thanks.
s= ["110", "212", "39839"]

i = 0
while i < len(s)-1:
    s= ''.join(map(lambda x:x*2, s[i]))
    if s[i] == s[i+1]:
            result += s[i]
            i += 1
            i += 1
return s


What I have tried:

so, I first converted to str()then use
lambda by multiply *2. But, I need to do this in every index so how can I do that?
My while statement does not work.
I am beginner so any helps will be appreciated. Thanks.
Posted
Updated 30-Oct-20 0:09am

You are not doubling numbers, you are modifying strings of characters. The fact that they are digits is irrelevant. So all you need is something simple such as:
Python
s= ["110", "212", "39839", "ABC" ]
index = 0
while index < len(s):   # iterate the entries in s
    pnew = ''           # create a new empty string
    for c in s[index]:  # for each character in the original
        pnew += c + c   # append two of them to the new string
    s[index] = pnew     # replace the original with the new one
    index += 1          # process the next item in the list
print(s)                # print the final result
 
Share this answer
 
Comments
CPallini 30-Oct-20 6:26am    
5.
Richard MacCutchan 30-Oct-20 6:36am    
Thanks.
M_N01 30-Oct-20 8:19am    
Hi, Thanks for the help with an explanation. really helpful
Richard MacCutchan 30-Oct-20 8:32am    
You are welcome. I also suggest you go to The Python Tutorial — Python 3.7.9 documentation[^], as there is lots of good information and samples to help you learn.
Um ... that's odd code!

Let's start with the loop itself. Suppose you have three pages in a book you need to read and for some reason they have been numbered as Page 0, Page 1, and Page 2. The number of the page acts as an "index" into the book: you want "Page n" you open the book and count pages from 0 to n until you reach it.

So to read all the pages, you need to look at each book[index] where index is zero for the first page, one for the second, and two for the last one.
Your code doesn't do that:
Python
while i < len(s)-1:
It starts with zero, that's fine - but the limit test is "less than the number of items minus one". Since there are three items, the len of the collection s will be three, so three minus one will be two, so the loop will never process the last item.
Change it to
Python
while i < len(s):
Now it will look at values of i between 0 (the start) and 2 (the end) inclusive.

But that's not all!
Why are you incrementing i twice each time round the loop?
Python
result += s[i]
i += 1
i += 1
That means that the first time round the loop, i is zero, the second time it is two, and there are no more values to look at after that!
Get rid of one increment, so it looks at each item in turn!

And do yourself a favour: go here: pdb — The Python Debugger — Python 3.9.0 documentation[^] and read up on the python debugger - it will let you look at exactly what your code is doing while it runs, and that can make your life a whole load easier with problems like this!
 
Share this answer
 
Comments
M_N01 30-Oct-20 8:21am    
Thanks for the solution!yes I will look at this. I still have a long way to go.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900