난 로렌아줌마가 이스마프 에 구축할 수 있다. I want to 하나님께용서를 위한 사용자 입력. 나는 그의 입력입니다 따라 몇 가지 정보를 검색하는 &solarisdvd 이스마프. 이 경우 사용자가 키를 입력하면 해당 값을 읽어들입니다 이스마프 하고 싶다.
이 기능을 어떻게 구현합니까 파이썬?
HashMap<String,String> streetno=new HashMap<String,String>();
streetno.put("1", "Sachin Tendulkar");
streetno.put("2", "Dravid");
streetno.put("3","Sehwag");
streetno.put("4","Laxman");
streetno.put("5","Kohli")
[파이썬 사전] [1] 은 내장현 유형: 敲阀汽俊 汲摹窍妨绰 key-value 쌍.
streetno = {"1": "Sachin Tendulkar", "2": "Dravid", "3": "Sehwag", "4": "Laxman", "5": "Kohli"}
물론 name. domain. 딕트 키워드:
streetno = dict({"1": "Sachin Tendulkar", "2": "Dravid"})
또는:
streetno = {}
streetno["1"] = "Sachin Tendulkar"
당신이 원하는 모든 (당시 처음 묻는 질문) 는 힌트입니다. S # 39, here& 힌트: 파이썬, [사전] [1] 에서 사용할 수 있습니다.
[1]: http://docs.python.org/tutorial/datastructures.html # 사전
따라 예:
streetno = {"1": "Sachine Tendulkar",
"2": "Dravid",
"3": "Sehwag",
"4": "Laxman",
"5": "Kohli" }
마치 그러하매 액세스할 수 있습니다.
sachine = streetno["1"]
어떤 가치가 있다. 모든 주요 유형으로 비사양 변경 가능 데이터를 사용할 수 있습니다. 즉, 튜플 부울입니다 또는 문자열 핵심적인 역할을 한 사용할 수 있습니다.
streetno = {} #create a dictionary called streetno
streetno["1"] = "Sachin Tendulkar" #assign value to key "1"
사용법:
"1" in streetno #check if key "1" is in streetno
streetno["1"] #get the value from key "1"
자세한 내용은 문서를 참조하십시오 내장현 방법 및 드릴링됩니다. (예: # 39, re 멋지구리해요 they& 매우 흔하게 파이썬 프로그램 (놀라지 않게).
[1]: http://docs.python.org/tutorial/datastructures.html # 사전
다음은 해시 맵 구현을 통해 파이썬 단순한 해시보다 맵은 대한 고정된 크기의 16. 쉽게 변경할 수 있습니다. 이 코드는 레이싱 범위 밖에 있다.
class Node:
def __init__(self, key, value):
self.key = key
self.value = value
self.next = None
class HashMap:
def __init__(self):
self.store = [None for _ in range(16)]
def get(self, key):
index = hash(key) & 15
if self.store[index] is None:
return None
n = self.store[index]
while True:
if n.key == key:
return n.value
else:
if n.next:
n = n.next
else:
return None
def put(self, key, value):
nd = Node(key, value)
index = hash(key) & 15
n = self.store[index]
if n is None:
self.store[index] = nd
else:
if n.key == key:
n.value = value
else:
while n.next:
if n.key == key:
n.value = value
return
else:
n = n.next
n.next = nd
hm = HashMap()
hm.put("1", "sachin")
hm.put("2", "sehwag")
hm.put("3", "ganguly")
hm.put("4", "srinath")
hm.put("5", "kumble")
hm.put("6", "dhoni")
hm.put("7", "kohli")
hm.put("8", "pandya")
hm.put("9", "rohit")
hm.put("10", "dhawan")
hm.put("11", "shastri")
hm.put("12", "manjarekar")
hm.put("13", "gupta")
hm.put("14", "agarkar")
hm.put("15", "nehra")
hm.put("16", "gawaskar")
hm.put("17", "vengsarkar")
print(hm.get("1"))
print(hm.get("2"))
print(hm.get("3"))
print(hm.get("4"))
print(hm.get("5"))
print(hm.get("6"))
print(hm.get("7"))
print(hm.get("8"))
print(hm.get("9"))
print(hm.get("10"))
print(hm.get("11"))
print(hm.get("12"))
print(hm.get("13"))
print(hm.get("14"))
print(hm.get("15"))
print(hm.get("16"))
print(hm.get("17"))
출력:
sachin
sehwag
ganguly
srinath
kumble
dhoni
kohli
pandya
rohit
dhawan
shastri
manjarekar
gupta
agarkar
nehra
gawaskar
vengsarkar
class HashMap:
def __init__(self):
self.size = 64
self.map = [None] * self.size
def _get_hash(self, key):
hash = 0
for char in str(key):
hash += ord(char)
return hash % self.size
def add(self, key, value):
key_hash = self._get_hash(key)
key_value = [key, value]
if self.map[key_hash] is None:
self.map[key_hash] = list([key_value])
return True
else:
for pair in self.map[key_hash]:
if pair[0] == key:
pair[1] = value
return True
else:
self.map[key_hash].append(list([key_value]))
return True
def get(self, key):
key_hash = self._get_hash(key)
if self.map[key_hash] is not None:
for pair in self.map[key_hash]:
if pair[0] == key:
return pair[1]
return None
def delete(self, key):
key_hash = self._get_hash(key)
if self.map[key_hash] is None :
return False
for i in range(0, len(self.map[key_hash])):
if self.map[key_hash][i][0] == key:
self.map[key_hash].pop(i)
return True
def print(self):
print('---Phonebook---')
for item in self.map:
if item is not None:
print(str(item))
h = HashMap()
파이썬 [엇회전식] (https://docs.python.org/3/library/collections.html # 엇회전식 객체에는) 도 좋은 옵션이 이 경우:
"' 엇회전식 임포트합니다 충스러웠으니 컬렉션
엇회전식 = 엇회전식 ([", 사친 Tendulkar" ";;, 사친 Tendulkar" ", 다른 things",])
보기인쇄 (엇회전식) "'
이 목록에 있는 각 요소의 카운트입니다 딕트 반환합니다. "' 엇회전식 ({& # 39, 사친 Tendulkar& # 39;: # 39, & # 39, 다른 things&, 2. 1}) "'
Python 에서 사용하는 것과 사전이란다
아주 중요한 유형으로 python and 자주 사용됩니다.
통해 손쉽게 만들 수 있습니다
"'파이썬 name = {} "'
사전을 많이 방법: "'파이썬
> > >;; 이름 [# 39, & # 39 first&;] # 39, & # 39 John&, = > > >;; 이름 [# 39, & # 39 second&;] # 39, & # 39 Doe&, = > > >;; 이름
> > >;; 이름 [# 39, & # 39 list&;] = [& # 39, list& # 39, & # 39, & # 39, # 39 dict& inside& # 39;;;] > > >;; 이름 [1] = 1 > > >;; 이름
"' 순서를 딕트 영향을 줄 수 없습니다.