Erinevus lehekülje "JavaPython:Sõnastikud" redaktsioonide vahel

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
(Uus lehekülg: '{{JavaPython-sisukord}}')
 
1. rida: 1. rida:
 
{{JavaPython-sisukord}}
 
{{JavaPython-sisukord}}
 +
 +
Pythoni sõnastikud on sarnased Java "map"-idele.
 +
 +
== Näide ==
 +
 +
{|
 +
!Java
 +
!Python
 +
|-
 +
|<syntaxhighlight lang="java" line="1" >
 +
HashMap<String, String> phoneBook =
 +
                        new HashMap<String, String>();
 +
phoneBook.put("Mike", "555-1111");
 +
phoneBook.put("Lucy", "555-2222");
 +
phoneBook.put("Jack", "555-3333");
 +
 +
//iterate over HashMap
 +
Map<String, String> map = new HashMap<String, String>();
 +
for (Map.Entry<String, String> entry : map.entrySet()) {
 +
    System.out.println("Key = " + entry.getKey() +
 +
      ", Value = " + entry.getValue());
 +
}
 +
 +
//get key value
 +
phoneBook.get("Mike");
 +
 +
//get all key
 +
Set keys = phoneBook.keySet();
 +
 +
//get number of elements
 +
phoneBook.size();
 +
 +
//delete all elements
 +
phoneBook.clear();
 +
 +
//delete an element
 +
phoneBook.remove("Lucy");
 +
</syntaxhighlight>
 +
|<syntaxhighlight lang="python" line="2" >
 +
#create an empty dictionary
 +
phoneBook = {}
 +
phoneBook = {"Mike":"555-1111",
 +
            "Lucy":"555-2222",
 +
            "Jack":"555-3333"}
 +
 +
#iterate over dictionary
 +
for key in phoneBook:
 +
    print(key, phoneBook[key])
 +
 +
#add an element
 +
phoneBook["Mary"] = "555-6666"
 +
 +
#delete an element
 +
del phoneBook["Mike"]
 +
 +
#get number of elements
 +
count = len(phoneBook)
 +
 +
#can have different types
 +
phoneBook["Susan"] = (1,2,3,4)
 +
 +
#return all keys
 +
print phoneBook.keys()
 +
 +
#delete all the elements
 +
phoneBook.clear()
 +
</syntaxhighlight>
 +
|}

Redaktsioon: 30. jaanuar 2016, kell 07:42

Java vs Python

Pythoni sõnastikud on sarnased Java "map"-idele.

Näide

Java Python
<syntaxhighlight lang="java" line="1" >

HashMap<String, String> phoneBook =

                       new HashMap<String, String>();

phoneBook.put("Mike", "555-1111"); phoneBook.put("Lucy", "555-2222"); phoneBook.put("Jack", "555-3333");

//iterate over HashMap Map<String, String> map = new HashMap<String, String>(); for (Map.Entry<String, String> entry : map.entrySet()) {

   System.out.println("Key = " + entry.getKey() +
     ", Value = " + entry.getValue());

}

//get key value phoneBook.get("Mike");

//get all key Set keys = phoneBook.keySet();

//get number of elements phoneBook.size();

//delete all elements phoneBook.clear();

//delete an element phoneBook.remove("Lucy"); </syntaxhighlight>

<syntaxhighlight lang="python" line="2" >
  1. create an empty dictionary

phoneBook = {} phoneBook = {"Mike":"555-1111",

            "Lucy":"555-2222", 
            "Jack":"555-3333"}

  1. iterate over dictionary

for key in phoneBook:

   print(key, phoneBook[key])

  1. add an element

phoneBook["Mary"] = "555-6666"

  1. delete an element

del phoneBook["Mike"]

  1. get number of elements

count = len(phoneBook)

  1. can have different types

phoneBook["Susan"] = (1,2,3,4)

  1. return all keys

print phoneBook.keys()

  1. delete all the elements

phoneBook.clear() </syntaxhighlight>