我遇到问题,我只想获取每次运行时最多包含30个字符的字符串。每次从列表中获取30个字符的字符串
import random
test=["test1 up to 30 characters",
"test2 passing 30 characters easy",
"test3 up to 30 characters",
"test4 passing 30 characters easy"]
random.shuffle(test)
if len(test[0]) <= 30:
print test[0]
elif len(test[0]) >= 30:
print "Shit"
print len(test[0])
修改:**************************************** ************************************
在测试中,我将有一个变量会是这个样子:
import random
test=["test1 up to "+variable+" characters",
"test2 passing "+variable+" characters easy",
"test3 up to "+variable+" characters",
"test4 passing "+variable+" characters easy"]
random.shuffle(test)
if len(test[0]) <= 30:
print test[0]
elif len(test[0]) >= 30:
print "Shit"
print len(test[0])
该变量将是5个字符最多我不知道的混合数,比方说10
我想要的程序从列表中选择总共包含最多30个字符的字符串。您的解决方案只打印出包含字符串的字符长达30个字符,就是这样。我希望它返回并在列表中找到每次会为每个变量提供总共30个字符的字符串。我真的希望你明白,我无法理解我自己。
已修改******************************************** ***********************************
好吧,我这样做:
import random
test=["test1 up to 30 characters",
"test2 passing 30 characters easy",
"test3 up to 30 characters",
"test4 passing 30 characters easy"]
random.shuffle(test)
print [x for x in test[0:1] if len(x) < 30]
据我想要做什么,但仍然得到空白,像空[]
你的编辑都没有区别 - 你已经被赋予仍然可以工作,你仍然落得一个字符串列表的解决方案,其中每一个仍然具有长度(其中将包括变量的长度) –
我明白了,我给它一个测试...非常感谢。 –