Python3捕获所有列表索引以获取dicts的访问权限

时间:2016-03-04 15:07:45

标签: python-3.x

我输入了以下内容:

mylist = [{street:long, name:foo, color:blue},{street=short,name:bar,color:red},{street:mid, name:me, color:black})

当我尝试:

mylist[*][name]
list indices must be integers, not str

所以,我怎样才能得到所有的名字'这个列表的子序列?

1 个答案:

答案 0 :(得分:0)

我不认识星形下标语法;也许你在想一种不同的语言?

通常的Pythonic方式是列表理解:

04 20:11:11.076 19366-19366/com.example..photoeditingapp E/AndroidRuntime: FATAL EXCEPTION: main
03-04 20:11:11.076 19366-19366/com.example..photoeditingapp E/AndroidRuntime: Process: com.example.zohair.photoeditingapp, PID: 19366
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime: java.lang.IllegalArgumentException: x must be < bitmap.width()
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime:     at android.graphics.Bitmap.checkPixelAccess(Bitmap.java:1449)
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime:     at android.graphics.Bitmap.getPixel(Bitmap.java:1401)
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime:     at com.example.zohair.photoeditingapp.MainActivity$3.onClick(MainActivity.java:95)
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime:     at android.view.View.performClick(View.java:4640)
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19425)
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733)
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:146)
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5593)
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
03-04 20:11:11.076 19366-19366/com.example.zohair.photoeditingapp E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
03-04 20:11:12.676 19366-19366/com.example.zohair.photoeditingapp I/Process: Sending signal. PID: 19366 SIG: 9

虽然您也可以使用mylist = [ {'street':'long', 'name':'foo', 'color':'blue'}, {'street':'short','name':'bar','color':'red'}, {'street':'mid', 'name':'me', 'color':'black'} ] [entry['name'] for entry in mylist] # => ['foo', 'bar', 'me']

map