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

时间:2024-04-19 20:20:01

作为基础练习吧。列表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)

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

输出:

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