[Solved] python, count characters in last line string [closed]

Split the string from the end(str.rsplit) only once and get the length of last item: >>> s = “””AAAAAAA BBBB CCCCC DDD””” >>> s.rsplit(‘\n’, 1) [‘AAAAAAA\n BBBB\n CCCCC’, ‘ DDD’] #On the other hand simple str.split will split the string more than once: >>> s.split(‘\n’) [‘AAAAAAA’, ‘ BBBB’, ‘ CCCCC’, ‘ DDD’] Now simply get … Read more

[Solved] Processing non-english text

It’s common question. Seems that you’re using cmd which doesn’t support unicode, so error occurs during translation of output to the encoding, which your cmd runs. And as unicode has a wider charset, than encoding used in cmd, it gives an error IDLE is built ontop of tkinter’s Text widget, which perfectly supports Python strings … Read more

[Solved] How to cluster with K-means, when number of clusters and their sizes are known [closed]

It won’t be k-means anymore. K-means is variance minimization, and it seems your objective is to produce paritions of a predefined size, not of minimum variance. However, here is a tutorial that shows how to modify k-means to produce clusters of the same size. You can easily extend this to produce clusters of the desired … Read more

[Solved] list populate by append function can’t be sorted by sort function

I think you may have to be more specific and provide a comparison function so that your list can be sorted. Took the code below from the following site. Hope it helps: https://wiki.python.org/moin/HowTo/Sorting >>> student_tuples = [ (‘john’, ‘A’, 15), (‘jane’, ‘B’, 12), (‘dave’, ‘B’, 10), ] >>> sorted(student_tuples, key=lambda student: student[2]) # sort by … Read more

[Solved] fix thsi please [closed]

Your code has multiple errors. Try this it should work. I tried on python3 and modified for python2.7 so there could be some syntax error. I’ve explained the errors in comment class restaurant(): def __init__(self): self.name = “” self.menu = {} self.order = [] self.bill = 0 def print_menu(self): print “MENU CARD” ##This should be … Read more

[Solved] Unique permutations of a list without repetition

As mentioned by @Daniel Mesejo comment, use combinations. >>> import itertools >>> set(itertools.combinations([‘nyc’,’sf’,’atl’], 2)) {(‘nyc’, ‘atl’), (‘sf’, ‘atl’), (‘nyc’, ‘sf’)} 2 solved Unique permutations of a list without repetition

[Solved] If statement vs if else

Based on your if test, one of the following two things happens: you set predict[passenger_numberId] to 1 first, then immediately, set it to 0. you set predict[passenger_numberId] to 0. So, without an else statement, you always set predict[passenger_numberId] to 0, and it doesn’t actually matter what the outcome of the passenger.gender == ‘female’ test is. … Read more

[Solved] How to update particular elements in a list? [closed]

If you write all the statements in the same line, it will be an invalid syntax. For this, solution 1: a=[‘vishal’,123,345,’out’,25,’going’] a[2]=455 a[0]=’dinesh’ Now print your list, you will see the result: [‘dinesh’, 123, 455, ‘out’, 25, ‘going’] If you don’t want to write in different lines, you can do this, Solution 2: a=[‘vishal’, 123, … Read more

[Solved] Do the projection (with Jacobian) and marginalisation (inversion of matrix and remove a row/column and reinversion) commute?

To do np.dot last dimension of first matrix must be the same as first dimension of second one. They are not, so you are getting ValueError, that shapes are not aligned. Everything seems to be fine as you printed, but then you forgot about lines: j_temp = np.copy(J_2_SYM) # Add row/col into J_2_SYM j_temp = … Read more

[Solved] How join a list of raw strings? [closed]

The join command works fine, but when you print the string, it cares about special characters so puts a new line whenever encounter to \n. But you can change the print behavior to escape special characters like this: Use repr: a = “Hello\tWorld\nHello World” print(repr(a)) # ‘Hello\tWorld\nHello World’ In your source code it would be … Read more

[Solved] Identation Error

Your code, properly indented, should look like this: def update_bullets(bullets): “””Update position of bullets and gets rid of old bullets””” #Update bullet position bullets.update() # Get rid of bullets that had dissapeared for bullet in bullets.copy(): if bullet.rect.bottom <= 1: bullets.remove(bullet) print(len(bullets)) Python code requires proper indentation at all times. Whitespace matters (unlike C)! I … Read more