Python 列表及其位置

Python 列表及其位置,python,list,Python,List,我正在创建一个代码,在其中输入一个句子,它将句子中的每个单词分割成一个单独的成员。当我输入一个单词,并再次输入时,这两个单词将在下面具有相同的单词。比如说, This is a This is a 1 2 3 1 2 3 问题是,我的代码做的不同,例如 This This Is Is 1 1 2 3 如果它做了两次,它将永远继续计算 这是我的密码: sent = input("Enter your sentence:").lower() sentlist

我正在创建一个代码,在其中输入一个句子,它将句子中的每个单词分割成一个单独的成员。当我输入一个单词,并再次输入时,这两个单词将在下面具有相同的单词。比如说,

This is a This is a 
 1   2  3  1    2 3
问题是,我的代码做的不同,例如

This This Is Is 
 1     1   2  3
如果它做了两次,它将永远继续计算

这是我的密码:

sent = input("Enter your sentence:").lower()
sentlist = sent.split()
print (sent)

for i in sentlist:
    if sentlist.index(i) in newlist:
        newlist.append(sentlist.index(i))
    else:
        newlist.append(int(count))
        count = count + 1
intlist = [x+1 for x in newlist]
print (intlist)

希望有人能帮助我。

您可以使用字典来存储每个唯一的单词以及它在输入列表中第一次出现的位置/索引。然后可以使用该词典有效地获取每个单词的位置/索引,然后将其附加到
新列表中

sentlist = input("Enter your sentence:").lower().split()

newlist = []
position = 0
d = {}

for word in sentlist:
    if word not in d:
        position += 1
        d[word] = position
    newlist.append(d[word])

print(newlist)
输出

对于输入
这是一个这是一个

[1, 2, 3, 1, 2, 3] 对于
您好,这是一个这是一个再见

[1, 2, 3, 4, 2, 3, 4, 5] 为什么您的解决方案返回错误的结果 当你遇到一个新词(在else情况下)时,你会在列表中添加一个新的数字,但这个数字不是该词的索引,而是列表中已有的单词数

第二次输入(“这是”)时将执行的步骤

  • i==“this”
    的索引为0,而0不在新列表中,因此它将0追加到列表中,并将
    计数增加到1
  • i==“this”
    也有索引0,它已经在newlist中,所以它会在另一次添加它
  • i==“is”
    具有不在newlist中的索引2,因此它将
    count
    附加到newlist(其值为1)并将其递增
  • i==“is”
    具有不在newlist中的索引2,因此它将
    count
    附加到newlist(其值为2)并将其递增
  • 为了解决这个问题,也可以使用
    sentlist.index()
    而不是
    count

    更好的解决方案 为了改善这一点,请使用字典而不是列表。这允许您有效地存储遇到的单词的位置并生成结果

    例如:

    sentence = input("Enter your sentence:").lower()
    words = sentence.split()
    
    positions = {}   # positions for each word
    count = 0        # number of unique words
    poslist = []     # the positions for the given sentence
    
    for word in words:
        if word in positions:
            pos = positions[word] # get the position of word from the dictionary
            poslist.append(pos)
        else:
            # new word encountered
            count += 1
            positions[word] = count # store the position for word in the dictionary
            poslist.append(count)
    
    print(poslist)
    

    恐怕我得不到你的要求。一个输入和输出示例会很好,或者尝试更好地解释问题。我刚刚用测试输入运行了您的代码,得到了所需的结果。⑨好吧,因为这不是一个真正的好方法,所以需要一些额外的行。你可以用这些单词创建一个字典,检查是否存在增加计数,或者将其添加到字典中。使用枚举而不是索引!谢谢,非常有帮助。谢谢你的解释。 [1, 1, 1, 1, 1]
    sentence = input("Enter your sentence:").lower()
    words = sentence.split()
    
    positions = {}   # positions for each word
    count = 0        # number of unique words
    poslist = []     # the positions for the given sentence
    
    for word in words:
        if word in positions:
            pos = positions[word] # get the position of word from the dictionary
            poslist.append(pos)
        else:
            # new word encountered
            count += 1
            positions[word] = count # store the position for word in the dictionary
            poslist.append(count)
    
    print(poslist)