如果有一个值,那么它应该是具有空值的键作为列表;如果有多个值,则第一个值将是键,其余的将是字典中的值列表。
>Ex: Column
ram
sneha, vijay, harish
deva
babu, dominic
Expected o/p:
{
'ram':[],
'sneha': ['vijay', 'harish'],
'deva' : [],
'babu' : ['dominic']
}
答案 0 :(得分:0)
假设您的列数据采用我描述的格式,以下解决方案将提供预期的输出结果
int i=1;
printf("%d, %d and %d\n", i++, i++, i--);
答案 1 :(得分:0)
您只需运行一次循环即可一次选择一行,并将该行的第0个索引设置为键,并将其后的值设置为列表中的值。
可以这样做
74| <form name="mobget">
75| <select id="monster">
>> 76| <% for (var i = 0; i < mobs.length; ++i) { %>
77| <option name="mobs" value="<%= mobs[i].id %>"><%= mobs[i].name %></option>
78| <% } %>
79| </select>
mobs is not defined
at eval (eval at compile (/home/kogadmin/www/node_modules/ejs/lib/ejs.js:618:12), <anonymous>:62:28)
at returnedFn (/home/kogadmin/www/node_modules/ejs/lib/ejs.js:653:17)
at tryHandleCache (/home/kogadmin/www/node_modules/ejs/lib/ejs.js:251:36)
at View.exports.renderFile [as engine] (/home/kogadmin/www/node_modules/ejs/lib/ejs.js:482:10)
at View.render (/home/kogadmin/www/node_modules/express/lib/view.js:135:8)
at tryRender (/home/kogadmin/www/node_modules/express/lib/application.js:640:10)
at Function.render (/home/kogadmin/www/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/home/kogadmin/www/node_modules/express/lib/response.js:1008:7)
at /home/kogadmin/www/server.js:112:9
at Layer.handle [as handle_request] (/home/kogadmin/www/node_modules/express/lib/router/layer.js:95:5)
答案 2 :(得分:0)
您可以这样做:
columns_list = [['ram'], ['sneha', 'vijay', 'harish'],
['deva'], ['babu', 'dominic']]
result = {item[0]: item[1:] for item in columns_list}
print(result)
# {'ram': [], 'sneha': ['vijay', 'harish'], 'deva': [], 'babu': ['dominic']}
如果输入为string
,则可以这样:
rows = '''ram
sneha, vijay, harish
deva
babu, dominic'''
columns_list = [row.split(',') for row in rows.split("\n")]
# if columns have space at begin and end then `strip` them
columns_list = tuple(map(lambda cols: [c.strip() for c in cols], columns_list))
print(columns_list)
# (['ram'], ['sneha', 'vijay', 'harish'], ['deva'], ['babu', 'dominic'])
result = {item[0]: item[1:] for item in columns_list}
print(result)
# {'ram': [], 'sneha': ['vijay', 'harish'], 'deva': [], 'babu': ['dominic']}
答案 3 :(得分:0)
如果您的源数据是字符串对象,那么
data = ["ram", "sneha, vijay, harish", "deva", "babu, dominic"]
res = {}
for i in data:
val = i.split(",")
res[val[0]]= list(map(str.strip, val[1:]))
print(res)
输出:
{'babu': ['dominic'], 'deva': [], 'ram': [], 'sneha': ['vijay', 'harish']}