python中字典、元组、集合

本文介绍了Python中的6种基本数据类型(整数、字符串、列表、元组、集合和字典),重点讲解了可变类型和不可变类型的区别,并通过实例展示了变量操作、for循环、字符串反转、元组与集合的使用以及字典的键值对操作。此外,文章还涵盖了集合运算、字典和字符串的实例应用。

python数据类型(6种):数字、字符串、列表、元组、集合、字典。
可变3种:列表,字典,集合
不可变:数字,字符串、元组。

x=int(input('please enter an inrerger:'))
if x<0:
    x=0
    print('Negative changed to zero')
elif x==0:
    print('Zero')

#for语句
words = ['cat','window','defenestrate']
for w in words:
    print(w,len(w))

#实例反转字符串
def reversewords(input):
    inputWords= input.split("")
    inputWords= inputWords[-1::-1]
    #重新组合称字符串
    output= ''.join(inputWords)
    return output
if __name__=="__main__":
    input='I like runoob'
    rw=reversewords(input)
    print(rw)

#tuple元组
#元组与列表类似,区别在于元组的元素不能修改,元组的元素写在小括号里。
tuple= ('abcd',786,2.323,'runoob',79.2)
tinytuple= (234,'runnoob')
#空元组
tup1=()
#元组中只有一个元素,需要在元素后面加逗号
tup2=(20,)


#set集合
#集合的标志是{}或者set(),但是创建空集合必须用set(),因为{}是用来创建空字典的。
sites = {'Google', 'Taobao', 'Runoob', 'Facebook', 'Zhihu', 'Baidu'}
sites2 = set('GoogleBaidu')
print(sites)   # 输出集合,重复的元素被自动去掉
# 成员测试
if 'Runoob' in sites :
    print('Runoob 在集合中')
else :
    print('Runoob 不在集合中')

#set可以进行集合运算
a= set('abracadabra')
b= set('alacazam')
print(a)
print(a-b)#求a和b集合的差集
print(a|b)#求a和b集合的并集
print(a&b)#求a和b的交际
print(a^b)#求a和b中不同时存在的元素

#字典dictionary
#列表是有序的对象集合。字典是无需的对象集合。两者的区别在于:字典是通过键进行存取,列表是通过偏移量进行存取。
#字典是一种映射类型,字典用{}标识,它是一个无序的键(key):值的集合。
dict= {}
dict['one']='1-菜鸟教程'
dict[2]='2-菜鸟工具'
tinydict= {'name':'runoob','code':1,'site':'www.runoob.com'}
print(dict['one'])#输出键为'one'的值
print(tinydict)#输出完整的字典
print(tinydict.keys())#输出所有的键
print(tinydict.values())#输出所有值


Python中,字典元组、列表集合之间可以通过特定的函数进行相互转换。转换成Python列表需要使用`list`函数,转成元组需要使用`tuple`函数,转成集合需要使用`set`函数,转成字典需要使用`dict`函数[^1]。 ### 字典转换为其他类型 - **转换为列表**:可以将字典的键、值或键值对转换为列表。 ```python d = {'a': 1, 'b': 2, 'c': 3} # 转换键为列表 keys_list = list(d.keys()) print(keys_list) # 输出: ['a', 'b', 'c'] # 转换值为列表 values_list = list(d.values()) print(values_list) # 输出: [1, 2, 3] # 转换键值对为列表 items_list = list(d.items()) print(items_list) # 输出: [('a', 1), ('b', 2), ('c', 3)] ``` - **转换为元组**:同样可以将字典的键、值或键值对转换为元组。 ```python d = {'a': 1, 'b': 2, 'c': 3} # 转换键为元组 keys_tuple = tuple(d.keys()) print(keys_tuple) # 输出: ('a', 'b', 'c') # 转换值为元组 values_tuple = tuple(d.values()) print(values_tuple) # 输出: (1, 2, 3) # 转换键值对为元组 items_tuple = tuple(d.items()) print(items_tuple) # 输出: (('a', 1), ('b', 2), ('c', 3)) ``` - **转换为集合**:可以将字典的键转换为集合。 ```python d = {'a': 1, 'b': 2, 'c': 3} keys_set = set(d.keys()) print(keys_set) # 输出: {'a', 'b', 'c'} ``` ### 元组转换为其他类型 - **转换为列表**:使用`list`函数。 ```python t = (1, 2, 3) l = list(t) print(l) # 输出: [1, 2, 3] ``` - **转换为集合**:使用`set`函数,集合会自动去重。 ```python t = (1, 2, 2, 3) s = set(t) print(s) # 输出: {1, 2, 3} ``` - **转换为字典**:如果元组是包含键值对的元组序列,可以使用`dict`函数。 ```python t = (('a', 1), ('b', 2), ('c', 3)) d = dict(t) print(d) # 输出: {'a': 1, 'b': 2, 'c': 3} ``` ### 列表转换为其他类型 - **转换为元组**:使用`tuple`函数。 ```python l = [1, 2, 3] t = tuple(l) print(t) # 输出: (1, 2, 3) ``` - **转换为集合**:使用`set`函数,集合会自动去重。 ```python l = [1, 2, 2, 3] s = set(l) print(s) # 输出: {1, 2, 3} ``` - **转换为字典**:如果列表是包含键值对的列表序列,可以使用`dict`函数。 ```python l = [('a', 1), ('b', 2), ('c', 3)] d = dict(l) print(d) # 输出: {'a': 1, 'b': 2, 'c': 3} ``` ### 集合转换为其他类型 - **转换为列表**:使用`list`函数。 ```python s = {1, 2, 3} l = list(s) print(l) # 输出: [1, 2, 3] ``` - **转换为元组**:使用`tuple`函数。 ```python s = {1, 2, 3} t = tuple(s) print(t) # 输出: (1, 2, 3) ``` 集合不能直接转换为字典,因为集合中的元素是无序且唯一的,不具备键值对的结构。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值