2021-11-02

元组

元组(tuple):存储任意类型数据,元组不可变其内的列表中的元素可以变)。

一、创建元组

创建元组

项目

示例

输出

创建一个空元

tup1 =()

tup2 = tuple()

print(type(tup1))

print(type(tup2))

<class 'tuple'> ()

<class 'tuple'> ()

创建存储一个数据的元

tup3 = (50,)

tup4 = 50,

print(type(tup3))

print(type(tup4))

<class 'tuple'> (50,)

<class 'tuple'> (50,)

创建存储多个不同类型数据的元组

tup5 = ('hello', 10, True, 3.14)

tup6 = "a", "b", "c", "d"

print(type(tup5))

print(type(tup6))

<class 'tuple'> ('hello', 10, True, 3.14)

<class 'tuple'> ('a', 'b', 'c', 'd')

  • 元组的基本操作

元组的基本操作

项目

示例

输出

访问元组可以使用下标索引来访问元组中的值

tup1 = ('Google', 'Runoob', 1997, 2000)

tup2 = (1, 2, 3, 4, 5, 6, 7 )

print ("tup1[0]: ", tup1[0])   

print ("tup2[1:5]: ", tup2[1:5])

tup1[0]:  Google

tup2[1:5]:  (2, 3, 4, 5)

对元组进行组合

tup1 = 12,34,56

tup2 = 'abc', 'xyz'

tup3 = tup1 + tup2

print (tup3)

(12, 34, 56, 'abc', 'xyz')

删除整个元组

tup = ('Google', 'Runoob', 1997, 2000)

del(tup)

len(tuple):计算元组元素个数。

tup1 = ('Google', 'Runoob', 1997, 2000)

print(len(tup1))

4

4

对元组进行复合

tup1=('Hi!',)

print(('Hi!',) * 4)

print(tup1* 4)

('Hi!', 'Hi!', 'Hi!', 'Hi!')

('Hi!', 'Hi!', 'Hi!', 'Hi!')

tuple.index(obj):从元组中找出某个值第一个匹配项的索引值

tup1=(1,2,3,5,6,2)

print (tup1.index(2))  #2出现了两次,只返回第一个匹配项的索引值

1

统计某个元素在元组中出现的次数tuple.count(obj)

tup1=(1,2,3,5,6,2,5,8,3,2,7)

print (tup1.count(2)) #统计2出现的次数

3

sum(tuple) :求元组中的元素之和

tup1=(2,4,3,6,7)  #元组中的元素必须全部为int或float类型

print(sum(tup1))

22

tuple(seq) :将列表转换为元组

list1 = ['Google', 'Runoob', 1997, 2000]

tup1=tuple(list1)

print(list1,tup1,type(tup1))

['Google', 'Runoob', 1997, 2000] ('Google', 'Runoob', 1997, 2000) <class 'tuple'>

min(tuple) :返回元组中元素最小值

tup1=(5,4,6,1,9)

print(min(tup1))

tup2=('9','3','2','0')

print(min(tup2))

1

0

max(tuple) :返回元组中元素最大值

tup2=(5,4,6,1,9)  #元素必须为同一类型的数据类型

print(max(tup2))

tup3=('5','3','2','0')

print(max(tup3))

9

5

使用in关键字检查某个元素是否包含在元组中。语法如下:

value in tuple

tup1=(5,4,6,1,9)

print(4 in (tup1))

print(4 in(5,4,6,1,9))

True

True

使用for in遍历

girl_tuple = ("貂蝉", "狐狸精","范金链","翠花","小班")

for everyOne in girl_tuple:

    print(everyOne)

貂蝉

狐狸精

范金链

翠花

小班

使用内置函数enumerate遍历。

girl_tuple = ("貂蝉", "狐狸精","范金链","翠花","小班")

for index, everyOne in enumerate(girl_tuple):

print (str(index) + everyOne)

0貂蝉

1狐狸精

2范金链

3翠花

4小班

使用range()内置函数遍历

girl_tuple = ("貂蝉", "狐狸精","范金链","翠花","小班")

for index in range(len(girl_tuple)):

    print (girl_tuple[index])

貂蝉

狐狸精

范金链

翠花

小班

使用iter()内置函数遍历

girl_tuple = ("貂蝉", "狐狸精","范金链","翠花","小班")

for everyOne in iter(girl_tuple):

    print(everyOne)

貂蝉

狐狸精

范金链

翠花

小班

三、元组的使用实例

下面的代码是判断输入的日期是当年的第几天。将闰年和平年每月的天数分别存入元组tup1、tup2,然后判断查询的年份是平年还是闰年,分别计算天数即可。

year=int(input("请输入需要查询的年:"))

month=int(input("请输入需要查询的月:"))

day=int(input("请输入需要查询的天:"))

tup1=(31,29,31,30,31,30,31,31,30,31,30,31)   #闰年

tup2=(31,28,31,30,31,30,31,31,30,31,30,31)   #平年

if year%400==0 or (year%4==0 and year%100!=0):  #闰年

    days=sum(tup1[:month-1])

    fin=days+day

    print(month,"月",day,"日是",year,"年的第",fin,"天")

else:

    days=sum(tup2[:month-1])

    fin=days+day

    print(month,"月",day,"日是",year,"年的第",fin,"天")

运行结果:

请输入需要查询的年:2019

请输入需要查询的月:12

请输入需要查询的天:31

12 月 31 日是 2019 年的第 365 天

生产者消费者问题是一个经典的同步问题,其中生产者和消费者共享一个缓冲区,生产者向缓冲区中生产产品,消费者从缓冲区中消费产品。在多线程的环境下,生产者和消费者可能会同时访问缓冲区,因此需要对缓冲区进行同步控制。 以下是一个简单的生产者消费者问题的实现: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define BUFFER_SIZE 10 int buffer[BUFFER_SIZE]; int count = 0; int in = 0; int out = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t empty = PTHREAD_COND_INITIALIZER; pthread_cond_t full = PTHREAD_COND_INITIALIZER; void *producer(void *arg) { int i; for (i = 0; i < 20; i++) { pthread_mutex_lock(&mutex); while (count == BUFFER_SIZE) { pthread_cond_wait(&empty, &mutex); } buffer[in] = i; in = (in + 1) % BUFFER_SIZE; count++; printf("producer: produced %d\n", i); pthread_cond_signal(&full); pthread_mutex_unlock(&mutex); } pthread_exit(NULL); } void *consumer(void *arg) { int i, data; for (i = 0; i < 20; i++) { pthread_mutex_lock(&mutex); while (count == 0) { pthread_cond_wait(&full, &mutex); } data = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; printf("consumer: consumed %d\n", data); pthread_cond_signal(&empty); pthread_mutex_unlock(&mutex); } pthread_exit(NULL); } int main() { pthread_t tid1, tid2; pthread_create(&tid1, NULL, producer, NULL); pthread_create(&tid2, NULL, consumer, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); return 0; } ``` 在代码中,我们定义了一个大小为10的缓冲区,使用一个计数器count来记录缓冲区中产品的数量,in和out分别表示生产者和消费者在缓冲区中的位置。我们使用了两个条件变量empty和full来控制生产者消费者的同步。 在生产者中,当缓冲区已满时,生产者会等待empty条件变量,直到缓冲区有空位。当生产者生产完一个产品后,会唤醒消费者,并释放互斥锁。 在消费者中,当缓冲区为空时,消费者会等待full条件变量,直到缓冲区有产品。当消费者消费完一个产品后,会唤醒生产者,并释放互斥锁。 通过使用互斥锁和条件变量,我们可以保证生产者和消费者的正确同步,避免了竞争条件和死锁等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值