Style for continued multi-line f-strings [closed] - python

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Do any popular Python style guides express a preference between:
x = (f'One plus one is '
f'equal to {1+1}')
and:
x = ('One plus one is '
f'equal to {1+1}')
Does it make sense to omit the f prefix for lines that don't involve interpolation? Should it be included for consistency?

If you look at the generated byte code for each of those two options, you will see that this both results in the same set of operations:
>>> dis.dis("""f'One plus one is ' f'equal to {1+1}'""")
1 0 LOAD_CONST 0 ('One plus one is equal to ')
2 LOAD_CONST 1 (2)
4 FORMAT_VALUE 0
6 BUILD_STRING 2
8 RETURN_VALUE
>>> dis.dis("""'One plus one is ' f'equal to {1+1}'""")
1 0 LOAD_CONST 0 ('One plus one is equal to ')
2 LOAD_CONST 1 (2)
4 FORMAT_VALUE 0
6 BUILD_STRING 2
8 RETURN_VALUE
So for Python, this both does exactly the same thing: The string is being concatenated at compile-time, and the whole string is being evaluated as a format string.
Note though that as per the PEP, f-strings are actually concatenated at run-time, to make sure that every format expression is evaluated independently:
Adjacent f-strings and regular strings are concatenated. Regular strings are concatenated at compile time, and f-strings are concatenated at run time.
That is why the following example will generate two FORMAT_VALUE opcodes:
>>> dis.dis("""f'{2+2}' f'{3+3}'""")
1 0 LOAD_CONST 0 (4)
2 FORMAT_VALUE 0
4 LOAD_CONST 1 (6)
6 FORMAT_VALUE 0
8 BUILD_STRING 2
10 RETURN_VALUE
This will not have an effect for f-strings that don’t actually contain any format placeholders though. Those will still be concatenated at compile-time:
>>> dis.dis("""f'foo' f'bar'""")
1 0 LOAD_CONST 0 ('foobar')
2 RETURN_VALUE
So you can just include the f prefix for constant strings anyway and when they don’t contain a format expression, they won’t end up being handled as format strings.
So in your particular case, since there is no functional difference and both options are compiled to the same thing, that leaves this as a pure style choice which everyone has to answer for themselves.
If you are looking at existing style guides, chances are that they are not covering f-strings yet. But then, you could use the same guideline as for raw strings, although I personally don’t expect style guides to really cover that area much. Just choose what looks best, is readable, and what makes most sense to you. After all, continued strings are not really that common (and you are more likely to have triple-quotes for longer strings).

Related

Count how many sentences there are in each row within a pandas column [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to determine how many sentences there are in each row.
Sent
I went out for a walk.
I don't know. I think you're right!
so boring!!!
WTF?
Nothing
I created a list of punctuation symbols I am interested in for determining the number of sentences per each row:
Output
1
2
1
1
1
In order to get this result, I first considered to split each row whether I met a symbol (for instance . or ! or ?). But I do not know how to get the count.
My code is
import re
def sentence(sent):
return re.findall('[\w][\.!\?]', sent)
df['Sent'] = df['Sent'].apply(sentence)
Could you please give my advice on how to get it?
One idea if dont need last value like 1 use Series.str.count with regex for match letter with escaped .!?:
df['Output'] = df['Sent'].str.count('[\w][\.!\?]')
print (df)
Sent Output
0 I went out for a walk. 1
1 I don't know. I think you're right! 2
2 so boring!!! 1
3 WTF? 1
4 Nothing 0
If need replace 0 by 1:
df['Output'] = df['Sent'].str.count('[\w][\.!\?]').clip(lower=1)
print (df)
Sent Output
0 I went out for a walk. 1
1 I don't know. I think you're right! 2
2 so boring!!! 1
3 WTF? 1
4 Nothing 1
Another idea is use textstat lib:
import textstat
df['Output'] = df['Sent'].apply(textstat.sentence_count)
print (df)
Sent Output
0 I went out for a walk. 1
1 I don't know. I think you're right! 2
2 so boring!!! 1
3 WTF? 1
4 Nothing 1

How to make Python treat sequences as 1-indexed instead of 0-indexed? [duplicate]

This question already has answers here:
Python/sage: can lists start at index 1?
(5 answers)
Closed 2 years ago.
Example:
My code:
lst=[15,18,20,1,19,65]
print(lst[2])
It prints 20, but I want my array to be 1-indexed and print 18 instead.
98,67,86,3,4,21
When I print the second number it should print 67 and not 86 based on indexing.
First number is 98
Second number is 67
Third number is 86 and so on. How to make my program have index 0 become index 1 and so on?
This cannot be done, Python's list and every other sequence is inherently 0-indexed. The same is true for the vast majority of modern programming languages. I would suggest you to just learn to live with it, it will not be the hardest thing in your programming career.
Subtract 1 from the index you are actually trying to look up.
print(lst[2-1])

Behaviour of "is" is different for primitives in python [duplicate]

This question already has an answer here:
What's with the integer cache maintained by the interpreter?
(1 answer)
Closed 6 years ago.
is in Python tests if 2 references point to the same object.
Numbers between -5 and 256 are cached internally so:
a = 10
b = 10
a is b # Results in True
How does this explain something like:
20000 is 20000 # Results in True
Both numbers are above 256.
Should not the 2 integers be 2 distinct objects?
The Python interpreter sees you are re-using a immutable object, so it doesn't bother to create two:
>>> import dis
>>> dis.dis(compile('20000 is 20000', '', 'exec'))
1 0 LOAD_CONST 0 (20000)
3 LOAD_CONST 0 (20000)
6 COMPARE_OP 8 (is)
9 POP_TOP
10 LOAD_CONST 1 (None)
13 RETURN_VALUE
Note the two LOAD_CONST opcodes, they both load the constant at index 0:
>>> compile('20000 is 20000', '', 'exec').co_consts
(20000, None)
In the interactive interpreter Python is limited to having to compile each (simple or compound) statement you enter separately, so it can't reuse these constants across different statements.
But within a function object it certainly would only create one such integer object, even if you used the same int literal more than once. The same applies to any code run at the module level (so outside of functions or class definitions); those all end up in the same code object constants too.
Python stores objects in memory to make things more efficient. It only needs to assign one block of memory to 20000, and so both references point to the same block of memory, resulting in True.

string in a sequence in python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am solving a problem in codechef which is
Chef has a sequence of N numbers. He like a sequence better if the sequence contains his favorite sequence as a substring.
Given the sequence and his favorite sequence(F) check whether the favorite sequence is contained in the sequence
Input
The first line will contain the number of test cases and are followed
by the cases. Each test case consists of four lines: The length of
the sequence, the sequence N,the length of F and the sequence F
Output
Print "Yes" if the sequence contains the favourite sequence int it
otherwise print "No" Constraints
1<=T<=10
1 1
Input:
2
6
1 2 3 4 5 6
3
2 3 4
6
22 5 6 33 1 4
2
4 15
Output:
Yes
No
to this I wrote
`
for _ in xrange(int(raw_input())):
raw_input()
s = raw_input()
raw_input()
f = raw_input()
print "Yes" if f in s else "No"`
it returns correct result (as far as I have checked ) bu the grader returns wrong. why is this wrong ?
Imagine a scenario where the sequence is '12 3 4' and the subsequence is '2 3 4'. Your code will return True since '2 3 4' in '12 3 4' is True. You need to convert the sequence and subsequence to integers before doing the comparison.

Printing multiple lists, vertically and formatted, in Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Let's say I have four lists:
l1=[3,5,6]
l2=[0,2]
l3=[3,4,3,1,2]
l4=[2,3,2]
And I want to print them like this:
2
1
6 3 2
5 2 4 3
3 0 3 2
Can anyone help me, please?
If you are using Python 2.x you can use izip_longest() from itertools:
for i in reversed([' '.join(map(str,i)) for i in izip_longest(l1, l2, l3, l4, fillvalue=' ')]):
print i
In Python 3.x you can use zip_longest() from itertools.
Output:
2
1
6 3 2
5 2 4 3
3 0 3 2
you may be able to solve this problem by using multi-dimensional arrays to store the lists. then cycling through them checking for values to display while using an if statement to print a space if there is no value currently residing there (aka initialize the array to none where no value resides). I believe that if you structure it like that you may have some success

Categories