小编itV*_*ico的帖子

如何为文件中的每一行从字符串中选择第 n 个字符?

每行都有一个单词和一个数字。我需要以某种方式选择第 n 个字母,它们一起组成一个新单词。例如:

and 3
for 3
map 2
wrestle 1

draw
Run Code Online (Sandbox Code Playgroud)

它必须这样开始

cat char.txt | ...
Run Code Online (Sandbox Code Playgroud)

而且我只允许使用 sed(不允许使用 awk、perl,...)。

我知道如何选择所有数字

sed 's/\(.*\) \(.*\)/\2/g'
Run Code Online (Sandbox Code Playgroud)

或文字

sed 's/\(.*\) \(.*\)/\1/g'
Run Code Online (Sandbox Code Playgroud)

我在想

cat char.txt | head -c $(sed 's/\(.*\) \(.*\)/\2/g') | tail -c 1 | sed 's/\n\//g'
Run Code Online (Sandbox Code Playgroud)

但它不起作用,因为它不会迭代所有行,并且由于某种原因它甚至无法在单行上工作。

请需要一些帮助和指导

linux bash sed

5
推荐指数
1
解决办法
4748
查看次数

ClassNotFoundException CrudRepository

我正在阅读Spring 上的JPA 文档,并且正在尝试重组我的代码。

我现在拥有的:

布鲁尔存储库

@Repository
public class BrewerRepository {
    @PersistenceContext(name = "vivesPU")
    private EntityManager entityManager;

    public List<Brewer> getAll() {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

布鲁尔服务公司

@Service
public class BrewerService {

    @Autowired
    private BrewerRepository brewerRepository;

    public List<Brewer> getAll() {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

家庭控制器

@Controller
public class HomeController {

    @Autowired
    private BrewerService brewerService;

    @GetMapping("/")
    public String index(Model model) {

        List<Brewer> brewers = this.brewerService.getAll();

        model.addAttribute("brewers", brewers);

        return "index";
    }
}
Run Code Online (Sandbox Code Playgroud)

持久化JPAConfig

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories
public class PersistenceJPAConfig{

    @Bean
    public LocalContainerEntityManagerFactoryBean …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-data-jpa

5
推荐指数
2
解决办法
9221
查看次数

java 解组 LocalDateTime

这是我的适配器类:

public class LocalDateTimeAdapter extends XmlAdapter<String, LocalDateTime> {

    @Override
    public LocalDateTime unmarshal(String v) throws Exception {
        return new LocalDateTime(v);
    }

    @Override
    public String marshal(LocalDateTime v) throws Exception {
        return v.toString();
    }

}
Run Code Online (Sandbox Code Playgroud)

这是一个我想存储日期的对象类:

    @XmlAccessorType(XmlAccessType.FIELD)
    public class Object {

    @XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
    private LocalDateTime time;

    public LocalDateTime getTime() {
            return time;
    }
Run Code Online (Sandbox Code Playgroud)

由于某种原因,我无法编译它。表明问题出在return new LocalDateTime(v);。这是我得到的错误:

Error:(9, 16) java: constructor LocalDateTime in class java.time.LocalDateTime cannot be applied to given types;
      required: java.time.LocalDate,java.time.LocalTime
      found: java.lang.String
      reason: actual and formal argument lists differ in …
Run Code Online (Sandbox Code Playgroud)

java xml datetime jaxb unmarshalling

4
推荐指数
1
解决办法
4940
查看次数

Python:TypeError:“ int”对象不可下标

我收到TypeError,但我不明白为什么。错误发生于c = t[i][0](根据调试器)。我有3个字符组(名单)g1g2g3我试图通过减去关键的改变字符的索引k1k2或者k3从索引。我现在用于测试的内容:

text = 'abcd'

l_text = [('a', 0), ('b', 1), ('c', 2), ('d', 3)]

k1, k2, k3 = 2, 3, 1
Run Code Online (Sandbox Code Playgroud)

这是代码:

def rotate_left(text, l_text, k1, k2, k3):
    i = 0
    newstr = [None]*len(text)
    for t in l_text: # t = tuple
        c = t[i][0] 
        if c in g1: # c = char
            l = int(l_text[i][1]) # l = index of the char in …
Run Code Online (Sandbox Code Playgroud)

python tuples list typeerror

3
推荐指数
1
解决办法
2万
查看次数

PriorityQueue,优先插入

我想知道如何为具有特定值的PriorityQueue添加值.

我有一个Map<Integer, Integer> // element -> value ,我想elements优先插入PriorityQueue value.

例如:

Map{1=0, 3=5265, 5=22375, 7=4202, 9=233, 11=351, 13=119}
Run Code Online (Sandbox Code Playgroud)

应该在队列中有这个顺序:

{1, 13, 9, 11, 7, 3, 5}
Run Code Online (Sandbox Code Playgroud)

java priority-queue

3
推荐指数
1
解决办法
4999
查看次数

Haskell - 递归/语法

我这里有一些代码,我需要有人来解释它的作用.

fun :: (a -> Bool) -> [a] -> [a]
fun p [] = []
fun p (x:xs) = if (p x) then (x : fun p xs) else (fun p xs)
Run Code Online (Sandbox Code Playgroud)

我没有得到这个部分

a - >布尔

(PX)

所以代码遍历列表,如果它是空的,它返回一个空列表.如果列表不为空,则检查是否为空(p x).如果是,则保持元素不变并转到列表中的下一个元素,否则它将删除元素.

haskell

1
推荐指数
1
解决办法
102
查看次数