我计时set()和list()构造函数。set()明显慢于list(). 我使用不存在重复项的值对它们进行了基准测试。我知道 set use hashtables 是它变慢的原因吗?
在撰写本文时(3 月8日),我正在使用 Python 3.7.5 [MSC v.1916 64 位 (AMD64)]、Windows 10。
#No significant changed observed.
timeit set(range(10))
517 ns ± 4.91 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
timeit list(range(10))
404 ns ± 4.71 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)Run Code Online (Sandbox Code Playgroud)
当大小增加set()变得非常慢时list()
# When …Run Code Online (Sandbox Code Playgroud) 我想附加到字符串列表中字典中的各个键
myDictionary = {'johny': [], 'Eli': [], 'Johny': [], 'Jane': [], 'john': [], 'Ally': []}
votes = ['johny', 'Eli', 'Eli', 'Jane', 'Ally', 'Johny', 'john', 'Eli']
outPut={'johny': ['johny'], 'Eli': ['Eli','Eli'], 'Johny': ['Johny'], 'Jane': ['Jane'], 'john': ['john'], 'Ally': ['Ally']}
Run Code Online (Sandbox Code Playgroud)
我试图这样做,但在每个键中附加整个列表
votes_dictionary={}
votes_dictionary=votes_dictionary.fromkeys(votes,[])
for i in votes:
    print(i.lower())
    votes_dictionary[i].append(i)
print(votes_dictionary)
Run Code Online (Sandbox Code Playgroud) 我有这个输出:
[[[-0.015,  -0.1533,  1.    ]]
 [[-0.0069,  0.1421,  1.    ]]
...
 [[ 0.1318, -0.4406,  1.    ]]
 [[ 0.2059, -0.3854,  1.    ]]]
Run Code Online (Sandbox Code Playgroud)
但我想删除剩余的方括号,如下所示:
[[-0.015  -0.1533  1.    ]
 [-0.0069  0.1421  1.    ]
 ...
 [ 0.1318 -0.4406  1.    ]
 [ 0.2059 -0.3854  1.    ]]
Run Code Online (Sandbox Code Playgroud)
我的代码是这样的:
XY = []
for i in range(4000):
     Xy_1 = [round(random.uniform(-0.5, 0.5), 4), round(random.uniform(-0.5, 0.5), 4), 1]
     Xy_0 = [round(random.uniform(-0.5, 0.5), 4), round(random.uniform(-0.5, 0.5), 4), 0]
     Xy.append(random.choices(population=(Xy_0, Xy_1), weights=(0.15, 0.85)))
Xy = np.asarray(Xy)
Run Code Online (Sandbox Code Playgroud) 我应该以更好的方式重写我自己的函数。该函数接受一个迭代器和一系列布尔函数,并返回输入迭代器中元素的迭代器,该迭代器返回一个真值。
def allTrue (iterator, funz):
for i in iterator:
    count = 0
    for f in funz:
        if f(i):
            count = count+1
        if count==len(funz):
            yield i
Run Code Online (Sandbox Code Playgroud)
我试着写这个:
def allTrue2 (iterator, funz):
    return (filter (f , iterator) for f in funz)
Run Code Online (Sandbox Code Playgroud)
但这是错误的。我怎样才能改进代码?
输出:
print(list(allTrue(range(50),
               (lambda x: x % 3 == 0,
                lambda x: x > 10,
                lambda x: x < 44)
               )))
>>> [12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42]
Run Code Online (Sandbox Code Playgroud) 例如,我有 2 个列表['name', 'age', 'id']和['John', '20', '441']. 我想得到输出:
name = John
age = 20
id = 441
Run Code Online (Sandbox Code Playgroud)
也就是说,从 1 列表中的元素来制作变量的名称。鉴于列表 1 的元素总是不同的。
更新:我想获得一个name值为"John"、age值为'20'等的变量。
迭代subset_candidates并检查它是否是groupsPython中任何集合的子集的有效方法是什么?
下面的示例只有几个项目,但我希望有 10k 个 subset_candidates 和 10k 个组,所以我想知道有效的方法。
也许networkX是解决方案,但我不知道应该对这种情况应用哪种方法。
subset_candidates = [
  [2, 3], # true (subset of groups[0]) 
  [10, 12], # false
  [100, 110], # true (subset of groups[2])
  [1, 10, 100], # false
]
groups = [
  [1,2,3],
  [10,11,13],
  [100, 105, 110],
]
Run Code Online (Sandbox Code Playgroud) 我想将普通列表(main_list)转换为嵌套列表(ans_list),但我不知道如何。
main_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
ans_list = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Run Code Online (Sandbox Code Playgroud) 我正在尝试这样做
#define _TEST_ test
#include <iostream>
int main()
{
        std::cout << "_TEST_" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
据我了解,我期望这个输出。
test
Run Code Online (Sandbox Code Playgroud)
但是,我得到的输出是
_TEST_
Run Code Online (Sandbox Code Playgroud)
为什么我在这里做错了?
如何在不生成二维列表的情况下拆分列表中的字符串?
list1:['Ben&Jerry', 'Julia', 'Sally&Don', 'Tom', 'Tracy&Jim']
Run Code Online (Sandbox Code Playgroud)
输出:
['Ben','Jerry','Julia','Sally', 'Don','Tom','Tracy','Jim']
Run Code Online (Sandbox Code Playgroud)
使用
flat_list=[s.split('&') if "&" in s else s for s in list1 ]
Run Code Online (Sandbox Code Playgroud)
会给我一个二维列表。
我想为 const 分配一些值,它曾经是这样的
const int x = animal == "cat" ? 0 : 1;
Run Code Online (Sandbox Code Playgroud)
但现在,我想要这样做,如果animal == cat,则分配0,如果dog返回1,否则2到const。嗯,实际上,事情没那么简单,但可以说使用方程 a,在本例中使用方程 b,在本例中使用方程 c。
当然,我总是可以做类似的事情
const int x = FunctionToHandleIt();
Run Code Online (Sandbox Code Playgroud)
但逻辑就是这么简单。我认为代码直接写出表达式要干净得多,这样人们就可以查看它并确切地知道当时发生了什么,而不是通过函数的标题和定义来查看发生了什么。
处理这样的情况,将一些东西分配给超过几行的 const ,更好的方法是什么?但是,不值得为它创建一个完整的函数吗?
python ×8
list ×6
python-3.x ×4
c++ ×2
set ×2
string ×2
algorithm ×1
c++17 ×1
coding-style ×1
dictionary ×1
flatten ×1
gcc9 ×1
performance ×1
variables ×1