根据输入值在 Python 中制作字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14147369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make a dictionary in Python from input values
提问by Beta Projects
Seems simple, yet elusive, want to build a dict from input of [key,value] pairs separated by a space using just one Python statement. This is what I have so far:
看起来很简单,但又难以捉摸,想要仅使用一个 Python 语句从由空格分隔的 [key,value] 对的输入构建一个 dict。这是我到目前为止:
d={}
n = 3
d = [ map(str,raw_input().split()) for x in range(n)]
print d
Input:
输入:
A1023 CRT
A1029 Regulator
A1030 Therm
Desired Output:
期望输出:
{'A1023': 'CRT', 'A1029': 'Regulator', 'A1030': 'Therm'}
采纳答案by Ashwini Chaudhary
using str.splitines()and str.split():
使用str.splitines()和str.split():
In [126]: strs="""A1023 CRT
.....: A1029 Regulator
.....: A1030 Therm"""
In [127]: dict(x.split() for x in strs.splitlines())
Out[127]: {'A1023': 'CRT', 'A1029': 'Regulator', 'A1030': 'Therm'}
str.splitlines([keepends]) -> list of strings
Return a list of the lines in S, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.
str.split([sep [,maxsplit]]) -> list of strings
Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.
str.splitlines([keepends]) -> 字符串列表
返回 S 中的行列表,在行边界处断开。结果列表中不包含换行符,除非给出了 keepends 并且为 true。
str.split([sep [,maxsplit]]) -> 字符串列表
返回字符串 S 中的单词列表,使用 sep 作为分隔符字符串。如果给出了 maxsplit,则最多完成 maxsplit 次分割。如果未指定 sep 或为 None,则任何空白字符串都是分隔符,并从结果中删除空字符串。
回答by piokuc
Assuming you have the text in variable s:
假设您在变量中有文本s:
dict(map(lambda l: l.split(), s.splitlines()))
回答by Beta Projects
This is what we ended up using:
这是我们最终使用的:
n = 3
d = dict(raw_input().split() for _ in range(n))
print d
Input:
输入:
A1023 CRT
A1029 Regulator
A1030 Therm
Output:
输出:
{'A1023': 'CRT', 'A1029': 'Regulator', 'A1030': 'Therm'}
回答by Aakash Balani
for i in range(n):
data = input().split(' ')
d[data[0]] = data[1]
for keys,values in d.items():
print(keys)
print(values)
回答by Atonu Ghosh
n = int(input()) #n is the number of items you want to enter
d ={}
for i in range(n):
text = input().split() #split the input text based on space & store in the list 'text'
d[text[0]] = text[1] #assign the 1st item to key and 2nd item to value of the dictionary
print(d)
INPUT:
输入:
3
A1023 CRT
A1029 Regulator
A1030 Therm
NOTE: I have added an extra line for each input for getting each input on individual lines on this site. As placing without an extra line creates a single line.
注意:我为每个输入添加了一个额外的行,以便在本网站的各个行上获取每个输入。因为没有额外的线放置会创建一条线。
OUTPUT:
输出:
{'A1023': 'CRT', 'A1029': 'Regulator', 'A1030': 'Therm'}
回答by rashedcs
n=int(input())
pair = dict()
for i in range(0,n):
word = input().split()
key = word[0]
value = word[1]
pair[key]=value
print(pair)
回答by sagar salunkhe
record = int(input("Enter the student record need to add :"))
stud_data={}
for i in range(0,record):
Name = input("Enter the student name :").split()
Age = input("Enter the {} age :".format(Name))
Grade = input("Enter the {} grade :".format(Name)).split()
Nam_key = Name[0]
Age_value = Age[0]
Grade_value = Grade[0]
stud_data[Nam_key] = {Age_value,Grade_value}
print(stud_data)
回答by Surendra Kumar Aratikatla
n = int(input("enter a n value:"))
d = {}
for i in range(n):
keys = input() # here i have taken keys as strings
values = int(input()) # here i have taken values as integers
d[keys] = values
print(d)
回答by Pragati Shandilya
I have taken an empty dictionary as f and updated the values in f as name,password or balance are keys.
我将一个空字典作为 f 并更新了 f 中的值,因为名称、密码或余额是键。
f=dict()
f.update(name=input(),password=input(),balance=input())
print(f)

