[英]Insert into every 4th character
I have seen solutions to this problem is other problems, but not in Python. 我已经看到解决此问题的方法是其他问题,但是在Python中却没有。 I was wondering, how do I insert a character in a string after every x amount characters?
我想知道,如何在每x个字符后的字符串中插入一个字符? For example, a forward-slash after every fourth character:
例如,每四个字符后加一个正斜杠:
Before:
AsQs7d4dJh2h
After:
AsQs/7d4d/Jh2h
I know in Python I would have to use slice notation. 我知道在Python中我必须使用切片符号。 x = AsQs7d4dJh2h x = x[0:4]
x = AsQs7d4dJh2h x = x [0:4]
but this only gives me the first instance, I would like to be able to do this on any string regardless of the length of that string 但这仅是第一个实例,我希望能够在任何字符串上执行此操作,而不管该字符串的长度如何
UPDATE NEW PROBLEM: What I am trying to do is separate the string in to card pairs, (2-card texas holdem), the problem is that the algorithm doesn't take into account the '10' card so separating into every 4 doesn't work, for instance this happens when faced with a 10 card in the string: 更新新问题:我想做的是将字符串分成两张成对的卡片(两张得克萨斯州扑克),问题是算法没有考虑“ 10”张卡片,因此每4张分开无效,例如,当遇到字符串中的10张卡片时,就会发生这种情况:
AsQs/10dA/h10h/2h
AsQs/10dA/h9h8/h
AsQs/10dA/h9h7/h
AsQs/10dA/h9h6/h
AsQs/10dA/h9h5/h
AsQs/10dA/h9h4/h
AsQs/10dA/h9h3/h
AsQs/10dA/h9h2/h
AsQs/10dA/h8h7/h
AsQs/10dA/h8h6/h
So what I need help with is how do I insert a forward slash or extract all the 2 card hands (in this instance 3 hands) from the string regardless if it has 1 or multiple '10' cards in the string? 因此,我需要帮助的是如何插入正斜杠或从字符串中提取所有2张牌(在这种情况下为3张牌),而不管字符串中有1张还是多张“ 10”张牌?
Complete algorithm: 完整的算法:
import itertools
strOutput = ""
lstMaster = ['As', 'Ks', 'Qs', 'Js', '10s', '9s', '8s', '7s', '6s', '5s', '4s', '3s', '2s',\
'Ad', 'Kd', 'Qd', 'Jd', '10d', '9d', '8d', '7d', '6d', '5d', '4d', '3d', '2d',\
'Ac', 'Kc', 'Qc', 'Jc', '10c', '9c', '8c', '7c', '6c', '5c', '4c', '3c', '2c',\
'Ah', 'Kh', 'Qh', 'Jh', '10h', '9h', '8h', '7h', '6h', '5h', '4h', '3h', '2h']
tupMasterEdited = itertools.combinations(lstMaster, 6)
lstMasterEdited = list(tupMasterEdited)
for combo in lstMasterEdited:
combo = str(combo).replace("(", "").replace(")", "").replace(" ", "").replace(",", "").replace("'", "")
combo = '/'.join([combo[i:i+4] for i in range(0, len(combo), 4)])
print(combo)
Probably not the best solution: 可能不是最好的解决方案:
'/'.join([str[i:i+4] for i in range(0, len(str), 4)])
>>> str = "AsQs7d4dJh2h"
>>> '/'.join([str[i:i+4] for i in range(0, len(str), 4)])
'AsQs/7d4d/Jh2h'
>>> str = "sdfjsdhjfkbsdajka"
>>> '/'.join([str[i:i+4] for i in range(0, len(str), 4)])
'sdfj/sdhj/fkbs/dajk/a'
If regexes is an option: 如果可以使用正则表达式:
>>> re.sub(r'(.{4})(?=.)', r'\1/', 'AsQs7d4dJh2h')
'AsQs/7d4d/Jh2h'
Here's a general solution for x
number of characters. 这是
x
个字符的通用解决方案。 The ch
argument can be one or several characters and there doesn't need to be an even multiple of x
characters in the string. ch
参数可以是一个或几个字符,并且字符串中的x
字符不必是偶数个。
from itertools import izip_longest
def insert_every(x, ch, str):
return ch.join(''.join(chars)
for chars in izip_longest(*([iter(str)]*x), fillvalue=''))
print insert_every(4, '/', 'AsQs7d4dJh2h') # AsQs/7d4d/Jh2h
print insert_every(4, '+-', 'AsQs7d4dJh2hZ') # AsQs+-7d4d+-Jh2h+-Z
This can be solve with a replacement: 这可以通过替换来解决:
import re
re.sub(r'((?:(?=(10|.))\2){4})(?!$)', r'\1/', 'AsQs10dAh10h2h')
(?=(10|.))\\2
emulates an atomic group (a feature that is not available in the re module) and stands for (?>10|.)
. (?=(10|.))\\2
模拟原子组(re模块中不可用的功能),并表示(?>10|.)
。 This uses the fact that the content of a lookahead is atomic. 这利用了先行内容是原子的事实。
(?!$)
is a negative lookahead and means not followed by the end of the string (?!$)
是否定的前瞻,表示不跟在字符串末尾
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.