python数据结构元组与集合

Posted 山外云

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python数据结构元组与集合相关的知识,希望对你有一定的参考价值。

元组

1、()来定义

2、有序,同列表

3、元组一旦创建,不能被修改

注:元组的标识是逗号,不是括号

 

元组的定义
a = (1,2)
type(a)
<class \'tuple\'>

元组的访问

a[1]
2

元组的切片
a[0:1]
(1,)

 

集合

集合:描述元素种类的一种无序序列
a = \'python\' set(a) {\'n\', \'t\', \'p\', \'y\', \'h\', \'o\'}

集合的访问

b
{\'n\', \'t\', \'p\', \'y\', \'h\', \'o\'}

\'n\' in b
True

 

集合的运算

a = [1,2,3,4,5,6,7]
b = [3,4,8,9]

set(a) & set(b)
{3, 4}
set(a) | set(b)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
set(a) - set(b)
{1, 2, 5, 6, 7}

 

 

 

 

#coding:gbk

ip = {\'store_type\': \'rsync\', \'app_name\': \'wechat-module\', \'store_path\': \'rsync://beibei@10.2.130.6/webroot/wechat-module/integrate_170901032328_69/\', \'app_id\': \'b1057b88-cc02-6d85-6fff-53f143baadb0\', \'prop\': \'e9f29d35-37ab-111f-6dc6-7764c69b2c1b\', \'token\': \'token\', \'ip_list\': [\'10.2.129.88\', \'10.2.11.31\', \'10.2.130.34\', \'10.2.129.176\', \'10.2.130.42\', \'10.2.128.158\', \'10.2.131.144\', \'10.2.3.32\', \'10.2.128.140\', \'10.2.131.31\', \'10.2.129.155\', \'10.2.128.179\', \'10.2.131.223\', \'10.2.128.240\', \'10.2.9.155\', \'10.2.128.233\', \'10.2.131.219\', \'10.2.21.35\', \'10.2.128.156\', \'10.2.20.192\', \'10.2.129.66\', \'10.2.21.192\', \'10.2.128.227\', \'10.2.129.137\', \'10.2.131.44\', \'10.2.8.137\', \'10.2.129.168\', \'10.2.129.165\', \'10.2.131.226\', \'10.2.131.15\', \'10.2.128.219\', \'10.2.17.33\', \'10.2.131.7\', \'10.2.128.148\', \'10.2.131.12\', \'10.2.8.143\', \'10.2.9.165\', \'10.2.20.190\', \'10.2.129.71\', \'10.2.131.22\', \'10.2.129.153\', \'10.2.129.138\', \'10.2.9.52\', \'10.2.129.181\', \'10.2.8.131\', \'10.2.129.67\', \'10.2.132.102\', \'10.2.128.215\', \'10.2.129.127\', \'10.2.131.50\', \'10.2.129.166\', \'10.2.129.156\', \'10.2.9.166\', \'10.2.132.141\', \'10.2.129.40\', \'10.2.9.76\', \'10.2.131.129\', \'10.2.129.184\', \'10.2.131.188\', \'10.2.129.148\', \'10.2.131.121\', \'10.2.131.130\', \'10.2.131.46\', \'10.2.129.132\', \'10.2.128.149\', \'10.2.9.34\', \'10.2.129.163\', \'10.2.5.65\', \'10.2.21.30\', \'10.2.129.162\', \'10.2.129.29\', \'10.2.131.182\', \'10.2.129.116\', \'10.2.131.13\', \'10.2.130.54\', \'10.2.130.135\', \'10.2.129.125\', \'10.2.132.103\', \'10.2.132.73\', \'10.2.131.133\', \'10.2.132.84\', \'10.2.131.122\', \'10.2.129.84\', \'10.2.131.9\', \'10.2.9.77\', \'10.2.129.175\', \'10.2.129.167\', \'10.2.9.33\', \'10.2.5.56\', \'10.2.128.216\', \'10.2.131.126\', \'10.2.8.144\', \'10.2.129.134\', \'10.2.5.61\', \'10.2.129.177\', \'10.2.129.126\', \'10.2.17.34\', \'10.2.128.155\', \'10.2.131.29\', \'10.2.130.13\', \'10.2.131.52\', \'10.2.5.62\', \'10.2.130.159\', \'10.2.130.32\', \'10.2.9.75\', \'10.2.130.24\', \'10.2.5.85\', \'10.2.129.198\', \'10.2.130.9\', \'10.2.132.224\', \'10.2.128.238\', \'10.2.130.156\', \'10.2.129.118\', \'10.2.11.58\', \'10.2.129.63\', \'10.2.131.35\', \'10.2.130.132\', \'10.2.131.20\', \'10.2.128.209\', \'10.2.8.141\', \'10.2.128.141\', \'10.2.5.57\', \'10.2.129.128\', \'10.2.5.86\', \'10.2.129.91\', \'10.2.131.227\', \'10.2.132.101\', \'10.2.129.31\', \'10.2.133.37\', \'10.2.130.121\', \'10.2.131.228\', \'10.2.130.46\', \'10.2.9.160\', \'10.2.129.87\', \'10.2.129.77\', \'10.2.130.31\', \'10.2.133.50\', \'10.2.9.162\', \'10.2.17.31\', \'10.2.9.72\', \'10.2.128.144\', \'10.2.9.37\', \'10.2.8.133\', \'10.2.132.98\', \'10.2.132.96\', \'10.2.128.143\', \'10.2.130.123\', \'10.2.133.38\', \'10.2.132.142\', \'10.2.128.139\', \'10.2.131.10\', \'10.2.131.125\', \'10.2.129.136\', \'10.2.129.124\', \'10.2.131.14\', \'10.2.9.55\', \'10.2.131.221\', \'10.2.129.61\', \'10.2.131.229\', \'10.2.129.182\', \'10.2.129.191\', \'10.2.129.41\', \'10.2.129.76\', \'10.2.131.132\', \'10.2.129.147\', \'10.2.128.232\', \'10.2.9.71\', \'10.2.128.213\', \'10.2.130.57\', \'10.2.131.220\', \'10.2.129.146\', \'10.2.132.144\', \'10.2.21.193\', \'10.2.129.174\', \'10.2.130.220\', \'10.2.17.35\', \'10.2.130.62\', \'10.2.132.89\', \'10.2.131.198\', \'10.2.131.230\', \'10.2.9.54\', \'10.2.131.36\', \'10.2.129.62\', \'10.2.130.63\', \'10.2.131.45\', \'10.2.129.192\', \'10.2.11.38\', \'10.2.131.34\', \'10.2.131.143\', \'10.2.8.138\', \'10.2.129.197\', \'10.2.131.48\', \'10.2.129.26\', \'10.2.130.12\', \'10.2.130.56\', \'10.2.9.73\', \'10.2.131.30\', \'10.2.132.99\', \'10.2.9.57\', \'10.2.9.49\', \'10.2.130.25\', \'10.2.131.33\', \'10.2.128.231\', \'10.2.131.11\', \'10.2.131.42\', \'10.2.128.218\', \'10.2.131.127\', \'10.2.131.124\', \'10.2.11.54\', \'10.2.5.84\', \'10.2.129.158\', \'10.2.21.31\', \'10.2.132.87\', \'10.2.131.26\', \'10.2.130.39\', \'10.2.129.70\', \'10.2.130.78\', \'10.2.11.36\', \'10.2.130.58\', \'10.2.131.38\', \'10.2.21.195\', \'10.2.11.56\', \'10.2.9.32\', \'10.2.133.55\', \'10.2.131.24\', \'10.2.129.129\', \'10.2.131.25\', \'10.2.129.161\', \'10.2.130.55\', \'10.2.129.120\', \'10.2.128.147\', \'10.2.131.18\', \'10.2.132.139\', \'10.2.129.143\', \'10.2.132.92\', \'10.2.130.52\', \'10.2.130.218\', \'10.2.8.134\', \'10.2.5.63\', \'10.2.21.34\', \'10.2.129.193\', \'10.2.129.73\', \'10.2.129.160\', \'10.2.132.143\', \'10.2.131.185\', \'10.2.130.131\', \'10.2.130.7\', \'10.2.9.66\', \'10.2.131.123\', \'10.2.11.51\', \'10.2.128.230\', \'10.2.130.26\', \'10.2.129.72\', \'10.2.21.191\', \'10.2.128.157\', \'10.2.129.196\', \'10.2.129.157\', \'10.2.21.194\', \'10.2.9.61\', \'10.2.131.39\', \'10.2.9.51\', \'10.2.130.75\', \'10.2.132.114\', \'10.2.9.161\'], \'env\': \'prod\', \'action\': \'release\', \'sign\': \'sign\', \'idc\': \'hangzhou-xiasha\', \'multi_cluster\': 1}

ips = ip.get(\'ip_list\')
#print(ips)
print(len(ips))


_input = open(r\'C:\\Users\\Martin\\Desktop\\ip.txt\')
content = _input.read()

for item in ips:
    if item not in content:
        print(item)

 

#coding: gbk

_input = [2,3,8,7,11,9,4]

flag=False

for item in _input:
    if flag ==True:
        break
    for i in _input:
        if item == i:
            continue
        if item + i == 6:
            print(\'%s +%s = 6\' %(item,i))
            flag = True

 

以上是关于python数据结构元组与集合的主要内容,如果未能解决你的问题,请参考以下文章

Python基础(3) - 去掉列表或元组中的重复元素

python基础-列表 元组 集合 字典区别和用法

Python基础数据结构:列表 | 元组 | 集合 | 字典

Python基础数据结构:列表 | 元组 | 集合 | 字典

Python列表元组字典和集合

元组与列表的区别