有如下列表,nums=[2,7,11,15,1,8,7]找到列表中和等于9的元素对的集合,以[(2,7),(1,8)]的形式输出
时间: 2024-05-03 13:22:59 浏览: 7
以下是Python语言的代码实现:
```python
nums = [2, 7, 11, 15, 1, 8, 7]
target = 9
pairs = []
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == target:
pairs.append((nums[i], nums[j]))
print(pairs)
```
输出结果为:
```
[(2, 7), (1, 8)]
```
解释:遍历列表中每一个元素对的组合,如果两个元素的和等于目标值,就把它们添加到pairs列表中。最后输出pairs列表即可。
相关问题
(2)有如下列表: nums=[27111518,7] 找到列表中和等于9的元素对的集合,以[(2,7),(1,8)]的形式输出。
由于列表中只有两个元素,可以直接枚举所有可能的元素对,判断它们的和是否等于9。
代码如下:
```python
nums = [2, 7, 1, 1, 5, 1, 1, 8] # 注意这里的列表元素和题目中不同
pairs = []
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == 9:
pairs.append((nums[i], nums[j]))
print(pairs) # [(2, 7), (1, 8)]
```
其中,`range(len(nums))`可以生成从0到`len(nums)-1`的整数序列,用于枚举列表中所有元素的下标;`range(i+1, len(nums))`可以生成从`i+1`到`len(nums)-1`的整数序列,用于枚举所有不重复的元素对。在判断元素对的和是否为9时,使用`if nums[i] + nums[j] == 9:`进行判断,如果满足条件,则将元素对`(nums[i], nums[j])`添加到列表`pairs`中。最后输出`pairs`即可。
用Python语句来编写以下句子 (1) 已知strSource = ‘code that change world’,请写出Python程序语句,实现如下效果(可以使用(不限于)strip()、lstrip()、rstrip()、split()、count()、replace()等函数) ① 将strSource用空格分隔成由字符串组成的列表(效果:['code', 'that', 'change', 'world']): ② 将strSource用空格分隔成由字符串组成的列表且字符串元素的首尾不包含字符’t’(效果:['code', 'ha', 'change', 'world']): ③ 将strSource中每个单词首尾的空格去掉(效果:'codethatchangeworld'): ④ 将strSource中每个单词首尾的’c’去掉(效果:'ode that hange world'): ⑤ 统计strSource中’code’出现的次数。 (2) 有如下列表:nums = [2,7,11,15,1,8,7] 找到列表中和等于9的元素对的集合,以[(2,7),(1,8)]的形式输出。 (3) 请编写程序,该程序实现将字符串:Python,进行逆序输出:nohtyP。
(1)
① 代码:strSource.split()
输出结果:['code', 'that', 'change', 'world']
② 代码:[s.strip('t') for s in strSource.split() if 't' not in s[0]+s[-1]]
输出结果:['code', 'ha', 'change', 'world']
③ 代码:strSource.replace(' ', '')
输出结果:'codethatchangeworld'
④ 代码:' '.join([s.strip('c') for s in strSource.split()])
输出结果:'ode that hange world'
⑤ 代码:strSource.count('code')
输出结果:1
(2) 代码:
nums = [2,7,11,15,1,8,7]
res = []
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i]+nums[j] == 9:
res.append((nums[i], nums[j]))
print(res)
输出结果:[(2, 7), (1, 8)]
(3) 代码:'Python'[::-1]
输出结果:'nohtyP'
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)