简体   繁体   English

将列表(数组值)转换为整数,然后将数组保存的输入作为转换后的整数而不是单词打印出来

[英]Converting list (array values) into integers and then print out the input which the array holds as the converted integers not words

    firstsentence = input('Enter a sentence: ')
    firstsentence = firstsentence.lower()
    words = firstsentence.split(' ')
    my_list = []
    my_list.append(words)
    for (i, firstsentence) in enumerate(my_list):
        if (aword == my_list): #This is where i get confused
            print(str(i+1)+ ' position')
            print (my_list)

So what i am try to do is ask the user for an input - "firstsentence". 因此,我试图做的就是要求用户输入-“ firstsentence”。 I will then try to split the inputted sentence into words and store them in the array (list) my_list. 然后,我将尝试将输入的句子拆分为单词并将其存储在数组(列表)my_list中。 I am getting stuck on how to convert the seperated words into numbers (their positions in the sentence) and then printing out the input("firstsentence") as the integer version. 我对如何将分隔的单词转换为数字(它们在句子中的位置),然后将输入(“ firstsentence”)打印为整数版本感到困惑。 'aword' is what i used to try and identify if the word was repeated in the sentence. 我曾经尝试使用“ aword”来识别单词是否在句子中重复。 This is quite confusing and here is an example :- "I like to do coding because I like it and i like it a lot" There are 15 words here. 这很令人困惑,下面是一个示例:-“我喜欢编码,因为我喜欢并且非常喜欢它”这里有15个单词。 Of these 15, only 10 are different. 在这15个中,只有10个不同。 So the repeated words MUST be put into the same integer (even though their positions are different their actual word is the same) value in the array. 因此,重复单词必须在数组中放入相同的整数(即使它们的位置不同,它们的实际单词也相同)值。 The code i want to print out should look like this for the sentence above:- 1 2 3 4 5 6 1 2 7 8 1 2 7 9 10. I am really confused as to how to store the separated words as different values (integers) and then especially on how to store the same repeating words as one value, and then printing the whole thing out. 我要打印的代码应该类似于上面的句子:-1 2 3 4 5 6 1 2 7 8 1 2 7 9 10.对于将分隔的单词存储为不同的值(整数,我真的很困惑) ),然后特别讨论如何将相同的重复单词存储为一个值,然后将整个内容打印出来。 Thanks to those who help. 感谢那些帮助。 Would be much appreciated :-). 将不胜感激:-)。

My answer is deliberately incomplete. 我的回答是故意不完整的。 It's clear you're new at this, and part of being new is discovering how code works. 显然,您是新手,而新手的一部分就是发现代码的工作方式。 But you're lacking in some basic concepts of what to do with lists in python, and this should point you in the right direction. 但是您缺少一些处理python中列表的基本概念,这应该为您指明正确的方向。

firstsentence = input('Enter a sentence: ')
firstsentence = firstsentence.lower()
words = firstsentence.split(' ')
my_list = []
for word in words:
    print(word in my_list) 
    # You can do something more useful than print here, right?
    # Maybe add the specific word to my_list...

for word in words:
    print(my_list.index(word))
    # This gives you the index, but you need to figure out how to output it the way you want
    # Note that indices in python start at 0 while your sample output starts
    # at 1.  So maybe do some addition somewhere in here.

So, what's happening here? 那么,这是怎么回事?

We're doing two passes. 我们正在做两遍。 Generally, you'll get very far in programming if you can decompose a problem into discrete steps. 通常,如果您可以将问题分解为离散的步骤,则会在编程上走得很远。 In this case, there are two steps to your problem - find the index of the first occurrence of each word, and then output the right indices. 在这种情况下,有两个步骤可以解决您的问题-找到每个单词首次出现的索引,然后输出正确的索引。

In the first pass, you'll create a list of unique words, where their ordering reflects their ordering in the sentence. 在第一遍中,您将创建一个唯一单词的列表,其顺序反映了它们在句子中的顺序。

In the second pass, you'll go through the original sentence and look up the place where it first occurred, and format that the way you need. 在第二遍中,您将遍历原始句子并查找其首次出现的位置,并按照所需的方式进行格式化。

If you're really attentive, you'll realize: you could easily do this in one pass. 如果您真的很专心,您将意识到: 您可以轻松地一口气做到这一点。 What does the second pass require? 第二遍需要什么? Just that the word you're looking for be in my_list . 只是您要查找的单词在my_list As long as you meet that requirement, you could combine the two loops. 只要满足该要求,就可以将两个循环组合在一起。 This is a good thing to do - it may not matter when you're looking at 20 words, but what if you were looking at 20,000,000,000 words? 这是一件好事-当您查看20个单词时可能并不重要,但是如果您查看20,000,000,000个单词怎么办? You'd really only want one loop. 您真的只想要一个循环。 But take small steps. 但是要采取一些小步骤。 Start by figuring out how to do it with two loops, and then maybe move on to putting it all in one. 首先弄清楚如何通过两个循环执行此操作,然后也许将其全部合并为一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM