简体   繁体   English

在python中生成数字和交替字符串的序列

[英]Generate a sequence of number and alternating string in python

Aim 目标

I would like to generate a sequence as list in python , such as: 我想在python生成一个序列作为list ,例如:

['s1a', 's1b', 's2a', 's2b', ..., 's10a', 's10b']

Properties: 特性:

  • items contain a single prefix 项目包含单个前缀
  • numbers are sorted numerical 数字按数字排序
  • suffix is alternating per number 后缀按数字交替显示

Approach 方法

To get this, I applied the following code, using an xrange and comprehensive list approach: 为此,我使用了xrange和综合列表方法应用了以下代码:

# prefix
p = 's'
# suffix
s = ['a', 'b']
# numbers
n = [ i + 1 for i in list(xrange(10))]
# result
[ p + str(i) + j for i, j in zip(sorted(n * len(s)), s * len(n)) ]

Question

Is there a more simple syntax to obtain the results, eg using itertools ? 是否有更简单的语法来获取结果,例如使用itertools Similar to this question? 类似于这个问题?

双重理解列表可以实现以下目的:

['s'+str(x)+y for x in range(1,11) for y in 'ab']

itertools.product might be your friend: itertools.product可能是您的朋友:

all_combos = ["".join(map(str, x)) for x in itertools.product(p, n, s)]

returns: 返回:

['s1a', 's1b', 's2a', 's2b', 's3a', 's3b', 's4a', 's4b', 's5a', 's5b', 's6a', 's6b', 's7a', 's7b', 's8a', 's8b', 's9a', 's9b', 's10a', 's10b']

EDIT: as a one-liner: 编辑:作为单线:

all_combos = ["".join(map(str,x)) for x in itertools.product(['s'], range(1, 11), ['a', 'b'])]

EDIT 2: as pointed out in James' answer, we can change our listed string element in the product call to just strings, and itertools will still be able to iterate over them, selecting characters from each: 编辑2:正如詹姆斯回答中所指出的,我们可以将product调用中列出的字符串元素更改为仅字符串,并且itertools仍然可以对其进行迭代,并从每个字符串中选择字符:

all_combos = ["".join(map(str,x)) for x in itertools.product('s', range(1, 11), 'ab')]

How about: 怎么样:

def func(prefix,suffixes,size):
    k = len(suffixes)
    return [prefix+str(n/k+1)+suffixes[n%k] for n in range(size*k)]

# usage example:
print func('s',['a','b'],10)

This way you can alternate as many suffixes as you want. 这样,您可以根据需要替换任意数量的后缀。

And of course, each one of the suffixes can be as long as you want. 当然,每个后缀都可以根据需要设置。

You can use a double-list comprehension, where you iterate on number and suffix. 您可以使用双列表理解,在其中迭代数字和后缀。 You don't need to load any 您不需要加载任何

Below is a lambda function that takes 3 parameters, a prefix, a number of iterations, and a list of suffixes 下面是一个lambda函数,它带有3个参数,一个前缀,多次迭代和一个后缀列表

foo = lambda prefix,n,suffix: list(prefix+str(i)+s for s in suffix for i in range(n))

You can use it like this 你可以这样使用

foo('p',10,'abc')

Or like that, if your suffixes have more than one letter 或者这样,如果您的后缀有多个字母

foo('p',10,('a','bc','de'))

For maximum versatility I would do this as a generator. 为了获得最大的通用性,我会以此为发​​电机。 That way you can either create a list, or just produce the sequence items as they are needed. 这样,您既可以创建列表,也可以仅根据需要生成序列项。

Here's code that runs on Python 2 or Python 3. 这是在Python 2或Python 3上运行的代码。

def psrange(prefix, suffix, high):
    return ('%s%d%s' % (prefix, i, s) for i in range(1, 1 + high) for s in suffix)

res = list(psrange('s', ('a', 'b'), 10))
print(res)

for s in psrange('x', 'abc', 3):
    print(s)

output 输出

['s1a', 's1b', 's2a', 's2b', 's3a', 's3b', 's4a', 's4b', 's5a', 's5b', 's6a', 's6b', 's7a', 's7b', 's8a', 's8b', 's9a', 's9b', 's10a', 's10b']
x1a
x1b
x1c
x2a
x2b
x2c
x3a
x3b
x3c

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

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