我一直在使用列表和数组。在这个任务中,我需要返回(按顺序)句子,而不是按顺序返回句子,我需要返回单词所在的位置,按顺序这里是我目前的代码:
sentence = "Hello my name is Daniel, hello Daniel"
sentence = sentence.upper()
sentence = sentence.split()
list = sentence
返回:
['HELLO', 'MY', 'NAME', 'IS', 'DANIEL,', 'HELLO', 'DANIEL']
但希望的结果是:
[0, 1, 2, 3, 4, 0, 4]
有没有人知道如何通过添加更多代码从给定代码中获得此结果?
答案 0 :(得分:2)
您似乎想要42.0923267, -80.1041138
42.0840190, -80.0974670
42.0840500, -80.0967051
42.0935450, -80.1033530
42.0855000, -80.1021200
42.0916309, -80.0989128
42.0846050, -80.1010570
42.0846050, -80.1010570
42.0843432, -80.0993665
42.0924650, -80.1074910
而不是Daniel
,因此您应该替换逗号或删除它。然后使用index方法返回obj出现的列表中的最低索引。
Daniel,
它返回
sentence = "Hello my name is Daniel, hello Daniel"
sentence = sentence.replace(',','').upper()
sentence = sentence.split()
print [sentence.index(i) for i in sentence]