简体   繁体   English

如何在列表中创建子列表?

[英]How to create a sublist inside a list?

I have a question on how to create a sublist in python. 我对如何在python中创建子列表有疑问。

I want to input 3 values and add them in a sublist with a function and call them 3 times. 我想输入3个值,并将它们添加到带有函数的子列表中,并调用它们3次。

But for a reason i cannot create the sublist. 但是由于某种原因,我无法创建子列表。

I want my output to be something like this: [['string1','string2','string3'], [10, 20, 30], [2.7, 5.5, 12.2]] 我希望我的输出是这样的: [[''string1','string2','string3'],[10、20、30],[2.7、5.5、12.2]]

word = input("add text (end stops): ")
list=[]
while word != 'end':
    a = int(input("add int: "))
    b = float(input("add float: "))
    word = input("add text (end stops): ")

def fixlist(lst, st):
    list=[]
    for item in lst:
        list.append(lst)
        for y in item:
            list.append(st[item])
    return [lst, st]


print(fixlist(list, word))
print(fixlist(list, a))
print(fixlist(list, b))

This is my output : 这是我的输出

[[], 'end']
[[], 10]
[[], 5.5]

You have a couple of problems in your code: 您的代码中有几个问题:

list=[]

As Patrick mentioned, do not use keywords as variable names. 如Patrick所言,请勿将关键字用作变量名。

while word != 'end':
    a = int(input("add int: "))
    b = float(input("add float: "))
    word = input("add text (end stops): ")

When you execute this code, you keep geting new input, but you are throwing away your old input. 执行此代码时,您会不断获得新的输入,但是您将丢弃旧的输入。 You need to store your older input if you wish to use it later. 如果希望以后使用,则需要存储较旧的输入。 for example: 例如:

word = input("add text (end stops): ")
all_string_inputs = []
while word != 'end':
    all_string_inputs.append(word)
    word = input("add text (end stops): ")

In the above code i store the older inputs in the all_string_input list. 在上面的代码中,我将较旧的输入存储在all_string_input列表中。
If you wish to enter only 3 values, do not use a while. 如果只想输入3个值,请不要使用一段时间。

def fixlist(lst, st):
    list=[]
    for item in lst:
        list.append(lst)
        for y in item:
            list.append(st[item])
    return [lst, st]

What are you intending to do here? 您打算在这里做什么? in the above code you iterate over the items in lst , for which your input is an empty list. 在上面的代码中,您遍历lst的项目,对于这些项目,您的输入是一个空列表。 And in the end you return your input. 最后,您返回您的输入。 Without changing it. 无需更改。 Also note that st[item] is not defined, as item is not necissarly an int. 另请注意,未定义st[item] ,因为item不一定是int。

If you wish to append to an existing list inside a function, you need to use the following scheme: 如果希望附加到函数内部的现有列表,则需要使用以下方案:

def fixlist(lst, st):
    lst.append(st)

The above will modify the existing list. 以上将修改现有列表。 If you wish to create a new list, you need to return the new list: 如果要创建新列表,则需要返回新列表:

def fixlist(lst, st):
    new_list = lst+[st]
    return new_list

The above will create a new list with lst and st , and returns it. 上面的代码将使用lstst创建一个新列表,并将其返回。

Try to fix your code now, if that still fails, edit the question with your attempts. 立即尝试修复代码,如果仍然无法解决,请尝试修改问题。

print("Please provide data!\n-----------------\n")

data = [
    [], [], []
]

while True:
    line = input("Enter: ")
    line = line.split(" ")

    if line[0].lower() == "end":
        print("\nEntering ended.\n")
        break

    data[0].append(line[0])
    data[1].append(int(line[1]))
    data[2].append(float(line[2]))

print(data)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM