将列表中的字符串拆分为Python中的单词

时间:2017-01-23 12:08:56

标签: python string split

我是Python的新手,我需要在Python中编写一个代码来读取文本文件,然后将其中的每个单词拆分,排序并打印出来。

这是我写的代码:

var telemetryClient = new TelemetryClient();
var connectionString = connectionStringSettings.ConnectionString;
var mongoUrl = new MongoUrl(connectionString);
var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl);

mongoClientSettings.ClusterConfigurator = clusterConfigurator =>
{
    clusterConfigurator.Subscribe<CommandSucceededEvent>(e =>
    {
        telemetryClient.TrackDependency("MongoDB", e.CommandName, DateTime.Now.Subtract(e.Duration), e.Duration, true);
    });

    clusterConfigurator.Subscribe<CommandFailedEvent>(e =>
    {
        telemetryClient.TrackDependency("MongoDB", $"{e.CommandName} - {e.ToString()}", DateTime.Now.Subtract(e.Duration), e.Duration, false);
    });
};

var mongoClient = new MongoClient(mongoClientSettings);

这是我的输出 -

  <'出现白皙的阳光,杀死羡慕的月亮','但柔和的光线透过窗户打破','它是东方,朱丽叶是太阳','谁已经病了,脸色苍白,有了grienter code herew' ,   'with','yonder']

然而,当我尝试将fname = raw_input("Enter file name: ") fh = open(fname) lst = list() words = list() for line in fh: line = line.strip() line.split() lst.append(line) lst.sort() print lst 分开时说

  

列表对象没有属性拆分

请帮忙!

3 个答案:

答案 0 :(得分:2)

您应该extend使用拆分行的新列表,而不是在追加后尝试拆分字符串:

for line in fh:    
    line = line.strip()
    lst.extend(line.split())

答案 1 :(得分:0)

问题是split()不会神奇地改变分割为list的字符串。你必须使用返回值。

for line in fh:    
    # line.split()  # expression has has no effect
    line = line.split()  # statement does
    # lst += line  # shortcut for loop underneath
    for token in line:
        lst = lst + [token]
        lst += [token]

以上是使用嵌套循环并避免appendextend的解决方案。整个逐行分割和排序可以非常简洁地完成,但是,使用嵌套的生成器表达式:

print sorted(word for line in fh for word in line.strip().split())

答案 2 :(得分:0)

你可以这样做:

fname = raw_input("Enter file name: ")
fh = open(fname, "r")
lines = list()
words = list()
for line in fh:    
    # get an array of words for this line
    words = line.split()
    for w in words: 
       lines.append(w)

lines.sort()
print lines

为避免重复:

no_dups_list = list()
for w in lines:
   if w not in no_dups_list:
       no_dups_list.append(w)