小编Sid*_*bra的帖子

从 C 中的 n 生成 k 个排列

我基本上需要itertools在 C中使用以下 Python命令的等效结果:

a = itertools.permutations(range(4),2))

目前我的方法包括如所示的第一“中选择”从10个然后生成排列5个元素为那些5个元素这里

这种方法的问题在于输出的顺序。我需要它是(a),而我得到的是(b),如下所示。

a = itertools.permutations(range(4),2)
for i in a:
    print(i)

(0, 1)
(0, 2)
(0, 3)
(1, 0)
(1, 2)
(1, 3)
(2, 0)
(2, 1)
(2, 3)
(3, 0)
(3, 1)
(3, 2)

b = itertools.combinations(range(4),2) 
for i in b:
    c = itertools.permutations(i)
    for j in c:
        print(j)
(0, 1)
(1, 0)
(0, 2)
(2, 0)
(0, 3)
(3, 0)
(1, 2)
(2, 1)
(1, 3)
(3, 1)
(2, 3) …
Run Code Online (Sandbox Code Playgroud)

c permutation python-itertools

5
推荐指数
1
解决办法
1402
查看次数

BeautifulSoup如何在运行时创建对象名称?

BeautifulSoup如何在运行时创建对象名称?以下面的代码为例

from bs4 import BeautifulSoup
html = """<html>
<head>
<sid><b>Custom Tag</b></sid>
<sid><b>Custom Tag</b></sid>
<sid><b>Custom Tag</b></sid>
</head>
</html>"""
soup = BeautifulSoup(html)
print(soup.html.head.sid)
Run Code Online (Sandbox Code Playgroud)

如何创建名称'sid'的对象.之前我认为标准的html标签是预先创建的,但是自定义标签作为对象名称的出现意味着我的理解是错误的.我有限的理解是bs4首先创建和类型的对象,<class 'bs4.BeautifulSoup'>递归地创建类型的对象<class 'bs4.element.Tag'>我的问题是bs4如何动态地将这些<class 'bs4.element.Tag'>对象命名为在html中找到的实际标签?

我怎么能复制这个?

python class beautifulsoup

2
推荐指数
1
解决办法
105
查看次数

标签 统计

beautifulsoup ×1

c ×1

class ×1

permutation ×1

python ×1

python-itertools ×1