Reputation: 87
So I need to create a function that iterates over a list of integers and print the characters of a string with those integers as indices. Something like this:
printString('123456789',[1,7,5,3])
2
8
6
4
i have this:
def printString(s,list):
i=0
resultString=str()
for i in range(len(list)):
resultString= s[list[i]]
i=i+1
print (resultString)
but it prints only the first character of the string, i guess there is a problem with the for-loop, i just can't find out what it is.
Any help will be useful! thank you
Upvotes: 2
Views: 111
Reputation:
You just need to put the print statement inside the for loop. It is only printing the last element because it gets overridden each time. You can also simply it as well
def printString(s,list):
resultString=str()
for i in range(len(list)):
resultString= s[list[i]]
print (resultString)
Upvotes: 0
Reputation: 17500
Given your args as a test case:
#!/usr/bin/python
def printString(strng, indexes):
return ''.join((c for i,c in enumerate(strng) if i in indexes))
This uses enumerate()
and a generator.
Upvotes: 0
Reputation: 76194
Move print(resultString)
so that it's inside the loop.
def printString(s,list):
i=0
resultString=str()
for i in range(len(list)):
resultString= s[list[i]]
i=i+1
print (resultString)
Additional but non-essential advice:
You don't need i=0
or i=i+1
, since the for
loop already does the work of creating and incrementing the value for you.
def printString(s,list):
resultString=str()
for i in range(len(list)):
resultString= s[list[i]]
print (resultString)
You don't need resultString=str()
because Python is perfectly happy to create a new variable in the middle of a function without any hints as to what its type should be.
def printString(s,list):
for i in range(len(list)):
resultString= s[list[i]]
print (resultString)
You don't necessarily need resultString
at all since you're only using its value once.
def printString(s,list):
for i in range(len(list)):
print (s[list[i]])
It's preferable to iterate over the elements of a list rather than its indices, if you don't have a specific need for the index.
def printString(s,list):
for idx in list:
print (s[idx])
Try not to use variable names that are identical to the names of built-in types.
def printString(s,seq):
for idx in seq:
print (s[idx])
Upvotes: 1
Reputation: 311338
There are several problems here. First, the print
statement should be indented inside the loop. Second, even though it's innocuous here, you shouldn't manipulate i
yourself, but leave the for
loop to do it for you. And last, list
is a horrible name for a variable, as it hides python's built-in list. Bring it all together, and you'd get something like this:
def printString(s, lst):
for i in range(len(lst)):
resultString = s[lst[i]]
print (resultString)
Upvotes: 0
Reputation: 15923
It's because you are not appending all the result into the string, this will give you answer: 2864
def printString(s,list):
i=0
resultString=str()
for i in range(len(list)):
resultString+=s[list[i]]
i=i+1
print (resultString)
printString('123456789',[1,7,5,3])
Upvotes: 0