wa12rior

19.11.2019 Python

Nov 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. >>> kier = {'krakow':12, 'gliwice':32}
  2. >>> for k,v in kier:
  3.     primn
  4. KeyboardInterrupt
  5. >>> for k,v in kier.items():
  6.     print(k,v)
  7.  
  8.    
  9. krakow 12
  10. gliwice 32
  11. >>> miasta = ['krakow', 'gliwice']
  12. >>> knumery = [12, 32]
  13. >>> for x, y in zip(miasta, knumery):
  14.     print(x, y)
  15.  
  16.    
  17. krakow 12
  18. gliwice 32
  19. >>> a = [23, 54, 213 , 54 , 65]
  20. >>> for x in a:
  21.     print(x)
  22.  
  23.    
  24. 23
  25. 54
  26. 213
  27. 54
  28. 65
  29. >>> for x in reversed(a):
  30.     print(x)
  31.  
  32.    
  33. 65
  34. 54
  35. 213
  36. 54
  37. 23
  38. >>> for x in sorted(a):
  39.     print(x)
  40.  
  41.    
  42. 23
  43. 54
  44. 54
  45. 65
  46. 213
  47.  
  48. ---------------------------------------------
  49.  
  50.  
  51. # pobieramy wyraz, obl liczbe wystapien znakow wyst w lancuchu
  52.  
  53. ##def lett_freq(word):
  54. ##    freqs = dict()
  55. ##
  56. ##    for letter in word:
  57. ##        if not letter in freqs:
  58. ##            freqs[letter] = 0
  59. ##        freqs[letter] += 1
  60. ##    
  61. ##    return freqs
  62.  
  63. def lett_freq(word):
  64.     freqs = dict()
  65.  
  66.     for letter in word:
  67.         if not letter in freqs:
  68.             freqs[letter] = 1
  69.         else:
  70.             freqs[letter] += 1
  71.    
  72.     return freqs
  73.  
  74.  
  75. word = input('Wpisz wyraz')
  76.  
  77. freqs = lett_freq(word)
  78.  
  79. for letter, occurences in freqs.items():
  80.     print(letter, '->', occurences)
  81. -----------------------------------
  82. for letter, occurences in sorted(freqs.items()):
  83.     print(letter, '->', occurences)
  84. # To samo z listami i krotkami na zadanie!!!
  85. -------------------------------------
  86. # Słownik o różnych wartościach, chciałbym mieć funkcje ktora przyjmujac slownik pozwoli
  87. # Zwrocic odwrocony slownik, klucze stana sie wartosciami a wartosci kluczami.
  88.  
  89. def reverse_dict(d):
  90. ##    result = dict()
  91. ##
  92. ##    for x, y in d.items():
  93. ##        result[y] = x
  94. ##    return result
  95.     return { y:x for x, y in d.items() }
  96.  
  97. d = { 2:4, 3:9, 4:16, 5:25 }
  98.  
  99. print(reverse_dict(d))
  100. --------------------------------------------
  101.  
  102. >>> x = set()
  103. >>> x.add('asdas')
  104. >>> x.add(123)
  105. >>> x
  106. {'asdas', 123}
  107. >>> x.add([123])
  108. Traceback (most recent call last):
  109.   File "<pyshell#25>", line 1, in <module>
  110.     x.add([123])
  111. TypeError: unhashable type: 'list'
  112. >>> x.add({'123'})
  113. Traceback (most recent call last):
  114.   File "<pyshell#26>", line 1, in <module>
  115.     x.add({'123'})
  116. TypeError: unhashable type: 'set'
  117. >>> x.add({1:2})
  118. Traceback (most recent call last):
  119.   File "<pyshell#27>", line 1, in <module>
  120.     x.add({1:2})
  121. TypeError: unhashable type: 'dict'
  122. >>> y = dict()
  123. >>> y[{1:2}] = 1
  124. Traceback (most recent call last):
  125.   File "<pyshell#29>", line 1, in <module>
  126.     y[{1:2}] = 1
  127. TypeError: unhashable type: 'dict'
  128. >>> y[1] = {1:2}
  129. >>> y[0] = [1,2]
  130. >>> y[2] = {'1', '2'}
  131. >>> x.add((1,3))
  132. >>> s.add((12, [3]))
  133. Traceback (most recent call last):
  134.   File "<pyshell#34>", line 1, in <module>
  135.     s.add((12, [3]))
  136. NameError: name 's' is not defined
  137. --------------------------------------------------
Add Comment
Please, Sign In to add comment