當前位置:首頁 » 編程語言 » pythonorange

pythonorange

發布時間: 2022-06-05 08:00:04

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將「apple蘋果橘子orange」,英文和漢字區分開來輸出

# -*- coding:utf-8 -*-
import string
mystring = 'apple蘋果橘子orange'
english = []
chinese = []
for i in mystring.decode('utf-8'):
if i in string.ascii_letters:
english.append(i)
else:
chinese.append(i)
print 'English:'+''.join(english)
print 'Chinese:'+''.join(chinese)

③ python輸出五種顏色隨機兩種的隨機組合

個人覺得,你問的沒問題,但是圖片上出的題有問題,隨機2種顏色和2中所有顏色的組合,這是2個問題啊。我2中方法都寫了,你自己看吧

importrandom

colour=('red','orange','green','blue','black')


defrand_2_colour():
#隨機挑選2種顏色
foriinrandom.sample(range(0,5),2):
print('隨機挑選的2種顏色是:'+colour[i])


defall_2_colour():
#列出所有2種顏色的組合
print('所有2中顏色的組合是:')
foriinrange(0,5):
forjinrange(i+1,5):
print(colour[i],colour[j])


if__name__=='__main__':
rand_2_colour()
all_2_colour()

④ 請教如何用python按字母順序排序英文名字但是不可以用sort函數

剛才試了一下,字元串是可以直接比較的,但是要區分大小寫。
'a'<'b'
'a'>'B'
所以首先都變成小寫吧。
然後
sorted_string=rece(lambda x,y:x>y and y+x or x+y,lowered_string )
這樣會失去原字元串的大小寫。
如果想保留大小寫, 讓他們等效,恐怕需要專門定義一個函數來比較, 一個lambda搞不定。
------------------
看錯了,是排序英文名,也是同樣的方法,把每個名字第一字元拿出來比較就行了。

⑤ orange數據是什麼

orange數據是由origin生成的各項數據。


使用辦法:

1、直接打開origin的相關窗口,按照Analysis→Mathematics→Interpolate/Extrapolate→Open Dialog的順序進行點擊。

⑥ Orange關於數據挖掘的軟體,請問誰知道它那個數據是怎麼導入Orange里的,詳細點,具體操作是用python嗎

你數據是存在哪兒的,如果是存在mysql裡面的,那可以使用orngMySQL和orngSQL模塊,如下所示

t=orngMySQL.Connect('localhost','root','','test')
data=t.query("SELECT * FROM busclass")
tree=orngTree.TreeLearner(data)
orngTree.printTxt(tree,nodeStr="%V (%1.0N)",leafStr="%V (%1.0N)")

熱點內容
tomcat上傳超時 發布:2025-02-09 01:41:42 瀏覽:483
androidactivity豎屏 發布:2025-02-09 01:41:40 瀏覽:377
家庭配置怎麼合理 發布:2025-02-09 01:36:14 瀏覽:807
頭條軍事源碼 發布:2025-02-09 01:31:53 瀏覽:997
androidintent視頻 發布:2025-02-09 01:31:43 瀏覽:858
歐姆龍plc密碼如何設置 發布:2025-02-09 01:24:31 瀏覽:687
php支持jpeg 發布:2025-02-09 01:24:22 瀏覽:803
反編譯去注冊碼 發布:2025-02-09 01:19:48 瀏覽:887
安卓如何查找舊密碼 發布:2025-02-09 01:17:21 瀏覽:418
hadoop雲存儲 發布:2025-02-09 01:02:49 瀏覽:453