python - function list generator

本文介绍了Python编程中的多种实用技巧,包括如何使用*args和**kwargs参数、列表推导式、迭代器和生成器等高级特性,以及如何高效处理大量数据。

*args

传递多个变量进来

**kwargs

传递个字典过来

def func(**kwargs):
	for key, value in kwargs.items():
		print(key + ':' + value)

lambda

map(func, seq) 会遍历所有items在seq中

⚠️:要使用list(xxxxx)来读取数据
在这里插入图片描述

filter(func, seq)

表达的是一个判断,返回为True的原数值

list(map(lambda x : x % 2, range(10)))
# => [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
list(filter(lambda x : x % 2, range(10)))
# => [1, 3, 5, 7, 9]

iter() + next()

e.g 每一次调用next都会读一个数据

# Create a list of strings: flash
flash = ['jay garrick', 'barry allen', 'wally west', 'bart allen']

# Create an iterator for flash: superhero
superhero = iter(flash)

# Print each item from the iterator
print(next(superhero)) # jay garrick
print(next(superhero)) # barry allen
print(next(superhero)) # wally west
print(next(superhero)) # bart allen

Q: 要做一个这样的tuple list怎么搞?
[(0, ‘a’), (1, ‘b’), (2, ‘c’), (3, ‘dd’)]

[(100, ‘a’), (101, ‘b’), (102, ‘c’), (103, ‘dd’)]

a = ['a', 'b', 'c', 'dd']
enu_a = enumerate(a)
print(type(enu_a))
#<class 'enumerate'>

list_enu_a = list(enu_a)
print(list_enu_a)
#[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'dd')]

list(enumerate(a, start=100))
# [(100, 'a'), (101, 'b'), (102, 'c'), (103, 'dd')]

Q: 如何去遍历读取enumerate里头的值?

# Unpack and print the tuple pairs
for index1, value1 in enumerate(enu_a):
    print(index1, value1)

# Change the start index
for index2, value2 in enumerate(enu_a, start=1):
    print(index2, value2)

Q: zip()有啥用?

a = ['a', 'b', 'c', 'dd']
b = ['q', 'w', 'e', 'rr']
c = ['a', 's', 'd', 'ff']
list(zip(a, b, c))
# [('a', 'q', 'a'), ('b', 'w', 's'), ('c', 'e', 'd'), ('dd', 'rr', 'ff')]

Q: 情景介绍:Processing large amounts of Twitter data
Sometimes, the data we have to process reaches a size that is too much for a computer’s memory to handle. This is a common problem faced by data scientists. A solution to this is to process an entire data source chunk by chunk, instead of a single go all at once.

使用chunksize批量处理

# Initialize an empty dictionary: counts_dict
counts_dict ={}

# Iterate over the file chunk by chunk
for chunk in pd.read_csv('tweets.csv', chunksize=10):

    # Iterate over the column in DataFrame
    for entry in chunk['lang']:
        if entry in counts_dict.keys():
            counts_dict[entry] += 1
        else:
            counts_dict[entry] = 1

# Print the populated dictionary
print(counts_dict)

Q:这个matrix怎么用[…]一行写出来?
在这里插入图片描述

matrix = [[col for col in range(5)] for row in range(5)]

or

matrix = [[col for col in range(5)]] * 5

generator function

应用:list创造出来的数据如果很大的话非常占内存,这时候可以换一种方法处理,就是用generator,思想是逐步计算出来,而不是一次性全算出来(内存不够用)。其中yield就相当于return,具体看这篇文章。next()与其连用读取数据
e.g.

# Create a list of strings: lannister
lannister = ['cersei', 'jaime', 'tywin', 'tyrion', 'joffrey']

# Create a generator object: lengths
lengths = (len(person) for person in lannister)

# Iterate over and print the values in lengths
for value in lengths:
    print(value)

相当于

# Create a list of strings
lannister = ['cersei', 'jaime', 'tywin', 'tyrion', 'joffrey']
# Define generator function get_lengths
def get_lengths(input_list):
    """Generator function that yields the
    length of the strings in input_list."""
    # Yield the length of a string
    for person in input_list:
        yield len(person)

# Print the values generated by get_lengths()
for value in get_lengths(lannister):
    print(value)
### 关于 `streamlist` 的定义与用法 在 Python 中,`streamlist` 并不是一个内置的标准库组件或常见术语[^1]。然而,在某些特定场景下,它可能被用于描述一种数据结构或者作为第三方模块的一部分功能。以下是对其潜在含义和用途的分析: #### 可能的解释 1:流处理中的列表 如果提到 `streamlist` 是指某种形式的数据流集合,则可以将其理解为一组连续的数据项组成的序列,类似于标准的 Python 列表,但在实际应用中更倾向于动态生成或实时更新的内容。 例如,使用生成器表达式可以模拟类似的流行为: ```python def stream_generator(): i = 0 while True: yield f"item_{i}" i += 1 stream = stream_generator() for _ in range(5): print(next(stream)) ``` 此代码片段展示了如何创建一个无限长度的“流”,每次调用 `next()` 都返回新的项目[^2]。 #### 可能的解释 2:自定义类实现 StreamList 另一种可能性是开发者自行设计了一个名为 `StreamList` 的类,该类扩展了基础列表的功能并加入了额外特性(比如自动刷新机制)。下面是一个简单的例子展示此类的设计思路: ```python class StreamList(list): def __init__(self, initial_data=None, refresh_func=None): super().__init__(initial_data if initial_data is not None else []) self.refresh_func = refresh_func def update(self): """Refreshes the list with new data from provided function.""" if callable(self.refresh_func): fresh_items = self.refresh_func() self.clear() self.extend(fresh_items) # Example Usage import random def generate_random_numbers(count=3): return [random.randint(1, 10) for _ in range(count)] my_stream_list = StreamList(refresh_func=generate_random_numbers) print(my_stream_list) # Initial state might be empty depending on init params. my_stream_list.update() print(my_stream_list) # Updated content based on generated numbers. ``` 这里我们定义了一种特殊类型的容器——能够依据外部逻辑定期自我调整其内部存储元素[^3]。 #### 总结 由于缺乏具体上下文支持进一步精确阐述,“Python streamlist”的确切意义需依赖更多背景资料确认。以上仅提供两种合理推测方向供参考考虑。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值