python元组操作中如何比较元组内两个值是否相等呢

> python怎么统计元组中重复元素个数
python怎么统计元组中重复元素个数
iup965 & &
发布时间: & &
浏览:13 & &
回复:4 & &
悬赏:0.0希赛币
python如何统计元组中重复元素个数rs = (('192.168.16.1',), ('192.168.41.1',), ('192.168.41.1',))想得到结果为:192.168.16.1:1,192.168.41.1:2
c=[]for n in rs:
if n not in c:
c.append(n)
ivan1219 & &
& & (0)(0)  Python code  Python 2.7.3 (default, Apr 10 :26) [MSC v.1500 32 bit (Intel)] on win32
Type &copyright&, &credits& or &license()& for more information.
$>>$ rs = (('192.168.16.1',), ('192.168.41.1',), ('192.168.41.1',))
$>>$ d = {}
$>>$ for k in rs:
for v in k:
if v in d:
$>>$ print d
{'192.168.16.1': 1, '192.168.41.1': 2}
ivan225 & &
& & (0)(0)  Python code  from collections import C
rs = (('192.168.16.1',), ('192.168.41.1',), ('192.168.41.1',));
print(dict(Counter(rs)));
ivan149 & &
& & (0)(0)  Python code  $>>$ from collections import Counter
$>>$ rs= (('192.168.16.1',), ('192.168.41.1',), ('192.168.41.1',))
$>>$ qty=Counter()
$>>$ for item in rs:
qty[item[0]]=qty[item[0]]+1
$>>$ dict(qty)IVAN007 & &
& & (0)(0)
本问题标题:
本问题地址:
温馨提示:本问题已经关闭,不能解答。
暂无合适的专家
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&Python元组添加元素方法详解
在Python的元组中,一旦创建就不能改变。也没有提供向元组添加元素的操作函数,因此,要向已创建好的元组添加元素只能另创方法。
我们知道,元组中的每一个元素的数据类型都不限制,根据这一点我们可以这么做
# coding=utf8
tuple=('a','b','c')
print '原先的元组:',tuple
newTuple=(tuple,'d','e')
print '添加元素后的元组:',newTuple
原先的元组: ('a', 'b', 'c')
添加元素后的元组: (('a', 'b', 'c'), 'd', 'e')
虽然是重新声明了一个元组,但不影响元素的使用。新的元素有三个元素,而第一个元素是一个元组,其余的元素则是字符串。这样的元组显然不是我们想要的格式,但可以使用嵌套循环遍历二维元组也可以取得全部元素。
标签(Tag):
------分隔线----------------------------
------分隔线----------------------------转自&/blog/128508python的元组、列表、字典数据类型是很python(Here, python is a adjective)的数据结构。这些结构都是经过足够优化后的,所以如果使用好的话,在某些方面会有很大的益处。元组(Tuple)笛卡尔积中每一个元素(d1,d2,…,dn)叫作一个n元组(n-tuple)或简称元组。元组是关系数据库中的基本概念,关系是一张表,表中的每行(即数据库中的每条记录)就是一个元组,每列就是一个属性。 在二维表里,元组也称为记录。元组是python中内置的一种数据结构元组和列表十分类似,只不过元组和字符串一样是不可变的,即你不能修改元组。元组通过圆括号中用逗号分割的项目定义。元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。1、Python中元组的书面形式和规范:tuplename=(tupleitem1,tupleitem2,tupleitem3,tupleitem4)tuplename=tupleitem1,tupleitem2,tupleitem3,tupleitem4注意:定义元组的是逗号,而非括号。&&& zoo = ('wolf', 'elephant', 'penguin')&&& print 'Number of animals in the zoo is', len(zoo)Number of animals in the zoo is 3&&& new_zoo = ('monkey', 'dolphin', zoo)&&& print 'Number of animals in the new zoo is', len(new_zoo)Number of animals in the new zoo is 3&&& print 'All animals in new zoo are', new_zooAll animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))&&& print 'Animals brought from old zoo are', new_zoo[2]Animals brought from old zoo are ('wolf', 'elephant', 'penguin')&&& print 'Last animal brought from old zoo is', new_zoo[2][2]Last animal brought from old zoo is penguin一个空的元组由一对空的圆括号组成,如 myempty = ()。然而,含有单个元素的元组必须在第一个(唯一一个)项目后跟一个逗号,这样Python才能区分元组和表达式中一个带圆括号的对象。python中的元组有以下特性:任意对象的有序集合,数组的同性;通过偏移读取;一旦生成,不可改变;固定长度,支持嵌套。例子python 代码&&& (0, 'haha', (4j, 'y'))   (0, 'haha', (4j, 'y'))   &&& t = (1, 3, 'b')   &&& t[2]   'b'   &&& t[3]    Traceback (most recent call last):     File &#41&&, line 1, in &module&&/module&       t[3]   IndexError: tuple index out of range  &&& t[-1]   'b'   &&& t[0:-1]   (1, 3)   &&& t * 2   (1, 3, 'b', 1, 3, 'b')   &&& for x in t:       &span style=&white-space:pre&& &/span&print x,           1 3 b   &&& 'b' in t   True  &&& q = t + ((3, 'abc'))   &&& q   (1, 3, 'b', 3, 'abc')   &&& for x in (2, (3, 'a')):       &span style=&white-space:pre&& &/span&print x           2   (3, 'a')   &&& len(q)   5   &&& len((2, (3, 'abc')))   2   &&& (1, 2, 3)[1]   2   &&& q[1] = 'd'     Traceback (most recent call last):     File &#57&&, line 1, in &module&&/module&       q[1] = 'd'   TypeError: 'tuple' object does not support item assignment   &&& a = ('b', 'c', q)   &&& 1 in a   False  &&& q in a   True  &&& a   ('b', 'c', (1, 3, 'b', 3, 'abc'))   &&& q = 'd'&&& q'd'&span style=&white-space:pre&& &/span&   &&& a   ('b', 'c', (1, 3, 'b', 3, 'abc'))上面的例子足以说明大部分了,使用元组时最重要的一点是“一旦生成,就不可变了”。列表(List)列表就像java里的collection,所具有的特性也要比元组更多,更灵活,其特征总结如下:任意对象的有序集合;可通过偏移存取,注意,列表中的元素都是可变的,这是不同于元组的;长度可变,支持嵌套;还有一些类似java的对象引用机制由于列表的这些特性,使得列表在实际应用中被广泛使用,下面是一些例子。1) 首先是基本用法python 代码&&& l = ['a', 'b', 'c']
&&& len(l)
&&& l + ['d']
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'a', 'b', 'c']
&&& for x in l:
&span style=&white-space:pre&& &/span&print x,
2) 索引和分片,赋值(单个元素赋值,分片赋值)python 代码&&& l = ['abc', 'def', 'ghi', 123]
'ghi'
'def'
['abc', 'def', 'ghi']
&&& l[1] = 'haha'
['abc', 'haha', 'ghi', 123]
&&& l[1:] = ['apple', 'banana']
['abc', 'apple', 'banana']
&&& l[2] = [123, 345, 456]
['abc', 'apple', [123, 345, 456]]
&&& l[1:] = [123, 234, 345, 456, 567]
['abc', 123, 234, 345, 456, 567]3) 添加、排序、删除操作python 代码&&& l = ['abc', 'def', 'ghi', 123]
&&& l.append(456)
['abc', 'def', 'ghi', 123, 456]
&&& l.sort()
[123, 456, 'abc', 'def', 'ghi']
&&& del l[0]
[456, 'abc', 'def', 'ghi']
&&& del l[2:]
[456, 'abc'] 4)一些有趣的用法(来自论坛 id—咖啡舞者)& & & 去掉列表中每个元素头尾的空格:python 代码&&& freshfruit = [' banana', ' loganberry ', 'passion fruit ']
&&& [str.strip() for str in freshfruit]
['banana', 'loganberry', 'passion fruit']把列表中,大于3的元素,乘以2:python 代码&&& vec = [2, 4, 6]
&&& [2*x for x in vec if x & 3]
[8, 12] 把列表1的每一个元素和列表2的每一个元素相乘:python 代码&&& lst1 = [2, 4, 6]
&&& lst2 = [4, 3, -9]
&&& [x*y for x in lst1 for y in lst2]
[8, 6, -18, 16, 12, -36, 24, 18, -54]取获[0-10)的平方:python 代码&&& [x**2 for x in range(10)][0, 1, 4, 9, 16, 25, 36, 49, 64, 81]字典(dictionary)python里的字典就像java里的HashMap,以键值对的方式存在并操作,其特点如下通过键来存取,而非偏移量;键值对是无序的;键和值可以是任意对象;长度可变,任意嵌套;在字典里,不能再有序列操作,虽然字典在某些方面与列表类似,但不要把列表套在字典上&1) 基本操作python 代码&&& table['abc']
&&& len(table)
&&& table.keys()
['abc', 'ghi', 'def']
&&& table.values()
&&& table.has_key('def')
&&& table.items()
[('abc', 1), ('ghi', 3), ('def', 2)]2) 修改,删除,添加python 代码&&& table = {'abc':1, 'def':2, 'ghi':3}
&&& table['ghi'] = ('g', 'h', 'i')
{'abc': 1, 'ghi': ('g', 'h', 'i'), 'def': 2}
&&& del table['abc']
{'ghi': ('g', 'h', 'i'), 'def': 2}
&&& table['xyz'] = ['x', 'y', 'z']
{'xyz': ['x', 'y', 'z'], 'ghi': ('g', 'h', 'i'), 'def': 2}
在这里需要来一句,对于字典的扩充,只需定义一个新的键值对即可,而对于列表,就只能用append方法或分片赋值。3)对字典的遍历python 代码&&& table = {'abc':1, 'def':2, 'ghi':3}
&&& for key in table.keys():
&span style=&white-space:pre&& &/span&print key, '/t', table[key]
文件与java的File类相比,python的文件类要狭义一些1) 文件写python 代码&&& myfile = open('myfile', 'w')
&&& myfile.write('hello world/n')
&&& myfile.close() python的一个open语句就打开了一个文件(当给定的文件不存在时,会自动建立一个新的文件)。open的第一个参数是文件名,第二个参数是操作模式,所谓操作模式就是你打开一个文件是用来干什么的,是读,还是写(当然操作模式不仅只有读和写)。还有一件事,操作完要记得关。2) 文件读python 代码&&& myfile = open('myfile', 'r')
&&& myfile.readlinereadline()
'hello world/n' 很是简单,这样两句就顶java一长串的流嵌套,当然,java那样做也是有道理的。ok,学了不少,说实话,python的core真的没多少,也很简单,难的是何时和如何用python。}

我要回帖

更多关于 python 元组 列表 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信