本文将带您了解关于Python遍历对象属性的新内容,同时我们还将为您解释python遍历对象属性的相关知识,另外,我们还将为您提供关于07-Python遍历&排序、ES6五种遍历对象属性的方式、Fle
本文将带您了解关于Python遍历对象属性的新内容,同时我们还将为您解释python 遍历对象属性的相关知识,另外,我们还将为您提供关于07-Python遍历&排序、ES6五种遍历对象属性的方式、Flex 遍历对象属性、foreach遍历对象属性的实用信息。
本文目录一览:Python遍历对象属性(python 遍历对象属性)
如何在Python中遍历对象的属性?
我有一堂课:
class Twitt: def __init__(self): self.usernames = [] self.names = [] self.tweet = [] self.imageurl = [] def twitter_lookup(self, coordinents, radius): cheese = [] twitter = Twitter(auth=auth) coordinents = coordinents + "," + radius print coordinents query = twitter.search.tweets(q="", geocode=coordinents, rpp=10) for result in query["statuses"]: self.usernames.append(result["user"]["screen_name"]) self.names.append(result[''user'']["name"]) self.tweet.append(h.unescape(result["text"])) self.imageurl.append(result[''user'']["profile_image_url_https"])
现在,我可以通过执行以下操作获取我的信息:
k = Twitt()k.twitter_lookup("51.5033630,-0.1276250", "1mi")print k.names
我想要做的是像这样循环遍历for循环中的属性:
for item in k: print item.names
答案1
小编典典更新
对于python 3,您应该使用items()
而不是iteritems()
PYTHON 2
for attr, value in k.__dict__.iteritems(): print attr, value
PYTHON 3
for attr, value in k.__dict__.items(): print(attr, value)
这将打印
''names'', [a list with names]''tweet'', [a list with tweet]
07-Python遍历&排序
1.List列表
①、列表遍历
charList = [''H'',''e'',''l'',''l'',''o'']
extList = [''P'',''y'',''t'',''h'',''o'',''n'']
# 列表遍历1
for value in charList:
print(value)
# 列表遍历2
for i in range(len(charList)):
print(i)
# 列表遍历,获取index和value(设置起始遍历索引)
for i,value in enumerate(charList,0):
print(i,value)
# 同时遍历两个列表
for value1,value2 in zip(charList,extList):
print(value1,value2)
# 反向遍历
for item in reversed(charList):
print(item)
②、列表排序
# 列表正序
charList.sort()
print(charList)
# 列表反序
charList.sort(reverse=True)
print(charList)
# 列表高级排序:对Item的某个关键字进行排序
L = [(''A'',1),(''C'',3),(''B'',2),(''D'',4)]
# L.sort(lambda x,y:cmp(x[1],y[1])) # Python 2.x
# lambda x:x[1] 是一个匿名函数,每次去取L中的元素x(元组),索引为1的值
L.sort(key=lambda x:x[1],reverse=True)
print(L)
# operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号)
import operator
L.sort(key=operator.itemgetter(1))
print(L)
# DSU排序方法:Decorate-Sort-Undercorate
config = [(x[1],i,x) for i,x in enumerate(L)]
config.sort()
print(L)
③、列表推导&切片
# 推导列表
print([[6 for x in range(5)] for y in range(3)])
# 切片:[start : end : step],step为步进值进行取值,step<0代表从右到左,step>0标识从左到右,结果返回List
# 反转List,与reverse()方法效果一致
print(charList[::-1])
# 反转字符串
print(''Hello Python!''[::-1])
2.Dictionary字典
# 字典key遍历
for key in D:
print(key)
# 字典的遍历
for key,value in D.items():
print(key,value)
3.String字符串
# 字符串格式化一
a = "I''m %s. I''m %d year old" % (''CaiBird'', 22)
# 字符串格式化二
b = ''{0}llo Py{1}!!''.format(''He'',''thon'')
# 字符串格式化三
c = ''{header}llo Py{footer}!!''.format(header=''He'',footer=''thon'')
# ''!a'' (使用 ascii()), ''!s'' (使用 str()) 和 ''!r'' (使用 repr()) 可以用于在格式化某个值之前对其进行转化
import math
print(''常量 PI 的值近似为: {}。''.format(math.pi))
#常量 PI 的值近似为: 3.141592653589793。
print(''常量 PI 的值近似为: {!r}。''.format(math.pi))
#常量 PI 的值近似为: 3.141592653589793。
ES6五种遍历对象属性的方式
ES6五种遍历对象属性的方式
function allObj(){
this.name = ''张三''; // 自有属性
this.age = ''12''; // 自有属性
this.invisible = {
enumerable: false,
value: ''hello''
},
this.invisible = {
enumerable: false,
value: ''hello''
}
}
allObj.prototype.disEnum = {
enumerable: false,
value: ''disEnum''
}
allObj.prototype.Enum = {
enumerable: true,
value: ''Enum''
}
let obj = new allObj
Object.assign(obj, {
a: ''1'',
b: ''2'',
c: ''3'',
[Symbol(''c'')]: ''c'',
})
Object.assign(obj,
Object.defineProperty({}, ''visible'',{
enumerable: true,
value: ''word''
})
)
console.log(obj); // allObj {a: "1",age: "12",b: "2",c: "3",invisible: {enumerable: false,value: ''hello''},name: "张三",visible: "word",Symbol(c): "c",__proto__:Enum: {enumerable: true, value: "Enum"},disEnum: {enumerable: false, value: "disEnum"}}
// for...in循环遍历对象自身的和继承的属性(不含Symbol属性)
for(let key in obj){
console.log(key);
// name
// age
// invisible
// a
// b
// c
// invisible
// visible
// disEnum
// Enum
}
// Object.keys()返回一个数组,包括对象自身(不含继承的)的所有属性(不含Symbol属性)
console.log(Object.keys(obj)); // ["name", "age", "invisible", "a", "b", "c", "visible"]
// Object.getOwnPropertyNames返回一个数组,包含对象自身的所有属性(不含Symbol属性,但是包括不可枚举属性 )
console.log(Object.getOwnPropertyNames(obj)); // ["name", "age", "invisible", "a", "b", "c", "visible"]
// Object.getOwnPropertySymbols() 返回一个数组,包含所有对象自身的所有Symbol属性
console.log(Object.getOwnPropertySymbols(obj)); // [Symbol(c)]
// Reflect.ownKeys()返回一个数组,包含对象自身的所有属性,不管属性名是Symbol还是字符串,也不管是否可枚举
console.log(Reflect.ownKeys(obj)); // ["name", "age", "invisible", "a", "b", "c", "visible", Symbol(c)]
解决for..in
遍历对象时,原型链上的所有属性都将被访问
function allObj(){
this.name = ''张三''; // 自有属性
this.age = ''12''; // 自有属性
this.invisible = {
enumerable: false,
value: ''hello''
},
this.invisible = {
enumerable: false,
value: ''hello''
}
}
allObj.prototype.disEnum = {
enumerable: false,
value: ''disEnum''
}
allObj.prototype.Enum = {
enumerable: true,
value: ''Enum''
}
let obj = new allObj
Object.assign(obj, {
a: ''1'',
b: ''2'',
c: ''3'',
[Symbol(''c'')]: ''c'',
})
Object.assign(obj,
Object.defineProperty({}, ''visible'',{
enumerable: true,
value: ''word''
})
)
console.log(obj);
for(let key in obj){
if(obj.hasOwnProperty(key)===true){
console.log(key);
// name
// age
// invisible
// a
// b
// c
// visible
}
}
ES6五种遍历对象属性的方式
Flex 遍历对象属性
var objInfo:Object = ObjectUtil.getClassInfo(vo); var fieldNameArr:Array = objInfo["properties"] as Array; for each(var fld:QName in fieldNameArr){ if(fld.localName != "content") snastrxml += " " + fld.localName + "='" + vo[fld.localName]+"'"; }
foreach遍历对象属性
<?php
class Stu
{
private $name = ''陈小春'';
protected $sex = ''男'';
public $man = ''人类'';
public function info()
{
foreach($this as $key=>$val )
{
echo ''类内输出: ''.$key.''=>''.$val.''<br>'';//因为在类内,输出所有属性
}
}
}
$obj = new Stu();
$obj->info();
//类外循环
foreach($obj as $key=>$val)
{
echo ''类外输出: ''.$key.''=>''.$val; //只有人类一个,因为其他2个是受保护和私有的
}
今天关于Python遍历对象属性和python 遍历对象属性的介绍到此结束,谢谢您的阅读,有关07-Python遍历&排序、ES6五种遍历对象属性的方式、Flex 遍历对象属性、foreach遍历对象属性等更多相关知识的信息可以在本站进行查询。
本文标签: