如何将用户输入内容添加到列表和字典中,并在python中用分号(;;;)分隔?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将用户输入内容添加到列表和字典中,并在python中用分号(;;;)分隔?相关的知识,希望对你有一定的参考价值。

myDict = 'Name': ['Daniel', 'Melissa', 'Vicky', 'Winona'],
         'Tribe': ['Bataknese', 'Javanese', 'Bataknese', 'Javanese'],
         'Town': ['Indramayu', 'Jakarta', 'Jakarta', 'Batam'],
         'Username': ['dan.iel', 'melissaaaa', 'vic.ky', 'weenonuhh']

myList = [['Daniel', 'Bataknese', 'Indramayu', 'dan.iel'],
         ['Melissa', 'Javanese', 'Jakarta', 'melissaaaa'],
         ['Vicky', 'Bataknese', 'Jakarta', 'vic.ky'],
         ['Winona', 'Javanese', 'Batam', 'weenonuhh']]

如何使函数向字典和列表添加/添加更多值?

def add(something):
    ....

 add('Clara Angela;;;Balinese;;;Bali;;;clara123')

然后,myDict和我的列表应如下所示:

myDict = 'Name': ['Daniel', 'Melissa', 'Vicky', 'Winona','Clara Angela'],
         'Tribe': ['Bataknese', 'Javanese', 'Bataknese', 'Javanese','Balinese'],
         'Town': ['Indramayu', 'Jakarta', 'Jakarta', 'Batam','Bali'],
         'Username': ['dan.iel', 'melissaaaa', 'vic.ky', 'weenonuhh','clara123']

myList = [['Daniel', 'Bataknese', 'Indramayu', 'dan.iel'],
         ['Melissa', 'Javanese', 'Jakarta', 'melissaaaa'],
         ['Vicky', 'Bataknese', 'Jakarta', 'vic.ky'],
         ['Winona', 'Javanese', 'Batam', 'weenonuhh'],
         ['Clara Angela', 'Balinese', 'Bali', 'clara123']]
答案

这里是将执行此操作的代码:

myDict = 'Name': ['Daniel', 'Melissa', 'Vicky', 'Winona'],
         'Tribe': ['Bataknese', 'Javanese', 'Bataknese', 'Javanese'],
         'Town': ['Indramayu', 'Jakarta', 'Jakarta', 'Batam'],
         'Username': ['dan.iel', 'melissaaaa', 'vic.ky', 'weenonuhh']

def add ( Name, Tribe, Town, Username):
    myDict["Name"].append(Name)
    myDict["Tribe"].append(Tribe)
    myDict["Town"].append(Town)
    myDict["Username"].append(Username)

add("Clara Angela","Balinese","Bali","clara123")

print(myDict)

输出将是:

'Name': ['Daniel', 'Melissa', 'Vicky', 'Winona', 'Clara Angela'],
 'Tribe': ['Bataknese', 'Javanese', 'Bataknese', 'Javanese', 'Balinese'],
 'Town': ['Indramayu', 'Jakarta', 'Jakarta', 'Batam', 'Bali'],
 'Username': ['dan.iel', 'melissaaaa', 'vic.ky', 'weenonuhh', 'clara123']
另一答案

您可以做:

myDict = 'Name': ['Daniel', 'Melissa', 'Vicky', 'Winona'],
         'Tribe': ['Bataknese', 'Javanese', 'Bataknese', 'Javanese'],
         'Town': ['Indramayu', 'Jakarta', 'Jakarta', 'Batam'],
         'Username': ['dan.iel', 'melissaaaa', 'vic.ky', 'weenonuhh']
myList = [['Daniel', 'Bataknese', 'Indramayu', 'dan.iel'],
         ['Melissa', 'Javanese', 'Jakarta', 'melissaaaa'],
         ['Vicky', 'Bataknese', 'Jakarta', 'vic.ky'],
         ['Winona', 'Javanese', 'Batam', 'weenonuhh']]

def add(my_string):
    splitted = s.split(';;;')
    myList.append(splitted)
    for i, item in enumerate(['Name', 'Tribe', 'Town', 'Username']):
        myDict[item].append(splitted[i])

add('Clara Angela;;;Balinese;;;Bali;;;clara123')

以上是关于如何将用户输入内容添加到列表和字典中,并在python中用分号(;;;)分隔?的主要内容,如果未能解决你的问题,请参考以下文章

如何将它们添加到字典中,并在python中每5个循环后使活动变量成为结尾?

Rails 6将输入添加到页面中的列表

python 从列表中取出一个字典元素

python如何将列表中的元素添加进字典

如何发送数组列表以解析和检索它并在 UItableView 中显示它?

如何将复选框添加到添加的列表中?