python 数组 hashmap 基本操作 demo

2017-07-10

30

0

技术:python3.5 数组

运行环境:python3.5 + windows 10

demo功能:提供 数组,hashmap,不变数组的基本操作

源代码

http://git.oschina.net/youlixishi/demo-world/blob/master/src/python/array-dictory/app.py

 

# -*- coding: utf-8 -*-
import sys, json, os, uuid, time

def array_test():
    array1 = [] #初始化空数组
    array2 = [1,2,3,45]#初始化指定数据的数组
    array3 = [1,[1,2,3,4],3,45]#初始化多维数组
    array4 = [i for i in range(10)] #初始化范围数据

    array1.append(1)#数组追加数据
    
    del array4[0]#删除指定元素
    
    arr = array4

    print(array2[-1])#倒数 访问
    
    for x in arr:#遍历
        print(x)

    for index, value in enumerate(arr):#输出下标号 和 数据
        print (index,value)

    ##转化为tuple
    t = tuple(array1)
    print(isinstance(t, tuple))
    
def dic_test():
    dic1 = {}
    dic2 = {'name': 'francis', 'password':'123456'}
    dic3 = {'name':[1,2,3], 'password':{'age':12, 'isok':1}} #初始化复杂字典, 强烈不建议这么保存数据

    dic1['name']='francis'##字典赋值
    dic1['password']='123456'

    del dic1['name']##删除指定键值得 数据
    
    dic = dic1x
    #遍历
    for key in dic:
        is_list = isinstance(dic[key], list)#判断变量类型是否list
        print('key='+key +', value type is list='+ str(is_list))#str() 函数 是字符串 转化函数
        if is_list:
            for x in dic[key]:
                print(str(x)+',')
            continue
        
        is_dic = isinstance(dic[key], dict)#判断变量类型是否是字典
        print('key='+key +', value type is dict='+ str(is_dic))#str() 函数 是字符串 转化函数
        if is_dic:
            for key1 in dic[key]:
                print('key1='+key1+',value='+str(dic[key][key1]))
            continue
        print('value=' + dic[key])
        
        
def tuple_test():#指定长度的数组, 一旦生成长度不能变化
    tuple1 = (1,2,3,4,5)#初始化

    #print(tuple1[-2])#倒数访问
    
    #for x in tuple1:
    #    print(x)

    tuple2 = tuple1[1:3]#这里 1:3 是 i>=1 and i<3 的区间
    for x in tuple2:
        print(x)

    ##转化为list
    t = list(tuple1)
    print(isinstance(t, list))
    
if __name__ == '__main__':
    tuple_test()

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think