相关疑难解决方法(0)

使用Python每64个字符插入一个换行符

使用Python我需要每64个字符在字符串中插入换行符.在Perl中很简单:

s/(.{64})/$1\n/
Run Code Online (Sandbox Code Playgroud)

如何使用Python中的正则表达式来完成?有更多的pythonic方式吗?

python regex

23
推荐指数
5
解决办法
3万
查看次数

拆分长串而不会破坏满足线条的单词

在你认为它是重复的之前(有许多问题要求如何分割长字符串而不破坏单词)请记住我的问题有点不同:顺序并不重要我应该使用单词以便使用每一行越多越好.

我有一组无序的单词,我希望在不使用超过253个字符的情况下将它们组合起来.

def compose(words):
    result = " ".join(words)
    if len(result) > 253:
        pass # this should not happen!
    return result
Run Code Online (Sandbox Code Playgroud)

我的问题是我想尽可能地填补这条线.例如:

words = "a bc def ghil mno pq r st uv"
limit = 5 # max 5 characters

# This is good because it's the shortest possible list,
#   but I don't know how could I get it
# Note: order is not important
good = ["a def", "bc pq", "ghil", "mno r", "st uv"]

# This is bad …
Run Code Online (Sandbox Code Playgroud)

python string algorithm

5
推荐指数
2
解决办法
725
查看次数

标签 统计

python ×2

algorithm ×1

regex ×1

string ×1