Python|从给定列表创建三元组的方法
给定一个单词列表,编写一个Python程序从给定列表创建三元组。
例子 :
Input: [‘Geeks’, ‘for’, ‘Geeks’, ‘is’, ‘best’, ‘resource’, ‘for’, ‘study’]
Output:
[[‘Geeks’, ‘for’, ‘Geeks’], [‘for’, ‘Geeks’, ‘is’],
[‘Geeks’, ‘is’, ‘best’], [‘is’, ‘best’, ‘resource’],
[‘best’, ‘resource’, ‘for’], [‘resource’, ‘for’, ‘study’]]
Input: [‘I’, ‘am’, ‘Paras’, ‘Jain’, ‘I’, ‘Study’, ‘From’, ‘GFG’]
Output:
[[‘I’, ‘am’, ‘Paras’], [‘am’, ‘Paras’, ‘Jain’],
[‘Paras’, ‘Jain’, ‘I’], [‘Jain’, ‘I’, ‘Study’],
[‘I’, ‘Study’, ‘From’], [‘Study’, ‘From’, ‘GFG’]]
让我们看看执行此任务的一些方法。
方法 #1:使用列表推导
# Python code to create triplets from list of words.
# List of word initialization
list_of_words = ['I', 'am', 'Paras', 'Jain',
'I', 'Study', 'DS', 'Algo']
# Using list comprehension
List = [list_of_words[i:i + 3]
for i in range(len(list_of_words) - 2)]
# printing list
print(List)
输出:
[['I', 'am', 'Paras'], ['am', 'Paras', 'Jain'],
['Paras', 'Jain', 'I'], ['Jain', 'I', 'Study'],
['I', 'Study', 'DS'], ['Study', 'DS', 'Algo']]
方法#2:使用迭代
# Python code to create triplets from list of words.
# List of word initialization
list_of_words = ['Geeks', 'for', 'Geeks', 'is',
'best', 'resource', 'for', 'study']
# Output list initialization
out = []
# Finding length of list
length = len(list_of_words)
# Using iteration
for z in range(0, length-2):
# Creating a temp list to add 3 words
temp = []
temp.append(list_of_words[z])
temp.append(list_of_words[z + 1])
temp.append(list_of_words[z + 2])
out.append(temp)
# printing output
print(out)
输出:
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
['best', 'resource', 'for'], ['resource', 'for', 'study']]