Codehs Python, Remove All from String

Remove string shorter than k from a list of strings

This is exactly the type of situation the filter function is intended for:

>>> mylist = ['a', 'b', 'c', 'aa', 'bb', 'cc', 'aaa', 'bbb', 'ccc',  'aaaa', 'bbbb', 'cccc']
>>> minlist = list(filter(lambda i: len(i) == 4, mylist))
>>> minlist
['aaaa', 'bbbb', 'cccc']

filter takes two arguments: the first is a function, and the second is an iterable. The function will be applied to each element of the iterable, and if the function returns True, the element will be kept, and if the function returns False, the element will be excluded. filter returns the result of filtering these elements according to the passed in function

As a sidenote, the filter function returns a filter object, which is an iterator, rather than a list (which is why the explicit list call is included). So, if you're simply iterating over the values, you don't need to convert it to a list as it will be more efficient

How do I delete a specific label on a frame that doesn't seem to have a name?

Got help in the comments, but posting here so it's easier to see. Basically just save the labels in a list as they are created and destroy them one by one later. Like this:

        def display():
global x
x = []
global y
y = []
global overall_data
length = len(overall_data)
print(length)
for i in range(len(overall_data)):
print("i = " + str(i))
global l
bg_colour = "Black"
l = tk.Message(my_frame3,
text = "Name = " + str(overall_data[i][0]) + "\n"
"Health = /" + str(overall_data[i][1]) + " \n"
"Armour Class = " + str(overall_data[i][2]) + "\n"
"Initiative = " + str(overall_data[i][3]),
fg='White',
bg=bg_colour)
x.append(l)
l.place(x = 20 + i*150, y = 60, width=120, height= 80)
string = str(overall_data[i][1])
data = tk.StringVar(my_frame3, value = string)
health_input = tk.Entry(my_frame3, textvariable = data, width = 15,fg = "Black",bg = "White")
health_input.place(x = 85 + i*150, y = 85, width = 25, height=15)
y.append(health_input)
def remove_labels():
for i in range(len(x)):
x[i].destroy()
for i in range(len(y)):
y[i].destroy()

How to remove all recent console command

If you want to clear the list of last typed commands, follow these steps:

(Step 1 and 2 are important, don't skip them!)

  1. Undock the console (click on the icon in the bottom-left corner, undock icon).

    (if you don't see the undock icon, but , then hold the mouse pressed for a few seconds to get the desired icon)
  2. Press Ctrl + Shift + J to open the console for this console. (On OSX use Cmd + Option + i)
  3. Go to the Resources tab, "Local Storage", chrome-devtools://devtools.
  4. Right-click on the item with key "consoleHistory", and choose "Delete".
    chrome-devtools://devtools -> consoleHistory -> Delete

  5. Done! You may close the new console, and then dock the previous one if wanted. The console history will be gone when you reload the console.

If you just want to clear the console log (not the commands), just press Ctrl + L.

You could also use Incognito mode if you don't want to keep the list of commands you're going to type.

How to reverse caps lock in a string in python?

For text character replacement python string has the str.maketrans and str.translate methods:

from string import ascii_lowercase as ascii_up, ascii_uppercase as ascii_low

def reverseCase(text):
m = str.maketrans(ascii_low + ascii_up, ascii_up + ascii_low)
return text.translate(m)


for w in ("tata", "TATA", "TaTa", "42"):
print(reverseCase(w))

Output:

TATA
tata
tAtA
42

To detect things

[...] either is like 'tHIS' [...]

you can use:

def isWordStartingLowerAndContinuingUpperCased(word):
"""Check if words starts with lower case and continues upper cased."""
return word[0].islower() and word[1:].isupper()

for w in ("tHIS", "This", "THIS"):
print(w, ":", isWordStartingLowerAndContinuingUpperCased(w))

To get

tHIS : True 
This : False
THIS : False

When normalizing text be aware of false positives - there is a huge group of words that belong into all-caps and should not be changed - abbreviations:

NBL, USA, MIB, CIA, FBI, NSA, MI6, NASA, DARPA, etc.



Related Topics



Leave a reply



Submit