(数组[i] ==; 1)比Python中的(array [i] ==')快的速度更快?

发布于 2025-02-12 07:25:10 字数 1446 浏览 1 评论 0原文

使用

if(array[i]=="1")

所有测试用例(以黑客等级为单位)。

但是

if(array[i]=='1')

没有通过所有测试用例。

“”''之间有什么区别?
为什么后者需要更多时间?

这是屏幕截图:

使用单价:

使用双引号:

代码:错误:

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'acmTeam' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts STRING_ARRAY topic as parameter.
#

def acmTeam(topic):
    m=0
    count=0
    for i in range(len(topic)):
        for j in range(i+1,len(topic)):
            k=(bin(int(topic[i],2) | int(topic[j],2)))[2:]
            # if k>m:
            #     m=k
            #     count=1
            # elif m==k:
            #     count+=1
            val=0
            for ind in range(len(k)):
                if (k[ind]=="1"):
                    val+=1
            if val>m:
                count=1
                m=val
            elif val==m:
                count+=1

    return [m,count]
               

Using

if(array[i]=="1")

passes all test cases (in Hacker rank).

But

if(array[i]=='1')

doesn't pass all the test cases.

What are the differences between "" and '' in Python?
Why does the latter take more time?

Here are the screenshots:

Using single-quotes:
HackerRank code and error using double-quotes

Using double-quotes:
HackerRank code and error using single-quotes

The code:

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'acmTeam' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts STRING_ARRAY topic as parameter.
#

def acmTeam(topic):
    m=0
    count=0
    for i in range(len(topic)):
        for j in range(i+1,len(topic)):
            k=(bin(int(topic[i],2) | int(topic[j],2)))[2:]
            # if k>m:
            #     m=k
            #     count=1
            # elif m==k:
            #     count+=1
            val=0
            for ind in range(len(k)):
                if (k[ind]=="1"):
                    val+=1
            if val>m:
                count=1
                m=val
            elif val==m:
                count+=1

    return [m,count]
               

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白龙吟 2025-02-19 07:25:10

没有区别。

如果我们查看使用dis软件包比较单引号与双引号的比较的字节码:

单引号

>>> dis.dis(lambda: input() == 'A')
  1           0 LOAD_GLOBAL              0 (input)
              2 CALL_FUNCTION            0
              4 LOAD_CONST               1 ('A')
              6 COMPARE_OP               2 (==)
              8 RETURN_VALUE

双引号

>>> dis.dis(lambda: input() == "A")
  1           0 LOAD_GLOBAL              0 (input)
              2 CALL_FUNCTION            0
              4 LOAD_CONST               1 ('A')
              6 COMPARE_OP               2 (==)
              8 RETURN_VALUE

python的词汇分析过程,

使用Python's <代码> AST 模块,我们可以在编译之前先查看代码。

单引号

>>> print(ast.dump(ast.parse('a == \'hello\'', mode='eval'), indent=4))
Expression(
    body=Compare(
        left=Name(id='a', ctx=Load()),
        ops=[
            Eq()],
        comparators=[
            Constant(value='hello')]))

双引号

>>> print(ast.dump(ast.parse('a == "hello"', mode='eval'), indent=4))
Expression(
    body=Compare(
        left=Name(id='a', ctx=Load()),
        ops=[
            Eq()],
        comparators=[
            Constant(value='hello')]))

结论

可能是Python评估器中的错误。

该代码无论哪种方式都是完全相同的,并且没有真正的方法知道这是否是巧合的,而无需检查口译员C实现的最后一个细节。

There's no difference.

If we view the bytecode for comparing single quoted vs double quoted comparisons using the dis package:

Single Quoted

>>> dis.dis(lambda: input() == 'A')
  1           0 LOAD_GLOBAL              0 (input)
              2 CALL_FUNCTION            0
              4 LOAD_CONST               1 ('A')
              6 COMPARE_OP               2 (==)
              8 RETURN_VALUE

Double Quoted

>>> dis.dis(lambda: input() == "A")
  1           0 LOAD_GLOBAL              0 (input)
              2 CALL_FUNCTION            0
              4 LOAD_CONST               1 ('A')
              6 COMPARE_OP               2 (==)
              8 RETURN_VALUE

Python's lexical analyzation process

Using Python's ast module, we can look at our code before it's compiled.

Single Quoted

>>> print(ast.dump(ast.parse('a == \'hello\'', mode='eval'), indent=4))
Expression(
    body=Compare(
        left=Name(id='a', ctx=Load()),
        ops=[
            Eq()],
        comparators=[
            Constant(value='hello')]))

Double Quoted

>>> print(ast.dump(ast.parse('a == "hello"', mode='eval'), indent=4))
Expression(
    body=Compare(
        left=Name(id='a', ctx=Load()),
        ops=[
            Eq()],
        comparators=[
            Constant(value='hello')]))

Conclusion

Probably an incorrect bit in Python's evaluator.

The code is the exact same either way, and there's no real way of knowing if this is coincidence without inspecting every last detail of the interpreter's C implementation.

哭泣的笑容 2025-02-19 07:25:10

我不知道为什么会发生,另一个会发生。每次执行的时间都不同。

关于双人行情,在python中没有差异,您可以在无数的网页中找到它。不幸的是,我在官方页面上找不到它, http://www.python.org/

I don't know why one happens and the other doesn't. The times vary in each execution.

Regarding the double and single quotes, in python there are no differences, you can find this in countless web pages. Unfortunately I couldn't find it on the official page http://www.python.org/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文