- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有两个列表:a 和 b。
a 是一个包含三个或更多字符串的列表,而 b 是一个分隔符列表。
我需要生成 a 的所有可能组合,然后将结果与 b 的所有可能组合“合并”(参见示例以更好地理解)。
我最终使用了这段代码:
from itertools import permutations, combinations, product
a = ["filename", "timestamp", "custom"]
b = ["_", "-", ".", ""]
output = []
for com in combinations(b, len(a) - 1):
for per in product(com, repeat=len(a) - 1):
for ear_per in permutations(a):
out = ''.join(map(''.join, zip(list(ear_per[:-1]), per))) + list(ear_per)[-1]
output.append(out)
# For some reason the algorithm is generating duplicates
output = list(dict.fromkeys(output))
for o in output:
print o
这是输出示例(这是正确的,在这种情况下这是我需要的):
timestamp.customfilename
filenamecustom.timestamp
custom_filenametimestamp
timestamp_custom_filename
timestamp-filename.custom
custom_filename-timestamp
filename.timestamp-custom
. . .
filename.custom.timestamp
filename-customtimestamp
custom-timestamp_filename
filename_custom-timestamp
filename.timestampcustom
timestampcustom-filename
custom-timestamp.filename
filenamecustom_timestamp
timestamp.custom_filename
custom.timestampfilename
timestampfilename.custom
customfilename_timestamp
filenametimestamp-custom
custom-filenametimestamp
timestampfilename-custom
timestamp-custom-filename
custom.filenametimestamp
customfilenametimestamp
timestampfilename_custom
custom_filename.timestamp
custom-timestamp-filename
custom-timestampfilename
filename_timestamp.custom
. . .
filename.custom-timestamp
timestamp_filenamecustom
custom_timestampfilename
timestamp.custom.filename
timestamp.filename-custom
filename-custom-timestamp
customfilename.timestamp
filename_timestamp_custom
timestamp_filename.custom
customtimestampfilename
filenamecustomtimestamp
custom.timestamp_filename
filename_customtimestamp
. . .
timestamp-customfilename
filename_custom.timestamp
这个算法有两个主要问题:
它会生成一些重复的行,所以我总是需要删除它们(在较大的数据集上速度较慢)
if len(a) > len(b) + 2
脚本不会启动。在那种情况下,我需要重复分隔符以覆盖 len(a) - 1
a 中包含的单词之间的可用空间。
最佳答案
这可能是一个可行的解决方案。它采用 a 的排列,(3*2 = 6)
,与 b 的
,总共得到product
交错排列(这里一次 2,4*4 == 16)6 * 16 == 96
个结果。
from itertools import permutations, chain, zip_longest, product
a = ["filename", "timestamp", "custom"]
b = ["_", "-", ".", ""]
i=0
for perm in permutations(a):
for prod in product(b,repeat=len(a)-1):
tpls = list(chain.from_iterable(zip_longest(perm, prod, fillvalue='')))
print(''.join(tpls))
i += 1
print(i)
关于python - Itertools - 合并两个列表以获得所有可能的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57876864/
降本手段一招鲜,增效方法吃遍天; 01 互联网行业里; 降本策略千奇百怪,手段却出奇一致;增效方法五花八门,手段更是花里胡哨; 对于企业来说;
有什么方法可以使用 angularjs 中的部分进行代码分组吗? 原因 --- 我的 Controller 包含太多代码。该 Controller 包含了多个方法和大量功能的代码,降低了代码的可读性。
不幸的是,我的数据库的数据模型必须改变,所以我正在寻找最轻松的方式来迁移我的数据。 此时情况如何: create table cargo{ id serial primary key, per
在 QTextEdit 对象中,假设我想知道字符在鼠标光标下的位置。 我会写... void MyQTextEditObject::mousePressEvent(QMouseEvent* mouse
是否可以在 C++ 中返回一个 return 语句或做一些具有类似功能的事情? 例如,如果代码中有几个函数将指针作为输入,并且每个函数都检查指针是否为 nullptr,这将很方便。如果它是一个 nul
我的 PC 上有一个控制台应用程序,它是 signalR 服务器。 我有一个 html 页面,它是互联网上的 signalR 客户端。但我尝试连接服务器,但我有一个错误的请求 400 错误。如果服务器
我想将应用程序作为后台进程运行。当点击应用程序图标时,它不会显示任何 View ,只会启动后台进程。 最佳答案 对于 iOS 这是不可能的,但是对于 android,react native 有 he
我知道有(昂贵的)框架可以让你在 VS C# 中编写 android 应用程序并将其编译为 android apk。 我也知道,可以在 VS 中编写 Java 应用程序(link)。 是否有可能,甚至
我在做: can :manage, :all if user.role == 'admin' can :approve, Anuncio do |anuncio| anuncio.try(:apr
我是一名优秀的程序员,十分优秀!