追加条件
Append on condition
我打算编写一个程序,将一些行转换成一行,但不幸的是,我在附加不常见的元素时遇到了问题。这是我到目前为止所做的:
List_of_sentences = []
for line in range(int(input())):
List_of_sentences.append(input().strip().split())
Sample_sentence = input().split()
Final_list = []
for lists in List_of_sentences:
for elements in Sample_sentence:
if elements in lists:
Final_list.append(lists[0])
else:
Final_list.append(elements)
print(' '.join(Final_list))
例如,考虑这两个列表:
List_of_sentences = [['man', 'I', 'je', 'ich'], ['kheili', 'very', 'très', 'sehr'], ['alaghemand', 'interested', 'intéressé', 'interessiert'], ['barnamenevisi', 'programming', 'laprogrammation', 'Programmierung']]
Sample_sentence = ['I', 'am', 'very', 'interested', 'in', 'programming']
它必须 return 这个:
man am kheili alaghemand in barnamenevisi
而是:
man kheili alaghemand barnamenevisi
我的问题是其他部分
非常感谢任何帮助
输出是这样的,因为您正在遍历每个列表,对于每个列表,您正在遍历示例句子中的每个元素,因此,首先,它将搜索示例句子中的任何元素是否匹配是否第一个列表(“I”将匹配),然后它将插入列表的第一个元素,即最终列表中的“man”。
对于第二个列表“非常”将匹配,因此,它将在最终列表中插入第二个列表的第一个元素,即“kheili”。
因此,最终列表将是这样的:["man", "kheili" .... ],这就是你得到的。
这解释了您遇到的问题。
你的循环似乎有点混乱。应该是这样的顺序
List_of_sentences = []
for line in range(int(input())):
List_of_sentences.append(input().strip().split())
Sample_sentence = input().split()
Final_list = []
for elements in Sample_sentence: #this for loop should be first
var = None
for lists in List_of_sentences: # this for loop should be second
if elements in lists:
#Final_list.append(lists[0])
var = lists[0]
break
Final_list.append(var if var else elements)
print(' '.join(Final_list))
这是我尝试过的并且有效的方法:
List_of_sentences = [
['man', 'I', 'je', 'ich'],
['kheili', 'very', 'très', 'sehr'],
['alaghemand', 'interested', 'intéressé', 'interessiert'],
['barnamenevisi', 'programming', 'laprogrammation', 'Programmierung']
]
Sample_sentence = ['I', 'am', 'very', 'interested', 'in', 'programming']
Final_list = []
for elements in Sample_sentence:
found = False
for lists in List_of_sentences:
if elements in lists:
Final_list.append(lists[0])
found = True
if not found:
Final_list.append(elements)
print(' '.join(Final_list))
您的代码的问题在于,如果找不到元素,它总是会添加一个元素,而不是添加一次然后继续下一个元素
您可以尝试这样的操作:
for elements in Sample_sentence: # since you need to check for elements in the sample sentence in all lists, this should be the outer loop
present = False
for lists in List_of_sentences:
if elements in lists:
Final_list.append(lists[0])
present = True
if not present:
Final_list.append(elements)
这对我有用
for word in Sample_sentence:
append = False
for list in List_of_sentences:
for translation in list:
if (translation == word):
Final_list.append(list[0])
append = True
if (not append):
Final_list.append(word)
print(' '.join(Final_list))
我打算编写一个程序,将一些行转换成一行,但不幸的是,我在附加不常见的元素时遇到了问题。这是我到目前为止所做的:
List_of_sentences = []
for line in range(int(input())):
List_of_sentences.append(input().strip().split())
Sample_sentence = input().split()
Final_list = []
for lists in List_of_sentences:
for elements in Sample_sentence:
if elements in lists:
Final_list.append(lists[0])
else:
Final_list.append(elements)
print(' '.join(Final_list))
例如,考虑这两个列表:
List_of_sentences = [['man', 'I', 'je', 'ich'], ['kheili', 'very', 'très', 'sehr'], ['alaghemand', 'interested', 'intéressé', 'interessiert'], ['barnamenevisi', 'programming', 'laprogrammation', 'Programmierung']]
Sample_sentence = ['I', 'am', 'very', 'interested', 'in', 'programming']
它必须 return 这个:
man am kheili alaghemand in barnamenevisi
而是:
man kheili alaghemand barnamenevisi
我的问题是其他部分
非常感谢任何帮助
输出是这样的,因为您正在遍历每个列表,对于每个列表,您正在遍历示例句子中的每个元素,因此,首先,它将搜索示例句子中的任何元素是否匹配是否第一个列表(“I”将匹配),然后它将插入列表的第一个元素,即最终列表中的“man”。 对于第二个列表“非常”将匹配,因此,它将在最终列表中插入第二个列表的第一个元素,即“kheili”。 因此,最终列表将是这样的:["man", "kheili" .... ],这就是你得到的。 这解释了您遇到的问题。
你的循环似乎有点混乱。应该是这样的顺序
List_of_sentences = []
for line in range(int(input())):
List_of_sentences.append(input().strip().split())
Sample_sentence = input().split()
Final_list = []
for elements in Sample_sentence: #this for loop should be first
var = None
for lists in List_of_sentences: # this for loop should be second
if elements in lists:
#Final_list.append(lists[0])
var = lists[0]
break
Final_list.append(var if var else elements)
print(' '.join(Final_list))
这是我尝试过的并且有效的方法:
List_of_sentences = [
['man', 'I', 'je', 'ich'],
['kheili', 'very', 'très', 'sehr'],
['alaghemand', 'interested', 'intéressé', 'interessiert'],
['barnamenevisi', 'programming', 'laprogrammation', 'Programmierung']
]
Sample_sentence = ['I', 'am', 'very', 'interested', 'in', 'programming']
Final_list = []
for elements in Sample_sentence:
found = False
for lists in List_of_sentences:
if elements in lists:
Final_list.append(lists[0])
found = True
if not found:
Final_list.append(elements)
print(' '.join(Final_list))
您的代码的问题在于,如果找不到元素,它总是会添加一个元素,而不是添加一次然后继续下一个元素
您可以尝试这样的操作:
for elements in Sample_sentence: # since you need to check for elements in the sample sentence in all lists, this should be the outer loop
present = False
for lists in List_of_sentences:
if elements in lists:
Final_list.append(lists[0])
present = True
if not present:
Final_list.append(elements)
这对我有用
for word in Sample_sentence:
append = False
for list in List_of_sentences:
for translation in list:
if (translation == word):
Final_list.append(list[0])
append = True
if (not append):
Final_list.append(word)
print(' '.join(Final_list))