设置字符串中单词或字符数的限制

时间:2015-08-13 01:32:27

标签: python

说我有一个字符串元素列表

wordlist = ["hi what's up home diddle mc doo", "Oh wise master kakarot", "hello have a da"]

我希望列表中的每个元素最多包含3个字或20个字符。有没有这样做的功能?

3 个答案:

答案 0 :(得分:9)

两者都可以使用列表理解来完成:

1)最多20个字符:

new_list = [item[:20] for item in wordlist]
>>> new_list
["hi what's up home di", 'Oh wise master kakar', 'hello have a da']

2)最多3个字:

new_list = [' '.join(item.split()[:3]) for item in wordlist if item]
>>> new_list
["hi what's up", 'Oh wise master', 'hello have a']

答案 1 :(得分:2)

以下是使用内置映射函数(使用lambda表达式)

的方法

20个字符限制

toResponse started 
javax.ws.rs.WebApplicationException
at    com.sun.jersey.server.impl.uri.rules.TerminatingRule.accept(TerminatingRule.java:66)
atcom.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:724)
toResponse Finished

3个字数限制

wordlist = ["hi what's up home diddle mc doo", "Oh wise master kakarot"]
new_wordlist = map(lambda x: x[:20], wordlist)
>>> ["hi what's up home di", 'Oh wise master kakar']

答案 2 :(得分:1)

您可以使用此代码执行此操作:

import sys

wordCount = int(sys.argv[1])
charCount = int(sys.argv[2])

wordlist = ["hi what's up home diddle mc doo", "Oh wise master kakarot", "hello have a da"]

print(wordlist)
for i in range(len(wordlist)):
    currItem = wordlist[i]
    splitItems = currItem.split(" ")
    length = sum(len(s) for s in splitItems[0:wordCount])
    index = wordCount
    while(length > charCount):
        index -= 1
        length = sum(len(s) for s in splitItems[0:index])
    pass
    wordlist[i] = ' '.join(splitItems[0:index])
pass    
print(wordlist)