This question already has answers here:
'str' object does not support item assignment [duplicate]
(10 answers)
Closed 8 years ago.
So I have to create a function who take a list like this:
[d,o,g] --> each caracter has a position
and the return is the reverse word...
So far I have this:
def invertido(x):
largo= len(x)
for i in range (0, largo/2):
x[i] = x[largo -i]
print x
I have the following error: TypeError: 'str' object does not support item assignment
You can do this directly using the indexing syntax in Python. Try:
word_inverted = word[-1::-1]
This syntax means "start at the last letter in word (index -1), move backwards one letter at a time (the -1 at the end), and move through all of the letters in the word (the :: in the middle)"
In general, you can index an array (a string is just an array of characters) with the syntax array[first:last:step], where first is the first item that you want, last is the first item that you don't want (i.e. the last item you get is the item right before last) and step is how far you want to move for each successive item.
You can also use some shortcuts to just grab all letters from the beginning of the word to last with array[:last:step], all letters from first to the end with array[first::step], and all letters at some interval step with array[::step]. Lastly, if you enter a negative value for step, then you step backwards through the array.
Related
This question already has answers here:
Why does reversing a list using slice notation with 0 as "stop" not return the entire list?
(9 answers)
How do I reverse a string in Python?
(19 answers)
Closed 10 days ago.
I am trying to reverse a string in python but i cannot include the first letter.
I tried this code:
a = "Helloworld"
print(a[3:0:-1])
but it doesn't work.
I also tried:
a = "Helloworld"
print(a[3:-1:-1])
It displays nothing when i try this.
The code you tried doesn't work because the slice a[3:0:-1] starts at index 3 and goes all the way to index 0 (in reverse), but it includes index 0, which is the first letter of the string.
The slice a[3:-1:-1] starts at index 3 and goes to the index before the last one (-1), but in reverse. This would give an empty string because the step value of -1 goes in the opposite direction of the start and end indices.
To reverse the string excluding the first letter, you can slice it like this:
a = "Helloworld"
print(a[1:][::-1])
This question already has answers here:
Understanding slicing
(38 answers)
Closed 4 years ago.
I don't know what 'Count : Count' does in my code. Below is the function that this is used in on line 4
I tried printing it but it gave me an error. CommandList is a string variable as well as Command.
def GetPositionOfCommand(CommandList, Command):
Position = Count = 0
while Count <= len(CommandList) - len(Command):
if CommandList[Count:Count + len(Command)] == Command:
return Position
elif CommandList[Count] == ",":
Position += 1
Count += 1
return Position
Position = GetPositionOfCommand(Items[IndexOfItem].Commands, "get")
Your question is off, since Count: Count does nothing in the code you show. Rather, what acts is Count:Count + len(Command). That would be better written as Count: (Count+len(Command)).
Both CommandList and Command are strings or lists or similar data types (I'll say strings hereafter), while Count is an integer. In particular, Count is an index into CommandList.
The expression CommandList[Count:Count + len(Command)] is a slice of CommandList. In other words, that expression is a sub-string of the string CommandList. That sub-string begins at the index position held in Count and stops just before the index position Count + len(Command). That sub-string has the same length that the string Command has.
Therefore the entire line
if CommandList[Count:Count + len(Command)] == Command:
checks if the sub-string pointed to by variable Count is equal to the string Command. If the sub-string and the string are equal, the next line executes, namely the return statement.
Is that clear? Read up more on Python's slices--the link I gave you is a good start. Slices are just one reason Python handles lists and strings so much better than most other languages. The code is written a little confusingly, so it looks like Count:Count is an expression in itself. The code should have used different spacing and perhaps parentheses to show that the inner expression is Count + len(Command) and the colon is used after that. Order of operations shows itself again!
This question already has answers here:
Find the index of the second occurrence of a string inside a list
(3 answers)
Find the index of the n'th item in a list
(11 answers)
Closed 7 years ago.
If I'm working with a list containing duplicates and I want to know the index of a given occurrence of an element but I don't know how many occurrences of that element are in the list, how do I avoid calling the wrong occurrence?
Thanks
I don't know that a single builtin does this thing alone, but you could fairly easily write it, for instance:
def index_second_occurence(alist, athing):
if alist.count(athing) > 1:
first = alist.index(athing)
second = alist[first + 1::].index(athing)
return second + first + 1
else:
return - 1
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 7 years ago.
I am working of Project Euler problem 2. I need to find the sum of all even Fibonacci numbers up to four million. I have a function that generates all possible Fibonacci numbers up to upperBound which is input by the user. I am passing this list of fibs to another function in order to eliminate the odd values. I am trying to iterate through the list and delete the element if it is odd. It is returning an error saying :
in evenFibs
del list_of_fibs[value]
IndexError: list assignment index out of range
Here is my code for evenFibs() :
def evenFibs(upperBound):
list_of_fibs = getFibs(upperBound)
for value in list_of_fibs:
if value % 2 != 0:
del list_of_fibs[value]
return list_of_fibs
I am not sure why this error is occuring.
Take a note that you shouldn't change array while iterating it, also to delete element from array you should use index of element, not value. You can get index of first element with specific value using index method of list. As for your task, it would be better to use list comprehension:
def evenFibs(upperBound):
list_of_fibs = getFibs(upperBound)
return [value for value in list_of_fibs if value % 2 == 0]
This question already has answers here:
How to find the last occurrence of an item in a Python list
(15 answers)
Closed 8 years ago.
For example, I have a list
[0,2,2,3,2,1]
I want to find the index of the last '2' that appears in this list.
Is there an easy way to do this?
You can try the following approach. First reverse the list, get the index using L.index().
Since you reversed the list, you are getting an index that corresponds to the reversed, so to "convert" it to the respective index in the original list, you will have to substract 1 and the index from the length of the list.
n = ...
print len(L) - L[::-1].index(n) - 1