最新消息:阿里云双12优惠,史上最低折扣。

python教程—列表转字符串

服务器租用 aliyun 185浏览

1M带宽不同应用类型承受力与优化的方法详解

云服务器在我们实际生活中的应用越来越多,有很多攻略文章上了就交大家如何选择带宽。不过有句话说的好“抛开一切实际应用场景谈选择,纯属耍流氓!”

应用Python列表十分重要,列表转字符串、字典这些小知识你都会了嘛?多维元组转换列表这些“惊天地泣鬼神”的操作你掌握的咋样?

本文有Python“大神”小睿给您全面解析……

小睿就是卖个萌,真正大佬不要出来打我哟!

Python列表转字符串

str1 = “12345”

list1 = list(str1)

print list1

str2 = “123 sjhid dhi”

list2 = str2.split() #or list2 = str2.split(” “)

print list2

str3 = “www.xinruiyun.cn”

list3 = str3.split(“.”)

print list3

输出为:

[‘1’, ‘2’, ‘3’, ‘4’, ‘5’]

[‘123’, ‘sjhid’, ‘dhi’]

[‘www’, ‘xinruiyun’, ‘cn’]

3.list >>>str

str4 = “”.join(list3)

print str4

str5 = “.”.join(list3)

print str5

str6 = ” “.join(list3)

print str6

输出为:

wwwxinruiyuncn

www.xinruiyun.cn

www xinruiyun cn

Python列表转字典

1、现在有两个列表,list1 = [‘key1’,‘key2’,‘key3’]和list2 = [‘1’,‘2’,‘3’],把他们转为这样的字典:{‘key1’:‘1’,‘key2’:‘2’,‘key3’:‘3’}

>>>list1 = [‘key1’,‘key2’,‘key3’]

>>>list2 = [‘1’,‘2’,‘3’]

>>>dict(zip(list1,list2))

{‘key1’:‘1’,‘key2’:‘2’,‘key3’:‘3’}

2、将嵌套列表转为字典,有两种方法,

>>>new_list= [[‘key1’,‘value1’],[‘key2’,‘value2’],[‘key3’,‘value3’]]

>>>dict(list)

{‘key3’: ‘value3’, ‘key2’: ‘value2’, ‘key1’: ‘value1’}

或者这样:

>>>new_list= [[‘key1’,‘value1’],[‘key2’,‘value2’],[‘key3’,‘value3’]]

>>>new_dict = {}

>>> for i in new_list:

   new_dict[i[0]] = i[1]                #字典赋值,左边为key,右边为value

>>> new_dict

{‘key3’: ‘value3’, ‘key2’: ‘value2’, ‘key1’: ‘value1’}

进阶教程:多维元组转换列表

代码如下:

= ((1,2,3),(4,5,6,),(7,8,9))

= list(a)

print b

for c in b:

    c = list(c)

    print c

print b

想把这个多维元组变成[[1,2,3],[4,5,6],[7,8,9]]

输出结果如下,仍然无法实现这种效果:

[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

[1, 2, 3]

[4, 5, 6]

[7, 8, 9]

[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

怎样才能将多维元组,全部转换成python的列表?

非常感谢大家的帮助,问题解决了,我来说说我的理解,如果有不对的地方,还请指正:

在使用for从序列中得到的每一个对象,事实上都是从原序列中复制了一份。因此对此对象进行处理时,只是处理了复制体,并没有影响到原序列。

如果需要对原序列做修改,有很多方法,下面是在评论中看到的一个比较简单易想的:

= ((1,2,3),(4,5,6,),(7,8,9))

= list(a)

print b

for c in b:

    b[b.index(c)] = list(c)

    print c

print b

即直接对原序列进行更换,运行结果如下:

[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

(1, 2, 3)

(4, 5, 6)

(7, 8, 9)

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

= ((1,2,3),(4,5,6,),(7,8,9))

= list(a)

b[:] = [list(c) for c in b]

print b

也凑个热闹吧 🙂

= ((1, 2, 3), (4, 5, 6), (7, 8, 9))

= []

for i in range(len(a)):

    b.append(list(a[i]))

print b

>>> a = ((1,2,3),(4,5,6,),(7,8,9))

>>> print map(list, a)

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

任意维度的tuple,稍作修改可以适应任意list, iterable:

def flatten(xs):

    for x in xs:

        if isinstance(x, tuple):

            for xx in flatten(x):

                yield xx

        else:

            yield x

= ((1,2,3),(4,5,6,),(7,8,9))

print list(flatten(a))

= ((1, 2, 3),((4, 5), (6)), ((7, 8, 9)))

print list(flatten(a))

有一个非常轻量级也非常经典与方便的python库,叫jerk,只有4个api,对于这种类型转换的问题,用起来非常方便。一楼利用map处理是非常方便与得体的。但是若对于更加复杂或者其他的情况,则不好弄。

如 a = ( (1, 2, 3), 1, 2, 3, (1, (1, 1), set([1, 2])), {1: ‘tutorial’} )

对于这类数值类型转换问题,使用jerk中的xlist即可。

(jerk 的四个api分别是:xlist, xtuple, xint, xlen)

    #先来安装jerk库

    $ pip install jerk

    #使用jerk中xlist的范例

    $ python

    >>> from jerk import *

    >>> a = ( (1, 2, 3), 1, 2, 3, (1, (1, 1), set([1, 2])), {1: ‘tutorial’} )

    >>> print xlist(a)

    >>> [[1, 2, 3], 1, 2, 3, [1, [1, 1], set([1, 2])], {1: ‘tutorial’}]

转载请注明:小猪云服务器租用推荐 » python教程—列表转字符串