欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关于Python学习之 列表与字典

程序员文章站 2022-08-20 16:06:20
列表 列表是Python中最具灵活性的有序集合对象类型。 # 列表迭代和解析 >>> res = [c*4 for c in 'Spam'] >>> res ['SSSS', 'pppp', 'aaaa', 'mmmm' >>> res = [] >>> for c in 'Spam': ... r ......

  列表

  列表是python中最具灵活性的有序集合对象类型。

  # 列表迭代和解析

  >>> res = [c*4 for c in 'spam']

  >>> res

  ['ssss', 'pppp', 'aaaa', 'mmmm'

  >>> res = []

  >>> for c in 'spam':

  ... res.append(c*4)

  ...

  >>> res

  ['ssss', 'pppp', 'aaaa', 'mmmm']

  >>> list(map(abs,[-1,-2,0,1,2]))

  [1, 2, 0, 1, 2]

  # 一般操作

  >>> l = [5,3,6,2,8]

  >>> sorted(l)

  [2, 3, 5, 6, 8]

  >>> l

  [5, 3, 6, 2, 8]

  >>> l.sort()

  >>> l

  [2, 3, 5, 6, 8]

  >>> l.insert(0,1)

  >>> l

  [1, 2, 3, 5, 6, 8]

  >>> l.reverse()

  >>> l

  [8, 6, 5, 3, 2, 1]

  >>>

  >>> del l[0]

  >>> l

  [6, 5, 3, 2, 1]

  >>> l.pop()

  1

  >>> l

  [6, 5, 3, 2]

  >>> l.remove(6)

  >>> l

  [5, 3, 2]

  '''

  原处修改列表:因为python只处理对象引用,所以需要将原处修改一个对象与生成的一个新对象区分开来。

  因为在原处修改一个对象时,可能同时会影响一个以上指向它的引用。

  '''

  # 其他

  >>> l = ['already','got','one']

  >>> l

  ['already', 'got', 'one']

  >>> l[1:]=[]

  >>> l

  ['already']

  >>> l[0]=[]

  >>> l

  [[]]

  字典

  如果把列表看成是有序的对象集合,字典可以当成是无序的集合。主要区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。

  >>> d = {'food':{'ham':1,'egg':2}}

  >>> d.get('food')

  {'ham': 1, 'egg': 2}

  >>> d2 = {'a':1,'b':2}

  >>> d.update(d2)

  >>> d

  {'food': {'ham': 1, 'egg': 2}, 'a': 1, 'b': 2}

  >>> d.pop('b')

  2

  >>> len(d)

  2

  >>> d

  {'food': {'ham': 1, 'egg': 2}, 'a': 1}

  >>> del d['a']

  >>> d

  {'food': {'ham': 1, 'egg': 2}}

  >>> d = {x:x*2 for x in range(10)}

  >>> d

  {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}

  >>> d = {'spam':2,'ham':1,'eggs':3}

  >>> d['spam']

  2

  >>> d

  {'spam': 2, 'ham': 1, 'eggs': 3}

  >>> len(d)

  3

  >>> 'ham' in d

  true

  >>> list(d.keys())

  ['spam', 'ham', 'eggs']

  >>> list(d.values())

  [2, 1, 3]

  >>> d

  {'spam': 2, 'ham': 1, 'eggs': 3}

  >>> d['ham']=['grill','bake','fry']

  >>> d无锡看男科医院哪家好 https://yyk.familydoctor.com.cn/20612/

  {'spam': 2, 'ham': ['grill', 'bake', 'fry'], 'eggs': 3}

  >>> del d['eggs']

  >>> d

  {'spam': 2, 'ham': ['grill', 'bake', 'fry']}

  >>> d['brunch'] = 'bacon'

  >>> d

  {'spam': 2, 'ham': ['grill', 'bake', 'fry'], 'brunch': 'bacon'}

  >>> list(d.items())

  [('spam', 2), ('ham', ['grill', 'bake', 'fry']), ('brunch', 'bacon')]

  >>> d2 = {'toast':4,'muffin':5}

  >>> d.update(d2)

  >>> d

  {'spam': 2, 'ham': ['grill', 'bake', 'fry'], 'brunch': 'bacon', 'toast': 4, 'muffin': 5}

  >>> table = {'python':'guido van rossum',

  ... 'perl':'larry wall',

  ... 'tcl':'john ousterhout'}

  >>>

  >>> language = 'python'

  >>> creator = table[language]

  >>> creator

  'guido van rossum'

  >>> for lang in table:

  ... print(lang,'\t',table[lang])

  ...

  python guido van rossum

  perl larry wall

  tcl john ousterhout

  # 三种方法来避免missing-key错误

  ... if (2,3,6) in matrix:

  ... print(matrix[(2,3,6)])

  ... else:

  ... print(0)

  ...

  0

  >>>

  >>>

  >>> try:

  ... print(matrix[(2,3,6)])

  ... except keyerror:

  ... print(0)

  ...

  0

  >>>

  >>>

  >>> matrix.get((2,3,4),0)

  98

  >>> matrix.get((2,3,6),0)

  0