zip的取值
a = [1,2,3]
b = [4,5,6]
c = [4,5,6,7,8]
方法一
for i in zipped:
print(i)
方法二
next(zipped)
方法三
while(a=next(zipped)):
next(zipped)
a=next(zipped)
import tensorflow as tf
从value分解的tensor对象列表。
A = [[1, 2, 3], [4, 5, 6]]
a1 = tf.unstack(A, num=3,axis=1)
a2 = tf.unstack(A, num=2,axis=0)
with tf.Session() as sess:
print(sess.run(a1))
print(sess.run(a2))
输出:
[array([1, 4]), array([2, 5]), array([3, 6])]
[array([1, 2, 3]), array([4, 5, 6])]
案例一
from collections import namedtuple
# 定义一个namedtuple类型User,并包含name,sex和age属性。
A = namedtuple('User', ['name', 'sex', 'age'])
# 创建一个User对象
user = A(name='kongxx', sex='male', age=21)
# 也可以通过一个list来创建一个User对象,这里注意需要使用"_make"方法
user = A._make(['kongxx', 'male', 21])
print (user)
# User(name='user1', sex='male', age=21)
# 获取用户的属性
print (user.name)
print (user.sex)
print (user.age)
# 修改对象属性,注意要使用"_replace"方法
user = user._replace(age=22)
print (user)
# User(name='user1', sex='male', age=21)
# 将User对象转换成字典,注意要使用"_asdict"
print (user._asdict())
# OrderedDict([('name', 'kongxx'), ('sex', 'male'), ('age', 22)])
结果:
User(name='kongxx', sex='male', age=21)
kongxx
male
21
User(name='kongxx', sex='male', age=22)
OrderedDict([('name', 'kongxx'), ('sex', 'male'), ('age', 22)])
案例二
from collections import namedtuple
# 初始化需要两个参数,第一个是 name,第二个参数是所有 item 名字的列表。
coordinate = namedtuple('Coordinate', ['x', 'y'])
c = coordinate(10, 20)
# or
c = coordinate(x=10, y=20)
c.x == c[0]
c.y == c[1]
x, y = c
print((x,y))
结果
(10, 20)
案例三
from collections import namedtuple
websites = [
('Sohu', 'http://www.google.com/', u'张朝阳'),
('Sina', 'http://www.sina.com.cn/', u'王志东'),
('163', 'http://www.163.com/', u'丁磊')
]
Website = namedtuple('Website', ['name', 'url', 'founder'])
for website in websites:
website = Website._make(website)
print (website)
结果
Website(name='Sohu', url='http://www.google.com/', founder='张朝阳')
Website(name='Sina', url='http://www.sina.com.cn/', founder='王志东')
Website(name='163', url='http://www.163.com/', founder='丁磊')