Python基础--列表、元组、字典(个人笔记)

list与tuple

区别

tuple无法进行元组内的修改(可以两个元组拼接),没有append()等函数,相对于list更安全。

相互转化

tuple(seq)            #将列表转换为元组。
list(seq)             #将元组转换为列表。

Python-list

操作符

len(list)
list3=list1+list2
list*4
a in list                      #返回Bool类型
for x in list: print(x)        #迭代
list4=list[:2]          

常用函数

  1. list.append()
  2. list.count(obj)
    统计某个元素在列表中出现的次数。
  3. list.extend(seq)
    在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表,与+不同)
  4. list.index(obj)
    从列表中找出某个值第一个匹配项的索引位置。
  5. list.insert(index, obj)
  6. list.pop(index)
    默认移除列表中的最后一个元素 。
  7. list.remove(obj)
  8. list.reverse()
    反向排列
  9. list.sort([func])
    排序
  10. list.copy()

Python-dict

常用函数

  1. str(dict)
    输出字典,以可打印的字符串表示。
  2. dict.get(key, default=None)
    不同于dict[key],若没有key将返回默认值。
  3. dict.items()
    返回可遍历的(键, 值) 元组数组。
#!/usr/bin/python3

dict = {'Name': 'Runoob', 'Age': 7}
print ("Value : %s" %  dict.items())

#以上实例输出结果为:
#Value : dict_items([('Age', 7), ('Name', 'Runoob')])
  1. dict.keys() & dict.values()
    返回list形式。

你可能感兴趣的:(Python基础--列表、元组、字典(个人笔记))