如何拆分用户输入,使其在数组中占据两个索引位置? (Python)
How do I split up a user input so that it takes up two index places in an array? (Python)
我希望能够接受用户输入的几个用逗号分隔的不同单词,然后将它们添加到一个数组中,以便他们输入的每个单词都占用不同的索引值。
这是我为此使用的功能:
array = ["cat","dog","house","car"]
print(array)
def append():#accepts a user input and adds it to the array
item = input("What would you like to append: ")
item = item.lower()
array.append(item)#appends the item variable delcared in the above statement
print ("The list is now: ",array)
目前,这是通过获取一个用户输入,将其更改为小写,将其添加到数组并打印出来来实现的。我想要它,以便用户可以输入:mouse、horse、mountain,程序会将这三个项目分别添加到数组中。目前它将它们全部加在一起——这是应该的。我已经尝试过 split() 命令,但是似乎所做的只是将它们作为一件事添加,然后将它们放在方括号中。
任何帮助都会很棒。干杯
你可以使用split
func:
lst = string.split(", ")
它 returns 一个字符串列表。
输入:
Apple, Facebook, Amazon
lst:
["Apple", "Facebook", "Amazon"]
更新
获得列表后,您可以将它们添加到主列表(无论您如何称呼):
array += lst
现在 array
包含这些:
["cat","dog","house","car","Apple", "Facebook", "Amazon"]
像这样
lst = ["cat","dog","house","car"]
def append():
item = input("What would you like to append: ")
lst.extend(item.lower().split(','))
print(f'Before: {lst}')
append()
print(f'After: {lst}')
You are thinking in the right direction, as one answer pointed out
that you can use .split() method I will try to explain it a little
more. You can create an item list to store the list of strings to be
appended. Something like this
```python
item = input("What would you like to append: ")
item_list=item.split(", ")
```
Now you can use for loop to append this list your original array.
Something like this..
```python
for item in item_list:
item=item.lower()
array.append(item)
```
Whole code for reference..
```python
array = ["cat","dog","house","car"]
print(array)
def append():#accepts a user input and adds it to the array
item = input("What would you like to append: ")
item_list=item.split(", ")
for item in item_list:
item = item.lower()
array.append(item) #appends the item variable
print ("The list is now: ",array)```
我希望能够接受用户输入的几个用逗号分隔的不同单词,然后将它们添加到一个数组中,以便他们输入的每个单词都占用不同的索引值。 这是我为此使用的功能:
array = ["cat","dog","house","car"]
print(array)
def append():#accepts a user input and adds it to the array
item = input("What would you like to append: ")
item = item.lower()
array.append(item)#appends the item variable delcared in the above statement
print ("The list is now: ",array)
目前,这是通过获取一个用户输入,将其更改为小写,将其添加到数组并打印出来来实现的。我想要它,以便用户可以输入:mouse、horse、mountain,程序会将这三个项目分别添加到数组中。目前它将它们全部加在一起——这是应该的。我已经尝试过 split() 命令,但是似乎所做的只是将它们作为一件事添加,然后将它们放在方括号中。
任何帮助都会很棒。干杯
你可以使用split
func:
lst = string.split(", ")
它 returns 一个字符串列表。
输入:
Apple, Facebook, Amazon
lst:
["Apple", "Facebook", "Amazon"]
更新
获得列表后,您可以将它们添加到主列表(无论您如何称呼):
array += lst
现在 array
包含这些:
["cat","dog","house","car","Apple", "Facebook", "Amazon"]
像这样
lst = ["cat","dog","house","car"]
def append():
item = input("What would you like to append: ")
lst.extend(item.lower().split(','))
print(f'Before: {lst}')
append()
print(f'After: {lst}')
You are thinking in the right direction, as one answer pointed out that you can use .split() method I will try to explain it a little more. You can create an item list to store the list of strings to be appended. Something like this
```python
item = input("What would you like to append: ")
item_list=item.split(", ")
```
Now you can use for loop to append this list your original array. Something like this..
```python
for item in item_list:
item=item.lower()
array.append(item)
```
Whole code for reference..
```python
array = ["cat","dog","house","car"]
print(array)
def append():#accepts a user input and adds it to the array
item = input("What would you like to append: ")
item_list=item.split(", ")
for item in item_list:
item = item.lower()
array.append(item) #appends the item variable
print ("The list is now: ",array)```