对字符串的长度求和并连接,使得字符串的长度满足条件

发布于 2025-01-16 06:15:17 字数 1017 浏览 1 评论 0原文

假设我有一个字符串列表,我想用它们创建一条推文。例如:

# should be two tweets total 
this_list = ["Today is monday tomorrow is tuesday the next" ,"will be friday and after that", "saturday followed by sunday", "this month is march the next", "april after that may followed", "by june then july then we", "have august then", "september and october finishing", "the year with november and december" ]

我想要的输出将与此类似(当然存储在列表中):

tweet 1:  'Today is monday tomorrow is tuesday the next will be friday and after that saturday followed by sunday this month is march the next april'
tweet 2:  'after that may followed by june then july then we have august then september and october finishing the year with november and december'

我尝试使用 while 循环来实现此目的,但我不确定循环是否正常工作...

out = [] # empty list
s = 0 # counter
tweet = "" # string to add too 
while s < 140:
    for x in this_list:
        tweet += x
        s += len(x)
    out.append(tweet)
print(len(out))


Suppose i have a list of strings and I wanted to make a tweet out of them. for example:

# should be two tweets total 
this_list = ["Today is monday tomorrow is tuesday the next" ,"will be friday and after that", "saturday followed by sunday", "this month is march the next", "april after that may followed", "by june then july then we", "have august then", "september and october finishing", "the year with november and december" ]

my desired output would be similar to this (stored in a list of course):

tweet 1:  'Today is monday tomorrow is tuesday the next will be friday and after that saturday followed by sunday this month is march the next april'
tweet 2:  'after that may followed by june then july then we have august then september and october finishing the year with november and december'

I have tried to use a while loop to achieve this but I am not sure the loop is working correctly...

out = [] # empty list
s = 0 # counter
tweet = "" # string to add too 
while s < 140:
    for x in this_list:
        tweet += x
        s += len(x)
    out.append(tweet)
print(len(out))


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔一刀 2025-01-23 06:15:17

我最终使用了一个包含长度和文本的元组列表;然后对长度求和,直到满足 140 个字符。

lot = [(len(i),i) for i in output] # list of tuples (length, txt)
out = [] # store tweets
y=0 # count len of tweets
t= '' # empty tweet 
for l, txt in lot:
    y += l
    t += txt
    if y >= 140:
        t= ''
        y = 0
    else:
        if y>= 119: # I want the 'full tweet' not building blocks
            print(t)
            out.append(t)

len(out) #number of tweets 


I ended up using a list of tuples that held the length and text; then summing through the lengths until 140 characters met.

lot = [(len(i),i) for i in output] # list of tuples (length, txt)
out = [] # store tweets
y=0 # count len of tweets
t= '' # empty tweet 
for l, txt in lot:
    y += l
    t += txt
    if y >= 140:
        t= ''
        y = 0
    else:
        if y>= 119: # I want the 'full tweet' not building blocks
            print(t)
            out.append(t)

len(out) #number of tweets 


浪漫之都 2025-01-23 06:15:17

这不是最Pythonic 的方法。但它非常清晰,并以可见的方式分解了逻辑:

mylist = ['Today', 'is', 'monday', 'tomorrow', 'is', 'tuesday', 'the', 'next', 'will', 'be', 'friday', 'and', 'after', 'that', 'saturday', 'followed', 'by', 'sunday', 'this', 'month', 'is', 'march', 'the', 'next', 'april', 'after', 'that', 'may', 'followed', 'by', 'june', 'then', 'july', 'then', 'we', 'have', 'august', 'then', 'september', 'and', 'october', 'finishing', 'the', 'year', 'with', 'november', 'and', 'december']

position = 0 #We keep track of the position, because we might reach the end of the list without meeting the 140 chars criteria
n_chars = 0 #character counting variable
list_of_tweets = []
iter_string = ''
for word in mylist:

    if(n_chars < 140): #We keep adding words to our sentence as max number of chars is not reached
        iter_string = iter_string + word + ' '
        n_chars = n_chars + len(word) + 1 #+1 because of the space we add
        if(n_chars >= 140):
                    list_of_tweets.append(iter_string[:-1]) #We delete the last char as it is a space
                    iter_string = ''
                    n_chars = 0

    if(position == len(mylist)-1):
        list_of_tweets.append(iter_string[:-1])

    position = position + 1 #We are advancing to the next word in the iteration

print(list_of_tweets)

This is not the most pythonic way of doing this. But it is pretty clear and breaks down the logic in a visible manner:

mylist = ['Today', 'is', 'monday', 'tomorrow', 'is', 'tuesday', 'the', 'next', 'will', 'be', 'friday', 'and', 'after', 'that', 'saturday', 'followed', 'by', 'sunday', 'this', 'month', 'is', 'march', 'the', 'next', 'april', 'after', 'that', 'may', 'followed', 'by', 'june', 'then', 'july', 'then', 'we', 'have', 'august', 'then', 'september', 'and', 'october', 'finishing', 'the', 'year', 'with', 'november', 'and', 'december']

position = 0 #We keep track of the position, because we might reach the end of the list without meeting the 140 chars criteria
n_chars = 0 #character counting variable
list_of_tweets = []
iter_string = ''
for word in mylist:

    if(n_chars < 140): #We keep adding words to our sentence as max number of chars is not reached
        iter_string = iter_string + word + ' '
        n_chars = n_chars + len(word) + 1 #+1 because of the space we add
        if(n_chars >= 140):
                    list_of_tweets.append(iter_string[:-1]) #We delete the last char as it is a space
                    iter_string = ''
                    n_chars = 0

    if(position == len(mylist)-1):
        list_of_tweets.append(iter_string[:-1])

    position = position + 1 #We are advancing to the next word in the iteration

print(list_of_tweets)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文