为列表中的每个字符串指定一个数字。Python 3.6
我试图编写一个代码,接收输入,将其拆分为一个列表,然后为列表中的每个字符串分配一个数字 例如:如果我的输入是“hello再见hello”,那么它将输出“1121” 这是我迄今为止编写的代码:为列表中的每个字符串指定一个数字。Python 3.6,python,string,python-3.x,Python,String,Python 3.x,我试图编写一个代码,接收输入,将其拆分为一个列表,然后为列表中的每个字符串分配一个数字 例如:如果我的输入是“hello再见hello”,那么它将输出“1121” 这是我迄今为止编写的代码: sentence = input("What is your sentence? ").lower() list_s = str.split(sentence) for i in range(len(list_s)): 使用包含字符串作为键和数字作为值的字典: inp = 'hello hello wor
sentence = input("What is your sentence? ").lower()
list_s = str.split(sentence)
for i in range(len(list_s)):
使用包含字符串作为键和数字作为值的字典:
inp = 'hello hello world hello'
d = {}
idx = 1
for word in inp.lower().split():
# If the word is in the dictionary print the corresponding number
if word in d:
print(d[word], end='')
# Otherwise print the next number and add the number to the dictionary
else:
print(idx, end='')
d[word] = idx
idx += 1
我之所以提出字典,是因为中的操作非常快,需要存储两样东西:字符串和值。使字典成为解决该问题的非常合适的数据结构
如果您喜欢短一点,您可以使用列表理解(使用trincots答案中的len(d)+1
“技巧”):
其中,result
将是一个包含[1,1,2,1]
的列表,您可以使用以下方式打印该列表:
print(''.join(map(str, result)))
或者(感谢Stefan Pochmann的提示):
使用包含字符串作为键和数字作为值的字典:
inp = 'hello hello world hello'
d = {}
idx = 1
for word in inp.lower().split():
# If the word is in the dictionary print the corresponding number
if word in d:
print(d[word], end='')
# Otherwise print the next number and add the number to the dictionary
else:
print(idx, end='')
d[word] = idx
idx += 1
我之所以提出字典,是因为
中的操作非常快,需要存储两样东西:字符串和值。使字典成为解决该问题的非常合适的数据结构
如果您喜欢短一点,您可以使用列表理解(使用trincots答案中的len(d)+1
“技巧”):
其中,result
将是一个包含[1,1,2,1]
的列表,您可以使用以下方式打印该列表:
print(''.join(map(str, result)))
或者(感谢Stefan Pochmann的提示):
作为一名老师,我建议你买一本字典。您可以在添加新词时根据词典的大小生成指定的编号。最终结果可以生成一个包含列表理解的列表:
list_s = str.split("hello hello goodbye hello")
d = {} # a dictionary, which will store for each word a number
for word in list_s:
if not word in d: # first time we see this word?
d[word] = len(d)+1 # assign to this word a unique number
result = [d[word] for word in list_s] # translate each word to its unique number
print (result) # [1, 1, 2, 1]
作为一名老师,我建议你买一本字典。您可以在添加新词时根据词典的大小生成指定的编号。最终结果可以生成一个包含列表理解的列表:
list_s = str.split("hello hello goodbye hello")
d = {} # a dictionary, which will store for each word a number
for word in list_s:
if not word in d: # first time we see this word?
d[word] = len(d)+1 # assign to this word a unique number
result = [d[word] for word in list_s] # translate each word to its unique number
print (result) # [1, 1, 2, 1]
您可以使用集合
:
def get_numbers(s):
s = s.split()
new_s = [a for i, a in enumerate(s) if a not in s[:i]]
return [new_s.index(i)+1 for i in s]
l = ["hello hello goodbye hello", "hello goodbye hello hello"]
final_l = list(map(get_numbers, l))
输出:
[[1, 1, 2, 1], [1, 2, 1, 1]]
您可以使用集合
:
def get_numbers(s):
s = s.split()
new_s = [a for i, a in enumerate(s) if a not in s[:i]]
return [new_s.index(i)+1 for i in s]
l = ["hello hello goodbye hello", "hello goodbye hello hello"]
final_l = list(map(get_numbers, l))
输出:
[[1, 1, 2, 1], [1, 2, 1, 1]]
@cricket_007的可能副本接受的答案给出了[1,1,3,1],而不是此处所需的[1,1,2,1]。可能重复@cricket_007的接受答案给出了[1,1,3,1],而不是此处所需的[1,1,2,1]。我真的不明白这行代码打印(d[word],end='')任何写的方式都是不同的?通常print
包含一个换行符,我用end='
删除了它。既然你在玩end
,你也可以玩sep
:print(*result,sep='')
我真的不明白这行代码打印(d[word],end='')任何书写方式都不同?通常print
包含一个新行,我用end='
删除了它。既然你在玩end
,你也可以玩sep
:print(*result,sep='')
你可以使用d.setdefault(word,len(d)+1)
如果d行中没有单词,则不带。不确定这是否更快,但会更短。:)事实上,这将是很好和简洁的;-)您可以使用d.setdefault(word,len(d)+1)
而不使用,如果d
行中没有单词。不确定这是否更快,但会更短。:)事实上,这将是很好和简洁的;-)