Py学习  »  Shubham Sharma  »  全部回复
回复总数  8
3 年前
回复了 Shubham Sharma 创建的主题 » Python Dataframe从行的每个列表中减去一个值

简单的列表理解如何:

df['new'] = [[i - 1 for i in l] for l in df['A']]

           A        new
0     [1, 2]     [0, 1]
1  [4, 5, 6]  [3, 4, 5]
3 年前
回复了 Shubham Sharma 创建的主题 » 在Python中查找整数可以与Numpy或Pandas一起驻留的最近边界值对

这似乎是使用的理想问题 np.searchsorted 但是,根据您的实际需求,有两种可能的解决方案:

  • 如果所有元素 a 保证落在边界点之间:
i = np.searchsorted(b, a)
df = pd.DataFrame({'a': a, 'lb': b[i - 1], 'tb': b[i]})
  • 如果 A. 如果不属于边界点,则更一般的解决方案是:
i = np.searchsorted(b, a)
m = ~np.isin(i, [0, len(b)])

df = pd.DataFrame({'a': a})
df.loc[m, 'lb'], df.loc[m, 'tb'] = b[i[m] - 1], b[i[m]]

后果

     a   lb   tb
0   65   49   72
1  251  240  258
2  431  420  441
3 年前
回复了 Shubham Sharma 创建的主题 » Python-将numpy数组中的值移动到底部

我们可以使用 argsort 若要获取索引,以使数字与底部对齐的方式对给定数组进行排序,请使用 np.take_along_axis 使用索引对数组进行排序

i = (arr != 0).argsort(axis=0)
np.take_along_axis(arr, i, axis=0)

array([[0., 0., 0., 0., 0., 0., 0.],
       [2., 2., 0., 0., 0., 0., 0.],
       [1., 1., 0., 0., 0., 0., 0.],
       [2., 2., 2., 0., 2., 0., 0.],
       [1., 1., 1., 0., 1., 0., 0.],
       [2., 2., 2., 0., 2., 0., 0.],
       [1., 1., 2., 0., 2., 1., 0.]])
5 年前
回复了 Shubham Sharma 创建的主题 » 如何使用多个分隔符拆分字符串(Python)

L = ['https:', '', 'expressjs.com', 'en', 'starter', 'hello-world.html']
L =  [subitem for item in L for subitem in item.split('.')]

print(L)

输出:

['https:', '', 'expressjs', 'com', 'en', 'starter', 'hello-world', 'html']
5 年前
回复了 Shubham Sharma 创建的主题 » Python中的易变性及其原因?
# case 1
x = 1
def test():
    x = 2
test()
print(x) #1

# case 2
x = 1
def test():
    global x
    x = 2
test()
print(x) #2

在案例1中,变量 x 里面 test 是本地作用域,因此更改其值不会更改外部声明的x的值 因此不会变异 测试 .

在案例2中,变量 测试 测试 测试 .


# case 3
x = [1]
def test():
    global x
    x = [2]
test()
print(x) # [2]

# case 4
x = [1]
def test():
    x[0] = 2
test()
print(x) #[2]

里面 测试 测试 ,但当您将新列表分配给 ,将创建对此列表的新引用 [2] 而改变这个新列表并不会改变外部声明的列表中的值 测试

在案例4中,列表 测试 方法保存对在函数外部声明的相同列表实例的引用 测试 ,当你打电话 x[0] 2 index 0

5 年前
回复了 Shubham Sharma 创建的主题 » python中的索引器错误

只需使用
string.find(sub) 方法

def getIndex(string, sequence):
    idx = string.find(sequence)
    if idx != -1:
        return idx
    else:
        return "Not found"

手动查找子字符串的另一种方法

def getIndex(string, sub):
    i = 0
    while i < len(string) - len(sub) + 1:
        j = 0
        while j < len(sub) :
            if string[i + j] != sub[j]:
                j = -1
                break
            j += 1

        # Substring found return the index to the caller
        if j != -1:
            return i
        i += 1

    return -1
5 年前
回复了 Shubham Sharma 创建的主题 » 为什么replace()在我的Python函数中不起作用?[副本]

您需要存储正在存储的值。 所以不是

string.replace(exception_char, exception_chars_dict[exception_char])

string = string.replace(exception_char, exception_chars_dict[exception_char])

完整代码

def replace_exception_chars(string):
    exception_chars_dict = {'Old': 'New', 'old': 'new'}
    exception_chars_keys = list(exception_chars_dict.keys())
    for exception_char in exception_chars_keys:
        if exception_char in string:
            string = string.replace(exception_char, exception_chars_dict[exception_char])
    return string

print(replace_exception_chars('Old, not old'))
6 年前
回复了 Shubham Sharma 创建的主题 » 循环python脚本

你需要申请循环运行100次,并采取你的所有网页。我希望下面的代码可以正常工作。

import requests
import pandas as pd
import urllib.parse
import urllib.request
import re
import os
import sys
import numpy as np


 url = "*******************/store/index.php"
 pagenums = np.arange(0,100)
 for i in pagenums:
     querystring ={"id":"***","act":"search","***":"***","country":"",
     "state":"*","city":"","zip":"","type":"","base":"","PAGENUM":str(i)}

     headers = {
     'Host': "www.*****",
     'Connection': "keep-alive",
     'Upgrade-Insecure-Requests': "1",
     'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 
     (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36",'Accept':"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    'Referer': "h************/store/index.php?id=********************&pagenum=2",
    'Accept-Encoding': "gzip, deflate",
    'Accept-Language': "en-US,en;q=0.9",
    'Cookie': "php_session_id_real=**********; cookname=**********; cook******",
    'cache-control': "no-cache",
    'Postman-Token': "**************************"
    }

     response = requests.request("GET", url, headers=headers,params=querystring)
     df_list = pd.read_html(response.text)
     df = df_list[-1]

        enter code here

     print(df)