把列表内的字符串转为int,列表相关内容

By admin in 4858.com on 2019年5月2日

list(列表)复习:

分段就是分开,由3个点来分散,发散成两条线以上,编制程序语句用if-elif-if和逻辑表明式,这几个逻辑表达式就是前边所说的布尔运算,得出的结果正是“真”或“假”,以此为依据来控制是还是不是进行冒号前边的代码块,好的,终于描述清楚了,摘选这几课中相比有效的代码贴上来

#开首列表,列表内为字符串内容

python 列表相关内容

壹.一组有各类的数额的组合

ex30

list = [‘1′,’2′,’3’]

把列表内的字符串转为int,列表相关内容。1、增加
2、修改
3、切片
4、清空
5、删除
6、杂项

贰.创建列表:

# coding=utf-8
people = 30
cars = 40
trucks = 15

if cars > people:
    print "We should take the cars."
elif cars < people:
    print "We should not take the cars."
else:
    print "We can't decide."

if trucks > cars:
    print "That's too many trucks."
elif trucks < cars:
    print "Maybe we could take the trucks."
else:
    print "We still can't decide."

if people > trucks:
    print "Alright, let's just take the trucks."
else:
    print "Fine, let's stay home then."

#新建2个列表

#概念五个列表
name2 = [1,2,3,4]
name_list = [‘zhang’,’wang’,’li’,’liu’,’yang’
,[‘ding’,’guo’],’zhao’]

  —–空列表

ex31

new_list=[]

1.列表的扩充操作:
name_list.append(name2)
print(name_list)
输出结果:
[‘zhang’, ‘wang’, ‘li’, ‘liu’, ‘yang’, [‘ding’, ‘guo’], ‘zhao’, [1,
2, 3, 4]]

# 1.创建空list
l=[]
l_one=list()
print(type(l))
print(type(l_one))
print("-" * 20)
# 2.创建带值的list
l2=[20]
print(type(l2))
print(l2)
print("-" * 20)
# 3.创建list,带多个值
l3=[1,2,3,4,4,5,8]
print(type(l3))
print(l3)
# coding=utf-8

print "You enter a dark room with two doors. Do you go through door #1 or door #2?"

door =raw_input("> ")

if door == "1":
    print "There's a giant bear here eating a cheese cake. What do you do?"
    print "1.Take the cake."
    print "2.Scream at the bear."
    bear = raw_input("> ")

    if bear == "1":
        print "The bear eats your face off. Good job!"
    elif bear == "2":
        print "The bear eats your legs off. Good job!"
    else:
        print "Well, doing %s is probably better. Bear runs away." % bear
elif door =="2":
    print "You stare into the endless abyes at Cthulhu's retina."
    print "1.Blueberries."
    print "2.Yellow jacket clothespins."
    print "3.Understanding revolvers yelling melodies."

    insanity = raw_input("> ")

    if insanity == "1" or "2":
        print "Your body survives powered by a mind of jello. Good job!"
    else:
        print "The insanity rots your eyes into a pool of muck. Good job!"

else:
    print "You stumble around and fall on a knife and die. Good job!"

#使用for循环

name2.insert(2,7)

输出结果:

ex32

for n in list:

二.列表修改:
name_list[2]=’WANG’
print(name_list)
[‘zhang’, ‘wang’, ‘WANG’, ‘liu’, ‘yang’, [‘ding’, ‘guo’], ‘zhao’]

<class 'list'>
<class 'list'>
--------------------
<class 'list'>
[20]
--------------------
<class 'list'>
[1, 2, 3, 4, 4, 5, 8]
# coding = utf-8
the_count = [1,2,3,4,5]
fruits =['apples','oranges','pears','apricots']
change = [1,'pennies',2,'dimes',3,'quarters']

for number in the_count:
    print "This is count %d" % number

for fruit in fruits:
    print "A fruit of type: %s" % fruit

for i in change:
    print "I got %r" % i 

elements = []

for i in range(0,6):
    print "Adding %d to the list." % i 
    elements.append(i)

for i in elements:
    print "Element was : %d" % i 

#添加int(n)到new_list内

3.列表切开:
秘笈:跟鸵鸟同样,顾首不顾尾
输入:
print(name_list[2:5])
print(name_list[1:-1])
print(name_list[:-2])
print(name_list[2:])
print(name_list[::2])
输出:
[‘zhang’, ‘wang’, ‘WANG’, ‘liu’, ‘yang’, [‘ding’, ‘guo’], ‘zhao’]
[‘WANG’, ‘liu’, ‘yang’]
[‘wang’, ‘WANG’, ‘liu’, ‘yang’, [‘ding’, ‘guo’]]
[‘zhang’, ‘wang’, ‘WANG’, ‘liu’, ‘yang’]
[‘WANG’, ‘liu’, ‘yang’, [‘ding’, ‘guo’], ‘zhao’]
[‘zhang’, ‘WANG’, ‘yang’, ‘zhao’]

三.列表操作

在延续的顺序中,for与列表有大气的协作使用

  new_list.append(int(n))

四.列表清空:
输入:
name2.clear()
print(name2)
出口结果:
[]

# 下标访问list
l=[2,3,4,1,56,78,11,23]
print('取第四个元素值:',l[3])
print('取第一个元素值:',l[0])
print("-" * 20)
#分片操作
print('取所有值',l[:])
print('取第2个到第4个值',l[1:4])
print('取第1个到第4个值',l[:4])
print('取第3个到最后的值',l[2:])
print("-" * 20)
#分片可以控制增长幅度
print('取第2个到第6个值,中间增长幅度为1',l[1:6:1])
print('取第2个到第6个值,中间增长幅度为2',l[1:6:2])
print("-" * 20)
#分片下标为负数时
#正常情况下,分片左边的值一定小于右边的值
print('取倒数第6个到倒数第2个值',l[-6:-1])
print('取倒数第6个到倒数第2个值,中间增长幅度为2',l[-6:-1:2])
#如果分片值左边值一定要比右边大的话,则增长幅度需要使用负数
print('取倒数第1个到倒数第5个值',l[-1:-6:-1])
print('取倒数第1个到倒数第5个值,中间增长幅度为2',l[-1:-6:-2])
print("-" * 20)

说一下range()函数,这一个函数也是会在背后大批量的运用,整理一下它的多少个点:

#sum()输出列表内数字相加的和

5.列表的去除操作:
1、remove 方法
name_list.remove(‘liu’)
2、pop 方法
name_list.pop(4)
3、del 方法
del name_list[2]
输入:
name_list.remove(‘liu’)
print(name_list)
name_list.pop(4)
print(name_list)
del name_list[2]
print(name_list)

出口结果:

  • range(一,五)输出的结果是[1, 2, 3, 4]铭记是没有五的
  • range(伍) 输出的结果是[0,1, 2, 3, 4]切记是从0开头的
  • range(0,5,二)输出的结果是[0,2,4],输出的依次是0,加二,加二,最大值要小于5

print(sum(new_list))

输出
[‘zhang’, ‘wang’, ‘li’, ‘yang’, [‘ding’, ‘guo’], ‘zhao’]
[‘zhang’, ‘wang’, ‘li’, ‘yang’, ‘zhao’]
[‘zhang’, ‘wang’, ‘yang’, ‘zhao’]

取第四个元素值: 1
取第一个元素值: 2
--------------------
取所有值 [2, 3, 4, 1, 56, 78, 11, 23]
取第2个到第4个值 [3, 4, 1]
取第1个到第4个值 [2, 3, 4, 1]
取第3个到最后的值 [4, 1, 56, 78, 11, 23]
--------------------
取第2个到第6个值,中间增长幅度为1 [3, 4, 1, 56, 78]
取第2个到第6个值,中间增长幅度为2 [3, 1, 78]
--------------------
取倒数第6个到倒数第2个值 [4, 1, 56, 78, 11]
取倒数第6个到倒数第2个值,中间增长幅度为2 [4, 56, 11]
取倒数第1个到倒数第5个值 [23, 11, 78, 56, 1]
取倒数第1个到倒数第5个值,中间增长幅度为2 [23, 78, 1]
--------------------

列表的相干的操作,现有列表list = [1, 2, 3, 4, 5]

#max()输出列表内最大的数字

6.杂项

4.list(del)

  • list[0:]4858.com,列出0号(包涵0)今后的内容,结果是[1,2,3,4,5]
  • list[1:]列出壹号(包罗壹)以往的内容,结果是[2,3,4,5]
  • list[:-1]列出-一号在此以前的(这里不包蕴-一号)结果是[1,2,3,4]
  • list[2:-2]结果是[3]
  • list[::]与list[:]作用等同都以呈现任何
  • list[::2]结果是[1,3,5]
  • list[2::]结果是[3,4,5]
  • list[::-1]结果是[5,4,3,2,1]
  • list[::-2]结果是[5,3,1]

**print(max(new_list))**

看清某八个要素出现的次数:
print(name_list.count(‘li’))

 

append()函数正是给list在最终面添增加少,举个例子:
list.append(‘haha’),那么地点的列表内容就是[1, 2, 3, 4,
5,‘haha’],英特网搜了壹晃,还有个一般的函数,扩张一下extend():
list.extend([‘haha’,’lulu’])结果是list = [1, 2, 3, 4,
5,’haha’,’lulu’]

*#min()输出列表内最小的数字*

获取某些给定成分的目录地方:
print(name_list.index(‘yang’))
列表翻转:
name_list.reverse()
print(name_list)
输出:
[‘zhao’, [‘ding’, ‘guo’], ‘yang’, ‘liu’, ‘li’, ‘wang’, ‘zhang’]

l=[1,2,3,4,5,6]
print(l)
del(l[2])
print(l)

print(min(new_list))

列表排序:
输入:
name2.insert(2,6)
print(name2)
name2.sort()
print(name2)

出口结果

 

输出:
[1, 2, 6, 3, 4]
[1, 2, 3, 4, 6]

[1, 2, 3, 4, 5, 6]
[1, 2, 4, 5, 6]

列表拷贝:

5.list相加

name2 = name_list.copy()
print(name2)
输出:
[‘zhang’, ‘wang’, ‘li’, ‘liu’, ‘yang’, [‘ding’, ‘guo’], ‘zhao’]

#list相加
l=[1,2,3,4,5,6]
l1=['a','b','c']
print(l+l1)
print(l*3)
#list in 的使用
#in
a=2 in l
print(a)
#not in
b=2 not in l
print(b)

输出结果

[1, 2, 3, 4, 5, 6, 'a', 'b', 'c']
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
True
False

6.list遍历(for while)

#for in list
l=[1,2,3,4,5,6]
for i in l:
    print(i)
print('-'*20)
#while in list
length=len(l)
index=0
while index<length:
    print(l[index])
    index+=1
print('-'*20)
#双层循环
a=[['one',1],['two',2],['three',3]]
for k,v in a:
    print(k,':',v)
print('-'*20)
#三层循环
a=[['one',1,'go'],['two',2,'down'],['three',3,'wash']]
for k,v,w in a:
    print(k,':',v,':',w)
print('-'*20)

出口结果:

1
2
3
4
5
6
--------------------
1
2
3
4
5
6
--------------------
one : 1
two : 2
three : 3
--------------------
one : 1 : go
two : 2 : down
three : 3 : wash
--------------------

7.list中内容for的应用

#for 创建 list
a=['a','b','c']
b=[i for i in a]
print(b)
print('-'*20)
a=[1,2,3,4,5]
print([i*10 for i in a])
print('-'*20)
a=[x for x in range(1,6)]
b=[m for m in a if m%2==0]
print('a:',a)
print('b:',b)
print('-'*20)
a=[i for i in range(1,4)]
print('a:',a)
b=[i for i in range(100,400) if i%100==0]
print('b:',b)
c=[ m+n for m in a for n in b]
print('c:',c)
#以下for与以上同样效果
for m in a:
    for n in b:
        print(m+n,end=" ")
print()
print('-'*20)

出口结果

['a', 'b', 'c']
--------------------
[10, 20, 30, 40, 50]
--------------------
a: [1, 2, 3, 4, 5]
b: [2, 4]
--------------------
a: [1, 2, 3]
b: [100, 200, 300]
c: [101, 201, 301, 102, 202, 302, 103, 203, 303]
101 201 301 102 202 302 103 203 303 
--------------------

捌.list 常用函数

#len
a=[x for x in range(1,10)]
print('len(a):',len(a))
print('-'*20)
#max
print('max(a):',max(a))
b=['woman','man','papa']
print('max(b):',max(b))
print('-'*20)
#list()
s='i love python'
print('lsit(s):',list(s))
print('-'*20)
#append
a=[i for i in range(1,5)]
print(a)
a.append(50)
print('append后:',a)
print('-'*20)
#indsert
a.insert(2,30)
print('insert后:',a)
print('-'*20)
#pop(从list取出并删除最后一个值
p=a.pop()
print('pop删除的值:',p)
print('删除后list:',a)
print('-'*20)
#remove
a.remove(30)
print('remove后:',a)
print('-'*20)
#clear 只清空值
a.clear()
print('clear后:',a)
print('-'*20)
#reverse 翻转
a=[1,2,3,4]
a.reverse()
print('reverse:',a)
print('-'*20)
#extend:扩展
b=[5,6,7,8,5]
a.extend(b)
print('extend:',a)
print('-'*20)
#count 统计某个数在list中出现的次数
print('5出现的次数:',a.count(5))
print('1出现的次数:',a.count(1))
print('-'*20)

输出内容:

4858.com 14858.com 2

len(a): 9
--------------------
max(a): 9
max(b): woman
--------------------
lsit(s): ['i', ' ', 'l', 'o', 'v', 'e', ' ', 'p', 'y', 't', 'h', 'o', 'n']
--------------------
[1, 2, 3, 4]
append后: [1, 2, 3, 4, 50]
--------------------
insert后: [1, 2, 30, 3, 4, 50]
--------------------
pop删除的值: 50
删除后list: [1, 2, 30, 3, 4]
--------------------
remove后: [1, 2, 3, 4]
--------------------
clear后: []
--------------------
reverse: [4, 3, 2, 1]
--------------------
extend: [4, 3, 2, 1, 5, 6, 7, 8, 5]
--------------------
5出现的次数: 2
1出现的次数: 1
--------------------

View Code

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有