Looking for better way of making a single list of forwards and reverse directory names for matching with files

huangapple go评论68阅读模式
英文:

Looking for better way of making a single list of forwards and reverse directory names for matching with files

问题

以下是您要翻译的代码部分:

Input = ['cat', 'tab', 'mad dog']
output = [x for x in Input] + [' '.join(x.split()[::-1]) for x in Input if len(x.split()) > 1]

以下是翻译结果:

Input = ['cat', 'tab', 'mad dog']
output = [x for x in Input] + [' '.join(x.split()[::-1]) for x in Input if len(x.split()) > 1]

请告诉我,如果您还需要其他帮助。

英文:

Here is the code I'm currently working with.

Input = ['cat','tab','mad dog']
output = [x for x in Input] + [' '.join(x.split(' ')[::-1]) for x in Input if len(x.split())>1]

It works fine and is pretty fast but I don't think it reads particularly well.

Is there anything better that wouldn't sacrifice speed, as that is the most important thing for its use?

Why? its actually for searching directories where the name (one or two words) may have been typed in reverse.

output= ['cat', 'tab', 'mad dog', 'dog mad']

This is the correct result but I'm pretty sure there is a better way of getting there.

Thank you.

答案1

得分: 1

output = Input + [' '.join(x.split()[::-1]) for x in Input if ' ' in x]
英文:

as #slothrop commented

Input = ['cat','tab','mad dog']

output = Input + [' '.join(x.split()[::-1]) for x in Input if ' ' in x]

or

Input.append(*[' '.join(x.split()[::-1]) for x in Input if ' ' in x])

or

output = Input + [' '.join(reversed(x.split())) for x in Input if ' ' in x]

#['cat', 'tab', 'mad dog', 'dog mad']

答案2

得分: 1

将问题稍微反转的另一种方法是将目标目录集合定义为一组单词的集合。集合本质上是无序的,因此无需遍历每种组合。如果您的名称超过2个单词,这种方法将扩展得相当不错。

然后,要检查一个目录是否对您感兴趣,请将目录名称转换为一组单词,并查看它是否在目标集合中。

例如:

# 1) 创建目标单词集合
input_words = ['cat', 'tab', 'mad dog']
target_word_sets = {frozenset(x.split()) for x in input_words}

# 2) 检查目录名称是否在目标单词集合中,程序中需要的任何地方
my_dir_names = ['cat', 'mad cat', 'mad dog', 'dog mad']
for dn in my_dir_names:
    is_in_target = set(dn.split()) in target_word_sets
    print(f'{dn}: {is_in_target}')

将打印:

cat: True
mad cat: False
mad dog: True
dog mad: True

请注意,在使用一组集合时,内部集合必须是可散列的,因此使用了frozenset而不是普通的set

英文:

Another approach, flipping the problem round a bit, is to define your collection of target directories as a set of sets of words. A set is inherently unordered, so there's no need to go through every combination. So this would scale up quite well if you have names with more than 2 words.

Then to check whether a directory is of interest to you, turn the directory name into a set of words and see if that's in the target set.

For example:

# 1) Creating the target word sets
input_words = ['cat','tab','mad dog']
target_word_sets = {frozenset(x.split()) for x in input_words}

# 2) Checking a directory name against the target word sets, wherever in the program this is needed
my_dir_names = ['cat', 'mad cat', 'mad dog', 'dog mad']
for dn in my_dir_names:
    is_in_target = set(dn.split()) in target_word_sets
    print(f'{dn}: {is_in_target}')

which prints:

cat: True
mad cat: False
mad dog: True
dog mad: True

Note, when using a set of sets, the inner sets must be hashable, hence the use of frozenset rather than plain old set.

huangapple
  • 本文由 发表于 2023年5月13日 20:30:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定