python - 什么是用一个键和多个值更改字典以获得所需输出的 ​​Pythonic 方法?

标签 python mysql list dictionary

手头的问题:

我有下面的元组列表(ID,国家/地区),我最终将存储在 MySQL 表中。

mylist = [(10, 'Other'), (10, 'India'), (10, 'Unknown'), (11, 'Other'), (11, 'Unknown'), (12, 'USA'), (12, 'UK'), (12, 'Other')]

我想使用以下条件来处理“其他”和“未知”:

Value       Replaced by => This value
----------------------------------------
Other & Unknown         => Other
A country & Other       => Country
A country & Unknown     => Country

python :

def refinelist(mylist):

    '''Updating the list to remove unwanted values'''
    '''
    Other & Unknown => Other
    A country & Other => Country
    A country & Unknown => Country
    '''

    if 'Other' in mylist and 'Unknown' in mylist:
        print 'remove unknown'
        mylist.remove('Unknown')
    if 'Other' in mylist and len(mylist) >= 2:
        print 'remove other'
        mylist.remove('Other')
    if 'Unknown' in mylist and len(mylist) >= 2:
        print 'remove unknown'
        mylist.remove('Unknown')

    return mylist

def main():

    mylist = [(10, 'Other'), (10, 'India'), (10, 'Unknown'), (11, 'Other'), (11, 'Unknown'), (12, 'USA'), (12, 'UK'), (12, 'Other')]

    d = {}

    for x,y in mylist:
        d.setdefault(x, []).append(y)

    # Clean the list values    
    for each in d:
        d[each] = refinelist(d[each])

    ## Convert dict to list of tuples for database entry

    outlist = []

    #result = [(key, value) for key,value in d.keys(), value in d.values()]  ## Couldn't get this to work. Can the below loop be written as list comprehension with minimal footprint?

    for key, value in d.items():
        if len(value) == 1:
            print key, value[0]
            outlist.append((key, value[0]))
        elif len(value) > 1:
            for eachval in value:
                print key, eachval
                outlist.append((key, eachval))

    print outlist

if __name__ == "__main__":
    main()    

输出:

remove unknown
remove other
remove unknown
remove other
10 India
11 Other
12 USA
12 UK
[(10, 'India'), (11, 'Other'), (12, 'USA'), (12, 'UK')]

问题:

我觉得这可以更有效地完成。使用字典是不是有点矫枉过正?

我从元组 (luple) 列表开始,将其转换为字典,执行干净操作,然后将其转换回 luple?

我可以只在 MySQL 表中插入原始 luple,然后用很少的查询处理“未知”和“其他”,但我更喜欢 Python 来完成这项任务。

非常感谢 pythonic 解决方案或对代码的一些批评。

最佳答案

大量使用生成器和列表理解,你可以这样写:

other = ['Other', 'Unknown']                        # Strings denoting non-contries
ids = set(i for i,j in mylist)                      # All ids in the list
known = set(i for i,j in mylist if j not in other)  # Ids of real countries
outlist = [k for k in mylist if k[1] not in other]  # Keep all real countries
outlist.extend((i, other[0]) for i in ids - known)  # Append "Other" for all IDs with no real country

结果会是

[(10, 'India'), (12, 'USA'), (12, 'UK'), (11, 'Other')]

如果顺序很重要,这将意味着更多的工作。

关于python - 什么是用一个键和多个值更改字典以获得所需输出的 ​​Pythonic 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11866716/

相关文章:

php - 加盐密码,特别是关于数据库和检索密码

mysql - 在同一 sql 语句中使用 mysql 别名

c++ - 如何正确使用和释放asn1c SEQUENCE_OF?

php - 使用 PHP 将数据从 MySQL 导入数组和表?

python - 如何在 Pandas 数据框中用 0 填充多个列表?

javascript - 聚合物 1.0 : Best practice for managing a simple list of items

python - 如何使用Python将csv行循环到selenium元素?

Python 字符编码

python - SQLALchemy 查询 : Unresolved attribute reference 'query' for class 'Car'

python - ldap python result3 函数结果中缺少 LDAP 属性,但出现在 ldapsearch 命令结果中