简体   繁体   English

从字符串创建特定列表

[英]create a specific list from strings

Edited according to comments: 根据评论编辑:

I would like to use the strings 'ABC' and '123' to create the list 我想使用字符串'ABC''123'创建列表

['A1','B2','C3','A3','C1']

The logic would be to pair the first string with the second string in sequence and then again backwards: 逻辑是将第一个字符串与第二个字符串按顺序配对,然后再次向后:

forwards: A1, B2, C3 backwards: A3, B2, C1 前进:A1,B2,C3前进:A3,B2,C1

B2 only appears once as the final list should be distinct values. B2仅出现一次,因为最终列表应该是不同的值。

I should be able to expand each of the two strings(they will always match in length) and have the same result pattern. 我应该能够扩展两个字符串中的每个字符串(它们的长度始终匹配)并具有相同的结果模式。

I tried a couple of things but I would like to know if there is a more pythonic way to accomplish this. 我尝试了几件事,但是我想知道是否还有一种更Python的方法来完成此任务。

The following solutions will work in Python 3.x, 以下解决方案可在Python 3.x中使用,

from collections import OrderedDict

s1 ='ABC'
s2 = '123'

result = list(OrderedDict.fromkeys(first + second for first, second in zip(s1 + s1, s2 + s2[::-1])))
print(result)

Output 输出量

['A1', 'B2', 'C3', 'A3', 'C1']

This first solution assumes you only need to repeat the iterables once. 第一个解决方案假定您只需要重复一次可迭代。 If you have to repeat the shortest iterable more than once you could use cycle to iterate several times over it: 如果必须多次重复最短的迭代,则可以使用循环对其进行多次迭代:

from itertools import cycle

s1 ='ABC'
s2 = '123'

seen = set()
result = []
for first, second in zip(cycle(s1), s2 + s2[::-1]):
    first_second = first + second
    if first_second not in seen:
        seen.add(first_second)
        result.append(first_second)

print(result)

Output 输出量

['A1', 'B2', 'C3', 'A3', 'C1']

The idea in this second solution is to use a set of seen elements and only add to the final result if not seen. 第二种解决方案的想法是使用一可见元素,并且仅在看不见的情况下才添加到最终结果中。 Finally you could combine the solutions and use cycle and OrderedDict : 最后,您可以组合解决方案并使用cycleOrderedDict

from itertools import cycle
from collections import OrderedDict

s1 ='ABC'
s2 = '123'

result = list(OrderedDict.fromkeys(first + second for first, second in zip(cycle(s1), s2 + s2[::-1])))
print(result)

Output 输出量

['A1', 'B2', 'C3', 'A3', 'C1']

If version greater than 3.6: 如果版本大于3.6:

print(list(map(''.join,list(dict.fromkeys(list(zip(s1,s2))+list(zip(s1,s2[::-1])))))))

Output: 输出:

['A1', 'B2', 'C3', 'A3', 'C1']

I tried a couple of things but I would like to know if there is a more pythonic way to accomplish this. 我尝试了几件事,但是我想知道是否还有一种更Python的方法来完成此任务。

Without saying what you've tried, it's hard to suggest an alternative, pythonic or otherwise. 不说您尝试过的方法,很难建议使用pythonic或其他方法。 Looking at what you're doing, it seems you're constructing a mapping between sets of the form: 看看您在做什么,似乎您正在构造表单之间的映射:

设置映射

This is about as atomic as it gets with respect to logical relationships; 就逻辑关系而言,这几乎是原子的。 I'm not sure there's any method that would be quicker than a dumb-as-a-rock string concatenation by index: 我不确定是否有任何方法会比按索引的dumb-as-a-rock字符串连接更快:

x = 'ABC'
y = '123'

new_list = [x[0] + y[0], x[1]+y[1], x[2]+y[2], x[0]+y[2], x[2]+y[0]]

Output: 输出:

['A1', 'B2', 'C3', 'A3', 'C1']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM