① 有關python3中集合(set)的問題
set是無序的,內部是哈希表實現,其中的元素沒有排序過程。
② 在python中字典和集合是用一對大括定號作為界符字典的每個元素有兩部分組成,是什麼鍵和什麼值
mydict = {} # 字典初始化
mydict['one'] = "This is one"
mydict[2] = "This is two"
mydict[3] = "This is three"
print(mydict) # 輸出完整的字典 {'one': 'This is one', 2: 'This is two', 3: 'This is three'}
print(mydict.keys()) # 輸出所有鍵 ['one', 2, 3]
print(mydict.values()) # 輸出所有值 ['This is one', 'This is two', 'This is three']
③ python集合
*事先說明:以下代碼及結果來自本設備Python控制台,在不同設備上可能結果有區別,望自己嘗試為妙
集合(set),是一種Python里的類(class),
集合類似於列表(list),可更改,可迭代(iterable),但是元素不重復
定義集合使用花括弧{},例如:
>>> s = {"apple", "banana", "strawberry", "watermelon"}
警告!!!如果使用空括弧
>>> a = {}
>>> a.__class__
<class 'dict'>
a將成為一個字典
想要定義空集合,
請使用類名。
>>> a = set()
類名定義也可以把迭代轉換為集合:
>>> b = set("abc")
>>> b
{'a', 'b', 'c'}
但是,保存後它是無序的。
>>> s
{'banana', 'watermelon', 'strawberry', 'apple'}
(結果僅供參考,在不同設備上略有不同)
下面介紹它的性質:
1. 可更改:
使用add(x)方法添加元素x:
>>> s.add("lemon")
>>> s
{'banana', 'strawberry', 'lemon', 'watermelon', 'apple'}
使用update(iter1, iter2, …)方法添加多個可迭代對象(如列表,元組(tuple),另一個集合)里的元素:
>>> s.update(("orange", "grape"))
>>> s
{'banana', 'strawberry', 'orange', 'lemon', 'watermelon', 'apple', 'grape'}
警告!!!如果使用字元串,字元串也會被迭代:
>>> a = set()
>>> a.update("apple")
>>> a
{'a', 'p', 'e', 'l'}
使用remove(x)移除元素x,如果x不存在,則拋出KeyError錯誤
>>> s.remove("lemon")
>>> s
{'banana', 'strawberry', 'orange', 'watermelon', 'apple', 'grape'}
>>> s.remove("cat")
Traceback (most recent call last):
File "<stdin>", line 1, in <mole>
s.remove("cat")
KeyError: 'cat'
使用discard(x)移除元素x,不會發生錯誤
>>> s.discard("grape")
>>> s
{'banana', 'strawberry', 'orange', 'watermelon', 'apple'}
>>> s.discard("dog")
>>> s
{'banana', 'strawberry', 'orange', 'watermelon', 'apple'}
2. 可迭代:
>>> for x in s:
... print(x)
banana
strawberry
orange
watermelon
apple
3. 可以使用 len 函數獲取集合長度;
>>> len(s)
5
可以使用in關鍵字檢查一個元素是否在集合內,將返回邏輯值(bool):
>>> "apple" in s
True
>>> "candy" in s
False
4.集合具有不重復性,因此它會自動去重:
>>> c = set("Hello World!")
>>> c
{' ', 'e', 'l', 'H', '!', 'r', 'W', 'o', 'd'}
5. 集合的運算
>>> d = set("abcdef")
>>> e = set("efghij")
>>> d
{'c', 'e', 'a', 'b', 'f', 'd'}
>>> e
{'h', 'e', 'g', 'j', 'f', 'i'}
>>> d - e # 集合d去掉集合e含有的元素,或者說集合d包含而集合e不包含的元素(不改變原集合)
{'a', 'b', 'd', 'c'}
>>> d | e # 集合d,e的所有元素
{'c', 'e', 'h', 'g', 'a', 'b', 'j', 'f', 'd', 'i'}
>>> d & e # 集合d,e都包含的元素
{'f', 'e'}
>>> d ^ e # 不同時出現在集合d, e中的元素
{'c', 'g', 'h', 'a', 'b', 'j', 'd', 'i'}
注意!!!
字元串、列表通用的+和*不適用於集合
>>> "abc" + "def"
'abcdef'
>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> d + e
Traceback (most recent call last):
File "<stdin>", line 1, in <mole>
d + e
TypeError: unsupported operand type(s) for +: 'set' and 'set'
>>> "a" * 5
'aaaaa'
>>> [1] * 3
[1, 1, 1]
>>> d*3
Traceback (most recent call last):
File "<stdin>", line 1, in <mole>
d * 2
TypeError: unsupported operand type(s) for *: 'set' and 'int'
(作者的小觀點:既然集合是不能重復的,那麼乘以、重復是沒有意義的)
④ 從鍵盤讀取一個字元串,使用集合類型,輸出該字元串中有哪些不相同的字元(用Python)
# 從鍵盤讀取一個字元串,使用集合類型,輸出該字元串中有哪些不相同的字元(用Python)
def func_2():
str = input("請輸入任意字元串:")
i = 0
list = []
while i<len(str):
if str[i] in list:
pass
else:
list.append(str[i])
i+=1
print(f"字元串中不相同的字元:{list}")
#執行函數
func_2()
⑤ Python中list,tuple,dict,set的區別和用法
python 中list,tuple,dict,set是最常用的集合類型。
list列表,相當於一個數組,不過list的長度是自動變化的而且列表元素自由的,不必每個元素都是同一種類型。它的簡潔的定義方式是a=[]。有序組合
tuple也是一個組合。不過tuple在定義好之後就不能再變化。它的簡潔的定義方式是a=1,3也可以是a=(1,3).有序組合。
dict是字典類型。也就是鍵值對類型。鍵名不可以重復,並且不可以變化(字元串就符合這個要求,常用字元串作為鍵名)。它的簡潔的定義方式是a={}.無序組合(意思就是你無法按照添加的順序對他進行遍歷)。
set是set類型(不好翻譯,用的也少)。也是一個無序的組合,元素是互斥的,也就不會出現相同的元素。可以把一個序列轉換成無重復元素的set.無序組合。
⑥ Python讀取鍵盤輸入的2種方法
這篇文章主要介紹了Python讀取鍵盤輸入的2種方法,主要使用的就是raw_input函數和input函數,本文分別給出使用實例,需要的朋友可以參考下
Python提供了兩個內置函數從標准輸入讀入一行文本,默認的標准輸入是鍵盤。如下:
1.raw_input
2.input
raw_input函數
raw_input()
函數從標准輸入讀取一個行,並返回一個字元串(去掉結尾的換行符):
代碼如下:
str
=
raw_input("Enter
your
input:
");
print
"Received
input
is
:
",
str
這將提示你輸入任意字元串,然後在屏幕上顯示相同的字元串。當我輸入"Hello
Python!",它的輸出如下:
代碼如下:
Enter
your
input:
Hello
Python
Received
input
is
:
Hello
Python
input函數
input()
函數和raw_input()
函數基本可以互換,但是input會假設你的輸入是一個有效的Python表達式,並返回運算結果。這應該是兩者的最大區別。
代碼如下:
str
=
input("Enter
your
input:
");
print
"Received
input
is
:
",
str
這會產生如下的對應著輸入的結果:
代碼如下:
Enter
your
input:
[x*5
for
x
in
range(2,10,2)]
Recieved
input
is
:
[10,
20,
30,
40]
⑦ 在Python中怎麼取出集合中的健
集合只有鍵,又沒有值,只有字典才有鍵和值的區分
所以用集合名就可以取出所有鍵了
⑧ python中空集合是怎麼表示的也請詳細的說下集合的用法!謝謝
集合就是s=set(),s是隨意的字母,下面是集合的用法
len(s)
set 的長度
x in s
測試 x 是否是 s 的成員
x not in s
測試 x 是否不是 s 的成員
s.issubset(t)
s <= t
測試是否 s 中的每一個元素都在 t 中
s.issuperset(t)
s >= t
測試是否 t 中的每一個元素都在 s 中
s.union(t)
s | t
返回一個新的 set 包含 s 和 t 中的每一個元素
s.intersection(t)
s & t
返回一個新的 set 包含 s 和 t 中的公共元素
s.difference(t)
s - t
返回一個新的 set 包含 s 中有但是 t 中沒有的元素
s.symmetric_difference(t)
s ^ t
返回一個新的 set 包含 s 和 t 中不重復的元素
s.update(t)
s |= t
返回增加了 set 「t」中元素後的 set 「s」
s.intersection_update(t)
s &= t
返回只保留含有 set 「t」中元素的 set 「s」
s.difference_update(t)
s -= t
返回刪除了 set 「t」中含有的元素後的 set 「s」
s.symmetric_difference_update(t)
s ^= t
返回含有 set 「t」或者 set 「s」中有而不是兩者都有的元素的 set 「s」
s.add(x)
向 set 「s」中增加元素 x
s.remove(x)
從 set 「s」中刪除元素 x, 如果不存在則引發 KeyError
s.discard(x)
如果在 set 「s」中存在元素 x, 則刪除
s.pop()
刪除並且返回 set 「s」中的一個不確定的元素, 如果為空則引發 KeyError
s.clear()
刪除 set 「s」中的所有元素
⑨ Python中list,tuple,dict,set的區別和用法
python中list,tuple,dict,set是最常用的集合類型。
list列表,相當於一個數組,不過list的長度是自動變化的而且列表元素自由的,不必每個元素都是同一種類型。它的簡潔的定義方式是a=[]。有序組合
tuple也是一個組合。不過tuple在定義好之後就不能再變化。它的簡潔的定義方式是a=1,3也可以是a=(1,3).有序組合。
dict是字典類型。也就是鍵值對類型。鍵名不可以重復,並且不可以變化(字元串就符合這個要求,常用字元串作為鍵名)。它的簡潔的定義方式是a={}.無序組合(意思就是你無法按照添加的順序對他進行遍歷)。
set是set類型(不好翻譯,用的也少)。也是一個無序的組合,元素是互斥的,也就不會出現相同的元素。可以把一個序列轉換成無重復元素的set.無序組合。
以下是使用的示例代碼。
a_tuple=(1,3423,'34')
a_list=[12,12.34,'sds']
a_dict={'key1':1,'key2':2}
a_set=set('2323')
fortina_tuple:
print('%sintuple'%t)
print('*'*10)
forlina_list:
print('%sinlist'%l)
print('*'*10)
fork,vina_dict.items():
print('key=%s,value=%sindict'%(k,v))
print('*'*10)
forsina_set:
print('%sinset'%s)
print('*'*10)
不明白可追問。