美文网首页
Python之list

Python之list

作者: 微微笑_蝶虹 | 来源:发表于2020-04-21 18:10 被阅读0次

import random

code=['010','020','029','025','027','021','028','022','023','024','0755','0571',

      '0371','0512','0531','0731','0532','0351','0086']

citycode=random.choice(code)#随机取出list中的数据进行赋值

print(code[0],'\n',len(code),'\n',citycode)#换行打印

for iin range(len(code)):

if i<=len(code):#长度为19但是只会执行18次

    citycode =code[i]#顺序取出list值进行赋值

    i = i +1

    print(citycode)

else:

print("超出限制")

-----------------------------------------------------------------

w=['1','2','3']

w.append('1')

print(w)#append(添加到列表末尾的对象)方法用于在列表末尾添加新的对象

#执行结果:['1', '2', '3', '1']

h=['a','b','c']

h.extend(w)#extend(元素列表) 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)

print(h)

#执行结果:['a', 'b', 'c', '1', '2', '3', '1']

相关文章

网友评论

      本文标题:Python之list

      本文链接:https://www.haomeiwen.com/subject/mlhpihtx.html