2013年8月12日Python的5个最有价值问题

问:Python怎么在字典里删除值但保留相应的键

答:

假设3都在值里,而非键
>>> for v in D1.values():
...     if 3 in v:
...         v.remove(3)
...
>>> D1
{'A1': [2], 'C1': [4, 5], 'B1': [3]}

这个代码还能适用于更多情况,比如:
>>> D1 = {'A1' : [2, 3], 'B1': [3, 3], 'C1' : [4, 5]}
>>> for k, v in D1.items():
...     D1[k] = filter(lambda x: x!=3, v)
...
>>> D1
{'A1': [2], 'C1': [4, 5], 'B1': []}

问:Python的TKinter框架怎么创建实体表

答:

from Tkinter import *
from string import ascii_lowercase
  
class app(Frame):
     def __init__( self , master = None ):
         Frame.__init__( self , master)
         self .grid()
         self .create_widgets()
  
     def create_widgets( self ):
         self .entries = {}
         self .tableheight = 9
         self .tablewidth = 9
         counter = 0
         for row in xrange ( self .tableheight):
             for column in xrange ( self .tablewidth):
                 self .entries[counter] = Entry( self , width = 5 )
                 self .entries[counter].grid(row = row, column = column)
                 counter + = 1
  
prog = app()
prog.master.title( 'Sudoku' )
prog.mainloop()
 
答:
import json
import pandas as pd
  
db = json.loads( open ( 'pruItems.json' , 'r' ).read())
pieces = []
for d in db:
     if d[ 'data' ]:
         df = pd.DataFrame(d[ 'data' ])
         df.columns = [ 'date' , 'bid' , 'ask' ]
         df = df.set_index( 'date' )
         pieces.append(df)
  
df = pd.concat(pieces, axis = 1 , keys = [d[ 'fund' ] for d in db])
  
print df
输出:
                 TGC               FEF              FAF       
                 bid      ask      bid      ask     bid     ask
date                                                         
18/06/2013  34.8400  34.8400  14.9179  14.9179  6.6780  6.6780
17/06/2013  34.4900  34.4900  14.8712  14.8712  6.6510  6.6570
 
答:
使用.content
>>> from bs4 import BeautifulSoup as BS
>>> html = """<li class="li_dataline2">
...
...
... <b> Expiry date: </b>14/09/2013
...
...
... </li>"""
>>> soup = BS(html)
>>> print soup.find( 'li' , { 'class' : 'li_dataline2' }).contents[ - 1 ].strip()
14 / 09 / 2013

转载于:https://www.cnblogs.com/sky7034/p/3252518.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值