python常用数据结构的常用操作

简介:

作为基础练习吧。列表LIST,元组TUPLE,集合SET,字符串STRING等等,显示,增删,合并。。。

复制代码
#===========List=====================
shoplist = ['apple','mango','carrot','banana']
print 'I have ',len(shoplist), ' items to purchase.'
print 'These items are:'
for item in shoplist:
    print item,
print '\nI also have to buy rice.'
shoplist.append('rice')
print 'My shopping list is now', shoplist

print 'I will sort my list now'
shoplist.sort()
print 'Sorted shopping list is', shoplist

print 'The first item I will buy is', shoplist[0]
olditem = shoplist[0]
del shoplist[0]
print 'I bought the', olditem
print 'My shopping list is ',shoplist
#=======================Tuple===================
zoo = ('python','elephant','penguin')
print 'Number of animals in the zoo is',len(zoo)

new_zoo = 'monkey', 'camel', zoo
print 'Number of cages in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are',new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2]
print 'Number of animal in the new zoo is ',\
    len(new_zoo)-1+len(new_zoo[2])
#===========Dictionary======================
ab = {'Swaroop' :'swaroop@swaroopch.com',
      'Larry'   :'larry@wall.org',
      'Matsumoto':'matz@ruby-lang.org',
      'Spammer' :'spammer@hotmail.com'
      }
print "Swaroop's address is", ab['Swaroop']

del ab['Spammer']

print '\nThere are {0} contacts in the address-book.\n'.format(len(ab))
for name, address in ab.items():
    print 'Contact {0} at {1}'.format(name, address)

ab['Guido'] = 'guido@python.org'

if 'Guido' in ab:
    print "\nGuido's address is",ab['Guido']

#=============Sequence==============
print 'Item 0 is',shoplist[0]
print 'Item 3 is',shoplist[3]
print 'Item -2 is',shoplist[-2]

print 'Item 1 to 3 is',shoplist[1:3]
print 'Item start to end is',shoplist[:]
print 'Item step 2 is',shoplist[::2]
#=============Set================
bri = set(['brazil','russia','china','india'])
print 'india' in bri
print 'usa' in bri
bric = bri.copy()
bric.add('france')
print bric.issuperset(bri)
bri.remove('russia')
print bri & bric
#=================References================
print 'Simple Assignment'
mylist = shoplist
del shoplist[0]

print 'shoplist is', shoplist
print 'mylist is', mylist

print 'Copy by making a full slice'
mylist = shoplist[:]
del mylist[0]

print 'shoplist is', shoplist
print 'mylist is', mylist
#=====================String================
name = 'Swaroop'

if name.startswith('Swa'):
    print 'Yes, the string starts with Swa'
if 'a' in name:
    print 'Yes, the string contains the string "a"'
if name.find('war') != -1:
    print 'Yes, the string contains the string "war"'
delimiter = '_*_'
mylist = ['BRAZIL','RUSSIA','INDIA','CHINA']
print delimiter.join(mylist)
复制代码

输出:

C:\webpy\webpy\Scripts\python.exe C:/pycode/pycode.py
I have  4  items to purchase.
These items are:
apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item I will buy is apple
I bought the apple
My shopping list is  ['banana', 'carrot', 'mango', 'rice']
Number of animals in the zoo is 3
Number of cages in the new zoo is 3
All animals in new zoo are ('monkey', 'camel', ('python', 'elephant', 'penguin'))
Animals brought from old zoo are ('python', 'elephant', 'penguin')
Last animal brought from old zoo is penguin
Number of animal in the new zoo is  5
Swaroop's address is swaroop@swaroopch.com

There are 3 contacts in the address-book.

Contact Swaroop at swaroop@swaroopch.com
Contact Matsumoto at matz@ruby-lang.org
Contact Larry at larry@wall.org

Guido's address is guido@python.org
Item 0 is banana
Item 3 is rice
Item -2 is mango
Item 1 to 3 is ['carrot', 'mango']
Item start to end is ['banana', 'carrot', 'mango', 'rice']
Item step 2 is ['banana', 'mango']
True
False
True
set(['brazil', 'china', 'india'])
Simple Assignment
shoplist is ['carrot', 'mango', 'rice']
mylist is ['carrot', 'mango', 'rice']
Copy by making a full slice
shoplist is ['carrot', 'mango', 'rice']
mylist is ['mango', 'rice']
Yes, the string starts with Swa
Yes, the string contains the string "a"
Yes, the string contains the string "war"
BRAZIL_*_RUSSIA_*_INDIA_*_CHINA

Process finished with exit code 0

目录
相关文章
|
4天前
|
数据处理 Python
如何使用Python的Pandas库进行数据排序和排名
【4月更文挑战第22天】Pandas Python库提供数据排序和排名功能。使用`sort_values()`按列进行升序或降序排序,如`df.sort_values(by='A', ascending=False)`。`rank()`函数用于计算排名,如`df['A'].rank(ascending=False)`。多列操作可传入列名列表,如`df.sort_values(by=['A', 'B'], ascending=[True, False])`和分别对'A'、'B'列排名。
14 2
|
2天前
|
机器学习/深度学习 数据挖掘 网络架构
Python对商店数据进行lstm和xgboost销售量时间序列建模预测分析
Python对商店数据进行lstm和xgboost销售量时间序列建模预测分析
15 0
|
3天前
|
机器学习/深度学习 算法 数据挖掘
PYTHON银行机器学习:回归、随机森林、KNN近邻、决策树、高斯朴素贝叶斯、支持向量机SVM分析营销活动数据|数据分享-2
PYTHON银行机器学习:回归、随机森林、KNN近邻、决策树、高斯朴素贝叶斯、支持向量机SVM分析营销活动数据|数据分享
27 1
|
2天前
|
机器学习/深度学习 算法 算法框架/工具
数据分享|PYTHON用KERAS的LSTM神经网络进行时间序列预测天然气价格例子
数据分享|PYTHON用KERAS的LSTM神经网络进行时间序列预测天然气价格例子
23 0
|
1天前
|
机器学习/深度学习 存储 监控
数据分享|Python卷积神经网络CNN身份识别图像处理在疫情防控下口罩识别、人脸识别
数据分享|Python卷积神经网络CNN身份识别图像处理在疫情防控下口罩识别、人脸识别
|
1天前
|
机器学习/深度学习 数据采集 算法
Python信贷风控模型:Adaboost,XGBoost,SGD, SVC,随机森林, KNN预测信贷违约支付|数据分享
Python信贷风控模型:Adaboost,XGBoost,SGD, SVC,随机森林, KNN预测信贷违约支付|数据分享
Python信贷风控模型:Adaboost,XGBoost,SGD, SVC,随机森林, KNN预测信贷违约支付|数据分享
|
1天前
|
JSON 数据可视化 定位技术
python_将包含汉字的字典数据写入json(将datav的全省数据中的贵州区域数据取出来)
python_将包含汉字的字典数据写入json(将datav的全省数据中的贵州区域数据取出来)
4 0
|
2天前
|
机器学习/深度学习 算法 Python
数据分享|Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户
数据分享|Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户
21 4
|
3天前
|
数据挖掘 数据处理 索引
如何使用Python的Pandas库进行数据筛选和过滤?
Pandas是Python数据分析的核心库,提供DataFrame数据结构。基本步骤包括导入库、创建DataFrame及进行数据筛选。示例代码展示了如何通过布尔索引、`query()`和`loc[]`方法筛选`Age`大于19的记录。
10 0
|
5天前
|
Python
如何使用Python的Pandas库进行数据缺失值处理?
Pandas在Python中提供多种处理缺失值的方法:1) 使用`isnull()`检查;2) `dropna()`删除含缺失值的行或列;3) `fillna()`用常数、前后值填充;4) `interpolate()`进行插值填充。根据需求选择合适的方法处理数据缺失。
39 9