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

时间:2019-11-14 12:27:30

标签: python list dictionary

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']]

3 个答案:

答案 0 :(得分:1)

假设顺序正确;使用str.split并将其附加到myList,然后循环遍历myDict.values(),将每个值附加({zip将很方便)

def add(data):
    data = data.split(';;;')
    myList.append(data)
    for l, v in zip(myDict.values(), data):
        l.append(v)

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

结果:

myDict

{'Name': ['Daniel', 'Melissa', 'Vicky', 'Winona', 'Clara Angela'],
 'Town': ['Indramayu', 'Jakarta', 'Jakarta', 'Batam', 'Bali'],
 'Tribe': ['Bataknese', 'Javanese', 'Bataknese', 'Javanese', 'Balinese'],
 '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']]

如果您不使用按字典顺序排序的Python 3.X,则operator.itemgetter是您的下一个优点:

get_lists = itemgetter('Name', 'Tribe', 'Town', 'Username')
for l, v in zip(get_lists(myDict), data):
    ...

答案 1 :(得分:-1)

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

@exception_safe()
def function():

输出将是:

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']}

答案 2 :(得分:-1)

您可以这样做:

so_function(NA_character_) # should pass
so_function(NA_integer_) # should pass
so_function(c(NA, NA)) # should fail
so_function(c("A", "B")) # should pass
so_function(c(1, 2, 3)) # should fail