how to lower case a list in python [duplicate] - python

This question already has answers here:
Convert a list with strings all to lowercase or uppercase
(13 answers)
Closed 6 years ago.
I am trying to read a text file in jupyter notebook and fetch the unique words in the file.
I am reading that file as a list and then trying to apply lower case on it. But the .lower() function doesn't work with a list. Please help with the same.

With a list of values in val you can do this:
valsLower = [item.lower() for item in vals]

Related

remove the [''] when printing a list in python [duplicate]

This question already has answers here:
Print list without brackets in a single row
(14 answers)
Closed 5 months ago.
For some reason when I print a list like
list = []
list.append("0")
list.append("1")
print(list[0])
the output will be ["0"]
My actual code is a large block of text. Here's a link to the actual code: https://pastebin.com/Z54NfivR
Try this:
print(*list)
This essentially unpacks your list and its elements are treated as if they were separated by commas in the print function.
I used the name list because that was included in your example but it is a good practice to avoid using python commands as variable names.

Make a list with multiple possible strings from file names with regex [duplicate]

This question already has answers here:
Regex match one of two words
(2 answers)
Closed 3 years ago.
I want to make a list of several PNG in a folder based on multiple references. So in the list I want the PNG that have the string "7029113" OR "7031503" in their name. This is what I got so far, I only need to know how to do OR with regex, and probably my wildcards are wrong too I'm not sure.
render_path = "C:/BatchRender/Renaming"
os.chdir(render_path)
list_files = glob.glob("*.png")
r = re.compile(".*7029113.*" OR ".*7031503.*")
list_40 = list(filter(r.match, list_files))
This is one way of doing it.
r = re.compile('.*(7029113|7031503).*')

Filter list and print whole name containing string python [duplicate]

This question already has answers here:
Filtering a list of strings based on contents
(5 answers)
Closed 3 years ago.
I am trying to filter a list in python.
What i need is a way to limit what is beeing printed out from the list
Example:
I want to find all subjects containing "ECON"
from this list:
List = ["INFO100","INFO104","INFO110","INFO150","INFO125","ECON100", "ECON102"]
And i want to be able to print out the full name of the objects containing "ECON" (that means i want it to return "ECON100", "ECON102")
is there an easy way to do this?
for sub_string in List:
if "ECON" in sub_string:
print(sub_string)

How to convert data in document using split-Python [duplicate]

This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Closed 4 years ago.
list.txt document contain data like
a,b,c,d,e,f
I want to insert above data into list or convert it as list. I tried this code. But it's not correct.
document=open("list.txt","r")
Mylist=[document.read().split(",")]
print(Mylist)
document.close()
with open("list.txt", "r") as Document:
print(Document.read().split(","))

Count letters in string python [duplicate]

This question already has answers here:
How to get the size of a string in Python?
(6 answers)
Closed 7 years ago.
(New to python and stack overflow)
I was curious if there was a way to count the amount of letters in a string for python. for example:
string="hello"
I just want something to count the letters then output it into a variable for later use.
The following will give the length of a string:
len(string)
In your case, you can assign it:
numLetters = len(string)
This function can be used for other objects besides strings. For additional uses, read the documentation.
Use python function len, i.e.:
size = len(string)
len()
https://docs.python.org/2/library/functions.html#len
DEMO
https://ideone.com/mhpdLi

Categories