Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 允许用户输入单词的列表位置,以返回列表中的第二个和第三个元素_Python_Python 3.x_Duplicates_Nested Lists_Python 3.3 - Fatal编程技术网

Python 允许用户输入单词的列表位置,以返回列表中的第二个和第三个元素

Python 允许用户输入单词的列表位置,以返回列表中的第二个和第三个元素,python,python-3.x,duplicates,nested-lists,python-3.3,Python,Python 3.x,Duplicates,Nested Lists,Python 3.3,我需要有人帮助我编写代码,而不使用外部库和异常计数器 lineList = [['Cat', 'c', 1, x],['Cat', 'a', 2, x],['Cat', 't', 3, x],['Bat', 'b', 1, 3],['Bat', 'b', 1, 2],['Mat', 'm', 1, 1],['Fat', 'f', 1, 13]] 2D列表中出现2次以上的单词将显示在数字列表中 例如: 如何允许用户通过输入列表位置编号来选择单词?例如,如果用户输入1,它将返回嵌套列表中Cat的第

我需要有人帮助我编写代码,而不使用外部库和异常计数器

lineList = [['Cat', 'c', 1, x],['Cat', 'a', 2, x],['Cat', 't', 3, x],['Bat', 'b', 1, 3],['Bat', 'b', 1, 2],['Mat', 'm', 1, 1],['Fat', 'f', 1, 13]]
2D列表中出现2次以上的单词将显示在数字列表中

例如:

如何允许用户通过输入列表位置编号来选择单词?例如,如果用户输入1,它将返回嵌套列表中Cat的第二个和第三个元素:

c = 1, a = 2, t = 3
我是Python的初学者,因此不确定如何处理此问题。

您可以使用、、和:

输出:

1. Cat
2. Bat
对于位于特定位置的字符串:

n = int(input('position: '))   #   1-indexed

print('{0}. {1}'.format(n, filtered[n - 1][0]))   #   0-indexed (hence, n - 1)
使用a数数单词,然后使用
枚举
为列表编号:

from collections import Counter

lineList = [['Cat', 'c', 1, 2],['Cat', 'c', 1, 3],['Bat', 'b', 1, 4],['Bat', 'b', 1, 3],['Bat', 'b', 1, 2],['Mat', 'm', 1, 1],['Fat', 'f', 1, 13]]

counts = Counter(word for word, *other_stuff in lineList)

filtered = [word for word, count in counts.items() if count >= 2]

for number, word in enumerate(filtered, start=1):
    print("{: >2}.".format(number), word)
印刷品

 1. Cat
 2. Bat
如果无法导入
计数器
,则可以非常轻松地编写基本替换:

def Counter(seq):
    d = {}
    for item in seq:
        d[item] = d.get(item, 0) + 1
    return d
计数器
有更多功能,但这就是我们使用的全部功能)

然后,您可以使用以下选项选择一个单词:

def choose(filtered):
    while True:
        choice = input("Select a word: ")
        try:
            choice = int(choice)
            return filtered[choice-1]
        except ValueError, IndexError:
            print("Please enter a number on the list")

就在这里,只需检查嵌套列表中第二项的值是否大于1

list1 = [['Cat', 2], ['Bat', 3], ['Fat', 1], ['Mat', 1]]

index = 1
for i in range(len(list1)):
    if list1[i][1] > 1:
        print (str(index)+ ". " + str(list1[i][0]))
        index += 1
这张照片是:

1. Cat
2. Bat

你把描述改了一点,所以我把答案改写得更合适

lineList = [['Cat', 'c', 1, 'x'],['Cat', 'a', 2, 'x'],['Cat', 't', 3, 'x'],['Bat', 'b', 1, 3],['Bat', 'b', 1, 2],['Mat', 'm', 1, 1],['Fat', 'f', 1, 13]]


#First we create a dictionary with the repeating words in the list you gave
nameList = []    
frequencyDict = {}
for i in range(len(lineList)):

    if lineList[i][0] in frequencyDict.keys():
        frequencyDict[lineList[i][0]] += 1
    else:
        frequencyDict[lineList[i][0]] = 1

    #this will give you a list with the order
    #it will be useful to get the indices of the repeating word later
    nameList.append(lineList[i][0]) 


# Printing a list of values when if they are repeated
index = 1
repeats = []
for i in frequencyDict.keys():

    if frequencyDict[i] > 1: #This if statement checks if it was repeated or not

        print(str(index)+ ". " + i)
        repeats.append(i) # we also crete yet another list so the user can call it with his input later
        index += 1



x = (int(input("Which item on the list would you like to know more information about: \n")) -1) #note that we are subtracting one from the input so it matches the index of the list

# Here I am trying to get all the indices that start with the word that user replied
indicesList = []
for i in range(len(nameList)):
    if nameList[i] == repeats[x]:
        indicesList.append(i)

# Here I am printing the value that is in the index 1 and 2 of the nested list in Linelist
for i in range(len(indicesList)):
    print(str(lineList[indicesList[i]][1]) +
        " = " +
        str(lineList[indicesList[i]][2]))

我如何允许用户通过输入列表位置号来选择一个单词,例如,如果用户输入1,它将返回Cat及其嵌套列表中的相应值,例如,“c”,1,2:)@gstar我刚刚用这个问题的解决方案更新了我的答案。@gstar您必须在我答案的第一个代码中定义它,
filtered=[p for p in word\u计数,如果p[1]>1]
。真是太棒了!唯一的问题是当我输入1时,它只返回cat。有没有一种方法可以让它从行列表返回所有其他元素,例如c、1和2,例如['Cat',c',1,2]@gstar如果用户输入
1
,您希望得到什么结果?因此,for循环中if语句中的值'i'是原始行列表的索引。使用那个索引号来获取原始列表的信息。我在不同的评论中回答了这个问题,因为现在问题的表述有点不同
1. Cat
2. Bat
lineList = [['Cat', 'c', 1, 'x'],['Cat', 'a', 2, 'x'],['Cat', 't', 3, 'x'],['Bat', 'b', 1, 3],['Bat', 'b', 1, 2],['Mat', 'm', 1, 1],['Fat', 'f', 1, 13]]


#First we create a dictionary with the repeating words in the list you gave
nameList = []    
frequencyDict = {}
for i in range(len(lineList)):

    if lineList[i][0] in frequencyDict.keys():
        frequencyDict[lineList[i][0]] += 1
    else:
        frequencyDict[lineList[i][0]] = 1

    #this will give you a list with the order
    #it will be useful to get the indices of the repeating word later
    nameList.append(lineList[i][0]) 


# Printing a list of values when if they are repeated
index = 1
repeats = []
for i in frequencyDict.keys():

    if frequencyDict[i] > 1: #This if statement checks if it was repeated or not

        print(str(index)+ ". " + i)
        repeats.append(i) # we also crete yet another list so the user can call it with his input later
        index += 1



x = (int(input("Which item on the list would you like to know more information about: \n")) -1) #note that we are subtracting one from the input so it matches the index of the list

# Here I am trying to get all the indices that start with the word that user replied
indicesList = []
for i in range(len(nameList)):
    if nameList[i] == repeats[x]:
        indicesList.append(i)

# Here I am printing the value that is in the index 1 and 2 of the nested list in Linelist
for i in range(len(indicesList)):
    print(str(lineList[indicesList[i]][1]) +
        " = " +
        str(lineList[indicesList[i]][2]))