Discuss / Python / 我来了

我来了

Topic source

L0standfound

#1 Created at ... [Delete] [Delete and Lock User]

Dict:

!/usr/bin/env python3

-- coding: utf-8 --

d = { 'Michael': 95, 'Bob': 75, 'Tracy': 85, (1, 2, 3):77 } print('d[\'Michael\'] =', d['Michael']) print('d[\'Bob\'] =', d['Bob']) print('d[\'Tracy\'] =', d['Tracy']) print('d[(1, 2, 3)] =', d[(1, 2, 3)]) print('d.get(\'Thomas\', -1) =', d.get('Thomas', -1))

Set:

!/usr/bin/env python3

-- coding: utf-8 --

s1 = set([1, 1, 2, 2, 3, 3, (1, 2, 3)]) print(s1) s2 = set([2, 3, 4]) print(s1 & s2) print(s1 | s2)

另一个(1, [2, 3])由于元祖中有一个可变的list元素所以不能作为dict和set的key.


  • 1

Reply