This question already has answers here:
Python for-in loop preceded by a variable [duplicate]
(5 answers)
Closed 6 years ago.
I'm new to python, and I'm trying to understand the following line:
"".join(char for char in input if not unicodedata.category(char).startswith('P'))
Source: https://stackoverflow.com/a/11066443/3818487
This code removes all unicode punctuation from input. I don't understand why it works. As far as I can tell, it just iterates over all characters in input ignoring the punctuation characters. How can it access char before it is declared in the for loop? I come from a java background, so this is very confusing to me.
This comprehension would look more like the following, in regular code (using a list to store our non-punctuation characters).
#input is defined somewhere prior to the loop
output = []
for char in input:
if not unicodedata.category(char).startswith('P'):
output.append(char)
''.join(output)
Comprehensions iterate over the loop portion first, with the value being iterated over on the left.
Related
This question already has answers here:
Python string.strip stripping too many characters [duplicate]
(3 answers)
Strip removing more characters than expected
(2 answers)
How to remove the left part of a string?
(21 answers)
Closed 13 days ago.
I have the following list of elements named 'files_temp':
['CDS_SPREAD_AA1EUNBCBM', 'CDS_SPREAD_AA1EUNCCBM', 'CDS_SPREAD_AA1USNBCBM', 'CDS_SPREAD_AA1USNCCBM', 'CDS_SPREAD_AALLN1EUNECBM', 'CDS_SPREAD_AALLN1USNECBM', 'CDS_SPREAD_ABB3EUNECBM', 'CDS_SPREAD_ABB3USNECBM', 'CDS_SPREAD_ABX1EUNCCBM', 'CDS_SPREAD_ABX1USNCCBM', 'CDS_SPREAD_ACAFP1EUBECBM', 'CDS_SPREAD_ACAFP1EUNECBM', 'CDS_SPREAD_ACOM1JPNACBM', 'CDS_SPREAD_ACOM1USNACBM', 'CDS_SPREAD_AEGON1EUBACBM', 'CDS_SPREAD_AEGON1EUNECBM', 'CDS_SPREAD_AEGON1JPBACBM', 'CDS_SPREAD_AEGON1USBACBM', 'CDS_SPREAD_AEGON1USNECBM', 'CDS_SPREAD_AEP1USNBCBM' ...]
I would like to keep only the alphanumeric codes, removing the CDS_SPREAD_ part and tried the following code:
files_temp=[elem.strip('CDS_SPREAD_') for elem in files_temp]
However, besides the CDS_SPREAD_ part it is also removing a part of the alphanumeric code:
['1EUNBCBM', '1EUNCCBM', '1USNBCBM', '1USNCCBM', 'LLN1EUNECBM', 'LLN1USNECBM', 'BB3EUNECBM', 'BB3USNECBM', 'BX1EUNCCBM', 'BX1USNCCBM', 'FP1EUBECBM', 'FP1EUNECBM', 'OM1JPNACBM', 'OM1USNACBM', 'GON1EUBACBM', 'GON1EUNECBM', 'GON1JPBACBM', 'GON1USBACBM', 'GON1USNECBM', '1USNBCBM', '1USNCCBM', 'T1EUNCCBM', 'T1USNBCBM' ...]
For instance, for the first element, in theory I should get AA1EUNBCBM instead of 1EUNBCBM. Would you know why this is happening? I would highly appreciate an alternative to solve the issue as well.
The strip() function removes all the characters you are providing as the parameters. For your case, you should use replace() function.
files_temp=[elem.replace('CDS_SPREAD_', '') for elem in files_temp]
This question already has an answer here:
Why does printing a tuple (list, dict, etc.) in Python double the backslashes?
(1 answer)
Closed 1 year ago.
enter image description here
is there a way to print single backslash within list?
Regarding the first version of your question, I wrote this:
First, this expression x='\' isn't right in Python in python. you should rather puth it this way: x='\\', since back slash is a special character in python.
Second, try this:
l=['\\'] print(l)
This will print: ['\\']
But when you execute this: print(l[0]), it renders this '\'. So basically, this ['\\'] is the way to print a backslash within a list.
This question already has answers here:
How do I save results of a "for" loop into a single variable? [duplicate]
(2 answers)
Closed 1 year ago.
I have written a code that prints all letters, but I want to take its output to put it in a variable. i.e. I want to take all letters and save it in one variable instead of writing all letters manually.
for letter in range(97, 123):
letters = chr(letter)
print(letters, end=" ")
If you wish to complete this in one line, Python has a neat way of allowing you to create lists with the use of list comprehensions. If you wish to know more about what a list comprehension is, you may see point 5.1.3 here.
letters = [chr(letter) for letter in range(97, 123)]
A more comprehensible way of solving the problem is to append the characters to a letters list which should be defined before you begin to loop your range.
letters = []
for letter in range(97, 123):
letters.append(chr(letter))
print(letters)
The Python documentation does a good job of explaining the different data structures used throughout the programming language, I hope this clears up the various ways to solve your problem described.
This question already has answers here:
Python for-in loop preceded by a variable [duplicate]
(5 answers)
Closed 4 years ago.
I'm new to Python, so I was hoping somebody could break down the following statement and explain the purpose of each part.
[digit for digit in string.split() if digit.isdigit()][0]
Obviously for digit in string.split() creates a list of substrings by separating the string into elements at each space.
What confuses me is the digit at the very beginning and the if statement at the very end.
Is the very first digit what will be returned if digit.isdigit()?
Why must this statement be wrapped in a list?
I've never seen a for loop and an if statement combined into one statement like this before, but it reminds me of a particular JS syntax: for (condition) // whatever or if (condition) // whatever. However, in JS you can't combine them into a single statement (i.e. for (condition) if (condition) // whatever).
This is called a list comprehension. You will find plenty of pages explaining how it works. Just ask you favorite search engine.
This question already has answers here:
why is python string split() not splitting
(3 answers)
Closed 6 years ago.
I've got a simple block of code which is meant to iterate over a list of strings and split each item in the list into a new list, then call another function on each item:
list_input = take_input()
for item in list_input:
item.split()
system_output(item)
The problem is that 'item.split()' doesn't seem to be doing anything. With a print(item) statement in the penultimate line, all that is printed to the console is the contents of item, not the contents split into a new list. I feel like I'm missing something obvious, can anyone help? Thanks!
EDIT: So I've been informed that strings are immutable in Python, and in light of this replaced the 'item.split()' line with 'item = item.split()'. However, I am still running into the same error, even with item redefined as a new variable.
split() does not split the string inplace, it only returns a splitted string that you have to put in an other variable.