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.
Related
This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed last year.
import time
while True:
npc=input("Josuke\n\nJotaro Kujo (Part 4)\n\nChoose NPC to talk to.")
if npc=="Josuke" or npc=="josuke":
confirm=input("[Press E to interact.]")
elif npc=="jp4" or npc=="JP4" or npc=="Jp4
Within this code you can see that there are 2 NPCs to interact to. Because there are many ways of addressing the name Jotaro Kujo (Part 4), the if statement has many "or"s, but I want to be able to condense it. Could I use an array to be able to put the possibilities in and then have the if statement identify if the value is within the array? (I haven't completed the code yet, but the problem doesn't require the full code to be completed.)
Yes, you can do it easily using the in operator to check if a particular string is in the list.
as an example:
lst = ["bemwa", "mike", "charles"]
if "bemwa" in lst:
print("found")
And if all the possibilities you want to cover are related to case-insensitivity you can simply convert the input to lower-case or upper-case and compare it with only one possibility.
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:
Carets in Regular Expressions
(2 answers)
Difference between * and + regex
(7 answers)
Closed 5 years ago.
I apologize for the poorly worded question.
I have a large number of strings like:
"ODLS_ND33283633__PS1185"
Which the first letters up to the first "_" are a header and the remainder (ND33283633__PS1185) is a unique ID.
I wrote a regex in python trying to remove everything up to the first "_" desiring
"ND33283633__PS1185"
as the end result.
I figured something like:
.*_? or .+?_
Would do the trick, but that was not the case...
I kept trying to write various regex unsuccessfully to accomplish this and finally went online and found another person's answer I was able to use as an example to rewrite as:
^[^_]+_
Which gave me my desired result, but now I have questions which I can't figure out the answer for:
I found that removing the "^" at the front and writing it as:
[^_]+_
caused the regex to remove everything up to the second "_" so the resulting string was:
"_PS1185"
I understand that "^" identifies as the beginning of the line, but I would like to know why not including it removes up to the second without the "^" at the front?
My understanding is that [^_]+ matches characters NOT equal to "_" 1 or more number of times, so why would including the "^" at the beginning cause it to stop at the first, while excluding it causes it to stop at the second?
Another thing, when I replaced the "+" symbol with a "*":
[^_]*_
I expected the same result but instead got:
PS1185
I thought that * matches 0 or more, while + matches 1 or more, so they're effectively the same except + is supposed to be more 'strict'. However, seeing these results makes me feel like I don't fully understand how regex is behaving. Is there anyone here that can please explain what is actually going on?
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.
This question already has answers here:
Are there limits to using string.lstrip() in python? [duplicate]
(3 answers)
Closed 8 years ago.
So I have a super long string composed of integers and I am trying to extract and remove the first three numbers in the string, and I have been using the lstrip method (the idea is kinda like pop) but sometimes it would remove more than three.
x="49008410..."
x.lstrip(x[0:3])
"8410..."
I was hoping it would just remove 490 and return 08410 but it's being stubborn -_- .
Also I am running Python 2.7 on Windows... And don't ask why the integers are strings. If that bothers you, just replace them with letters. Same thing! LOL
Instead of remove the first 3 numbers, get all numbers behind the third position. You can do it using : operator.
x="49008410..."
x[3:]
>> "8410..."