python中基本类型的连接组合和互相转换

本篇总结了一下字符串,列表,字典,元组的连接组合使用和类型的互相转换小例子,尤其列表中的extend()方法和字典中的

update方法非常的常用。

1.连接两个字符串

a = "hello "
b = "world"
a += b
print(a)  # hello world
2.字典的连接
dict1 = {1: "a", 2: "b"}
dict2 = {3: "c", 4: "d"}
dict1.update(dict2)
print(dict1)  # {1: ‘a‘, 2: ‘b‘, 3: ‘c‘, 4: ‘d‘}
3.列表的连接
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)  # [1, 2, 3, 4, 5, 6]
print(list1)
4.元组的连接
tuple1 = (1, 2)
tuple2 = (3, 4)
tuple1 += tuple2
print(tuple1)  # (1, 2, 3, 4)

5.字典转换为字符串

dict1 = {1: "a", 2: "b"}
str1 = str(dict1)
print(str1)  # {1: ‘a‘, 2: ‘b‘}
print(type(str1))  # <class ‘str‘>

6.字典转换为列表

dict1 = {1: "a", 2: "b"}
list1 = list(dict1.keys())
list2 = list(dict1.values())
list3 = list(dict1)
print(list1)  # [1, 2]
print(list2)  # [‘a‘, ‘b‘]
print(list3)  # [1,2]

7.字典转换为元组

dict1 = {1: "a", 2: "b"}
tuple1 = tuple(dict1.keys())
tuple2 = tuple(dict1.values())
tuple3 = tuple(dict1)
print(tuple1)  # (1, 2)
print(tuple2)  # (‘a‘, ‘b‘)
print(tuple3)  # (1, 2)

8.列表转换为字符串

list1 = [1, 2, 3]
str1 = str(list1)
print(str1)  # [1, 2, 3]
print(type(str1))  # <class ‘str‘>

9.列表转换为字典

# 1.
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
dict1 = dict(zip(list1, list2))
print(dict1)  # {1: ‘a‘, 2: ‘b‘, 3: ‘c‘}
# 2.
dict1 = {}
for i in list1:
    dict1[i] = list2[list1.index(i)]
print(dict1)  # {1: ‘a‘, 2: ‘b‘, 3: ‘c‘}
# 3.
list1 = [[1, ‘a‘], [2, ‘b‘], [3, ‘c‘]]
dict1 = dict(list1)
print(dict1)  # {1: ‘a‘, 2: ‘b‘, 3: ‘c‘}

10.列表转换为元组

list1 = [1, 2, 3]
tuple1 = tuple(list1)
print(tuple1)  # (1, 2, 3)

11.元组转换为字符串

tuple1 = (1, 2, 3)
str1 = tuple(tuple1)
print(str1)  # (1, 2, 3)
print(type(str1))  # <class ‘tuple‘>

12.元组转换为字典

# 1.
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
dict1 = dict(zip(tuple1, tuple2))
print(dict1)  # {1: 4, 2: 5, 3: 6}
# 2
dict1 = {}
for i in tuple1:
    dict1[i] = tuple2[tuple1.index(i)]
print(dict1)  # {1: 4, 2: 5, 3: 6}

# 3
tuple1 = (1, 2)
tuple2 = (4, 5)
tuple3 = (tuple1, tuple2)
dict1 = dict(tuple3)
print(dict1)  # {1: 2, 4: 5}


13.元组转换为列表

tuple1 = (1, 2)
list1 = list(tuple1)
print(list1)  # [1, 2]
 
时间: 2024-08-10 08:01:57

python中基本类型的连接组合和互相转换的相关文章

python中文和unicode字符串之间的互相转换

首先:中文->unicode字符串 import chardet import codecs >>> a = "我是一个中国人">>> a'\xce\xd2\xca\xc7\xd2\xbb\xb8\xf6\xd6\xd0\xb9\xfa\xc8\xcb' >>> chardet.detect(a){'confidence': 0.99, 'encoding': 'GB2312'}>>> b = a.decod

Python中布尔类型

我们已经了解了Python支持布尔类型的数据,布尔类型只有True和False两种值,但是布尔类型有以下几种运算: 与运算:只有两个布尔值都为 True 时,计算结果才为 True. True and True   # ==> TrueTrue and False   # ==> FalseFalse and True   # ==> FalseFalse and False   # ==> False或运算:只要有一个布尔值为 True,计算结果就是 True. True or

python中time类型,datetime类型的关系与互相转换

一.time模块 time模块提供各种操作时间的函数       一般有两种表示时间的方式:       第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的 Python代码   #当前时间的时间戳 In [9]: time.time() Out[9]: 1376102328.536908 第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同 Python代码   In 

python中列表类型常用操作

列表是个在写测试用例时经常被用到的类型,我们来看下列表常用的一些操作吧. 1. 分片 作用:提取列表中的一部分元素出来(分片在测试的时候也经常会用到) 这里的a[1:4]指取从a这个列表的下标为1的索引开始(即第二个元素),到下标为3的索引的元素,即为[2,3,4] 分片有很多种简写:第一个等于a[0:4],第二个等于a[4:7] 其实我们还可以加步长,比如:0:4本来输出的是[1,2,3,4],但加了步长为2,就输出为[1,3] 2. 列表相加与乘法 两个列表相加得到一个相加后的列表相加比较简

python中干掉tornado的连接失败日志

用了tornado真的是比较舒服,很多事都为你做好了. 但也有不令人满意的地方--对于我这个洁癖来说,自动给我的控制台打印不受我控制的信息是不能忍受的. 连接到一个新的地方,如果失败,tornado会使用python的日志像控制台写错误. 为了消灭这个错误,并且不丢失这个记录,转移到文件,可以这样做: 获取python的这个错误日志对象: gen_log = logging.getLogger("tornado.general") 添加新的日志文件处理器: file_handler =

python中int类型、bool类型补充,及字符串的部分常用方法

一.int类型 bit_length() 返回一个数的二进制长度 二.bool类型 布尔只有两个值. True,False. 一般是没有什么操作的. 所有的空都是False,所以的非空都是True 三.str类型 由',",''', """阔起来的内容就是字符串 字符串是不可变的数据类型.不论你执行任何操作. 源字符串是不会改变的, 每次操作都会返回新字符串 1. 索引和切片 索引从0开始, 使用[下标]可以获取到每一个字符, 还可以倒着数 切片: [起始位置:结束

在PYTHON中,用cx_Oracle连接ORACLE数据库简单示例

一,在安装的时候,参数有点不一样: python setup.py build install 二,连接数据库,有两种方式,DSN和TNSNAMES方式: #dsn = orcl.makedsn(self.oracle_host, self.oracle_port, self.oracle_sid) #con = orcl.connect(self.oracle_username, self.oracle_password, dsn) con = orcl.connect(self.oracle

Python中怎样对数据集整体进行映射转换类型

1 1 >>> line2=['10.235186', '11.321997'] 2 2 >>> line3=[list(map(lambda x:float(x),line2))]; 3 3 >>> line3 4 4 [[10.235186, 11.321997]] 注意: 1 >>> map(lambda x:frozenset(x),C1) 2 <map object at 0x0231BCF0> 3 >&g

python中字符串类型与字典类型相互转换

eval真的好神奇啊,卧槽! 字典(dict)转为字符串(string) 通过遍历dict中的所有元素就可以实现字典到字符串的转换: for key, value in sample_dic.items():         print "\"%s\":\"%s\"" % (key, value 字符串(string)转为字典(dict) 使用 eval()或exec() 函数: a = "{'a': 'hi', 'b': 'there