当前位置:首页 » 编程语言 » 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)")

热点内容
如何运行python代码 发布:2025-02-08 23:28:15 浏览:691
新箱子密码锁怎么设置 发布:2025-02-08 23:26:50 浏览:147
安卓如何可以看见被撤回的消息 发布:2025-02-08 23:19:17 浏览:797
火影忍者竞技场脚本 发布:2025-02-08 23:10:18 浏览:934
英国访问学者签证费用 发布:2025-02-08 23:04:46 浏览:926
洛奇合成脚本 发布:2025-02-08 22:57:04 浏览:141
linux文件软链接 发布:2025-02-08 22:35:48 浏览:773
iphone6s缓存怎么清理 发布:2025-02-08 22:33:17 浏览:928
数据库系统设计的步骤 发布:2025-02-08 22:11:19 浏览:44
processc语言 发布:2025-02-08 22:11:15 浏览:537