在python 3中从用户输入创建2个列表

时间:2014-08-18 12:50:58

标签: list python-3.x

我正在编写一个需要从用户读取字符串的程序,并从输入中创建2个单词列表。一个包含至少一个大写字母的单词和一个不包含任何大写字母的单词。 使用单个for循环打印出带有大写字母的单词,然后是单词中没有大写字母的单词,每行一个单词。

到目前为止,我已经得到了这个:

"""Simple program to list the words of a string."""

s = input("Enter your string: ")
words = s.strip().split()
for word in words:
    print(word)
words = sorted([i for i in words if i[0].isupper()]) + sorted([i for i in words if i[0].islower()])enter code here

问题是我不知道如何将其拆分为2个单独的列表(列出每个列表的条件)。任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

您的要求很奇怪,第一部分表示您希望有两个列表,第二部分表示您应该使用for循环打印不带大写字符的字符串,然后打印其他字符。

如果第一个版本是正确的,请检查重复的问题,否则如果您想使用单个循环打印没有大写字母的单词,那么您可以执行其他操作:

s = input("Enter your string: ")
lowers = []

for word in s.strip().split():
    if not word.islower():
        print(word)
    else:
        lowers.append(word)

print('\n'.join(lowers))