简体   繁体   English

以两个包含单词的列表作为输入,以形成包含两个单词的元组,每个列表中的一个单词的起始字母相同

[英]Taking two lists as input that contain words, to form a tuple with two words, one from each list that have the same starting letter of each word

I've to take two lists as an input which contain words. 我必须将包含单词的两个列表作为输入。 Using those words I form a tuple using two words, one from each list that contain the same first letter of each word. 使用这些单词,我使用两个单词组成一个元组,每个单词列表中的每个单词都包含相同的第一个字母。 Then creating a list of those tuples and printing them. 然后创建这些元组的列表并进行打印。

I have a solution, however, I cannot seem to produce the same item twice. 我有一个解决方案,但是,我似乎无法两次生产相同的产品。 Here's an example of what I mean in words. 这是我用语言表达的一个例子。

List A: ['Jack', 'Tim', 'John', 'Ahmed'] 列表A: ['Jack', 'Tim', 'John', 'Ahmed']

List B: ['Julie', 'Tom', 'Henry', 'Harper'] 名单B: ['Julie', 'Tom', 'Henry', 'Harper']

c = input().lower()
d = input().lower()

a = c.split()
b = d.split()

x = []
for i in range(len(a)):
    if a[i][0] == b[i][0]:
        x.append((a[i], b[i]))

print(x)

My Output: [('joy', 'juggle'), ('troy', 'trim')] 我的输出: [('joy', 'juggle'), ('troy', 'trim')]

Expected Output: [('Jack', 'Julie'), ('John', 'Julie'), ('Tim', 'Tom')] 预期的输出: [('Jack', 'Julie'), ('John', 'Julie'), ('Tim', 'Tom')]

I'm quite new to learning the language and wasn't quite able to find any similarities to my previous work to find out how I could reiterate through a / b without reproducing the same output. 我对语言学习很陌生,并且无法找到与以前工作相似之处,无法找到如何在不产生相同输出的情况下通过a / b进行重复的工作。

Use itertools.product to get all the pairs and then filter them out: 使用itertools.product获取所有对,然后过滤掉它们:

In [1]: from itertools import product

In [2]: a =  ['Jack', 'Tim', 'John', 'Ahmed']

In [3]: b = ['Julie', 'Tom', 'Henry', 'Harper']

In [4]: out = [i for i in product(a, b) if i[0][0] == i[1][0]]

In [5]: out
Out[5]: [('Jack', 'Julie'), ('Tim', 'Tom'), ('John', 'Julie')]

With list comprehensions: 具有列表理解:

In [50]: a = ['Jack', 'Tim', 'John', 'Ahmed']

In [51]: b = ['Julie', 'Tom', 'Henry', 'Harper']

In [55]: c = [(x,y) for x in a for y in b if x.lower()[0]==y.lower()[0]]

In [56]: c
Out[56]: [('Jack', 'Julie'), ('Tim', 'Tom'), ('John', 'Julie')]

你可以试试这个

[(x, y) for x in A for y in B if x[0] == y[0]]

暂无
暂无

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

相关问题 从多个列表中获取所有以相同字母开头的单词列表 - Get list of words all starting with the same letter from multiple lists Tweepy:收集两个搜索词列表中至少有 1 个词的推文 - Tweepy: Collect tweets with at least 1 word each of two lists of search words 打印一个字符串,该字符串是通过根据单词的位置获取单词列表中的每个连续字母而形成的? - Printing a string which is formed by taking each successive letter in the list of words based on the position of the word? 如何组合两个列表使列表中的每个元素都有两个值,每个列表一个? - How to combine two lists to make each element of the list have two values, one from each list? 计算列表元组中每个列表中给定单词的出现次数 - Count occurrences of given words per each list in a tuple of lists 从两个列表开始,如何将每个列表中相同索引的元素放入元组列表 - Starting from two lists, how to put the elements of the same index from each list into a list of tuples 获取列表中的单词并在文本文件中搜索它们并计算每个单词的数量 - Taking words that I have in a list and searching for them within a Text File and getting a count for each word 如何从两个列表中删除相同的单词? - How to remove the same words from two lists? 列出 Python 中两个列表中不匹配的单词 - List mismatched words from two lists in Python Python-计算单词列表中的每个字母 - Python- Count each letter in a list of words
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM